{"id":3249,"date":"2025-12-24T10:00:38","date_gmt":"2025-12-24T15:00:38","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3249"},"modified":"2025-12-24T10:00:38","modified_gmt":"2025-12-24T15:00:38","slug":"java-tips-part-5","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-tips-part-5\/","title":{"rendered":"Java Tips Part 5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Tip 21: Use Prepared Statements<\/h2>\n\n\n\n<p>When working with JPA\/Hibernate make use of Prepared Statements that can be reused.  Basically, I&#8217;m saying do not do the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Query query = JPA.em().createNativeQuery(\"select count(*) from user u inner join\" +\n    \"address a where a.user_id=u.id and a.city='\" + city + \"'\");\n\nBigInteger val = (BigInteger)query.getSingleResult();<\/code><\/pre>\n\n\n\n<p>This is considered a statement and will have to be generated every time by Hibernate.  Instead, do this using a prepared statement that can be reused:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Query query = JPA.em().createNativeQuery(\"select count(*) from user u inner join\" +\n    \"address a where a.user_id=u.id and a.city=:city\");\nquery.setParameter(\"city\", city);\n\nBigInteger val = (BigInteger)query.getSingleResult();<\/code><\/pre>\n\n\n\n<p>Or if you&#8217;re doing JPQL in your Repository interfaces, you could do it this way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Query(value = \"select count(*) from user u inner join \" +\n    \"address a where a.user_id=u.id and a.city=:city\", nativeQuery = true)\nBigInteger findAllByNameAndDate(@Param(\"city\") String city);<\/code><\/pre>\n\n\n\n<p>Either of these 2 methods will create a prepared statement that can be reused multiple times, instead of a new one being created each time.  It&#8217;s a minor change but a significant one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tip 22: Recursion<\/h2>\n\n\n\n<p>Recursion can be a saving grace in simplifying code, it can also be a curse.  You must understand your stopping point in the recursion and how deep it may need to go.  It is very possible and very likely you can and will blow the stack by too many recursive calls going deep.  This can be costly in a high-performance system even if it doesn&#8217;t blow the stack.  You need to judge how much recursion is too much. A few iterations deep v&#8217;s hundreds or thousands of interactions can significantly slow down your process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tip 23: Native Types<\/h2>\n\n\n\n<p>Java objects are created on the heap, while native types are created on the stack and are much smaller.  Making use of native types are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>byte<\/li><li>short<\/li><li>int<\/li><li>long<\/li><li>float<\/li><li>double<\/li><li>char<\/li><li>boolean<\/li><\/ul>\n\n\n\n<p>If your data can be represented as one of these types and is short-lived, and you will not need to make use of methods of the native wrappers, this can significantly reduce memory usage, allocation time, and garbage collection.  Make intelligent use of these. Remember these can not be NULL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tip 24: StringBuilder<\/h2>\n\n\n\n<p>I spoke in <a href=\"https:\/\/www.mymiller.name\/wordpress\/programming\/java\/java-tips-part-1\/\" data-type=\"post\" data-id=\"3111\">Tip 3: Parallel Stream thread-safety<\/a> about the use of StringBuilder not being thread-safe.  If thread safety isn&#8217;t a concern, then StringBuilder is far more efficient performance-wise for concatenating strings together, than StringBuffer, or String concatenation using the + method. StringBuilder is recommended when you need to concatenate a list of strings, 2 or 3 strings just use the + method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tip 25: StringUtils.replace() v&#8217;s String.replace()<\/h2>\n\n\n\n<p>If your doing a large number of Stirng.replace() consider switching it out with Apache Commons Lang StringUtils.replace().  This outperforms String.replace() as outlined Benchmarking JDK String.replace() vs Apache Commons StringUtils.replace().  It&#8217;s easy to make use of by including in your Maven Project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.commons&lt;\/groupId&gt;\n    &lt;artifactId&gt;commons-lang3&lt;\/artifactId&gt;\n    &lt;version&gt;3.12.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n\n\n\n<p>The code change is simple enough to make from:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String test = \"My test to show you.\";\n\ntest.replace(\u201ctest\u201d, \u201csimple test\u201d);<\/code><\/pre>\n\n\n\n<p>to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>StringUtils.replace(test, \u201ctest\u201d, \u201csimple test\u201d);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tip 21: Use Prepared Statements When working with JPA\/Hibernate make use of Prepared Statements that can be reused. Basically, I&#8217;m saying do not do the following: This is considered a statement and will have to be generated every time by Hibernate. Instead, do this using a prepared statement that can be reused: Or if you&#8217;re [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3129,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[452],"tags":[69,361,362],"series":[360],"class_list":["post-3249","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java_tips","tag-java-2","tag-java-tips","tag-tips","series-java-tips"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1","jetpack-related-posts":[{"id":2067,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitofmemory\/","url_meta":{"origin":3249,"position":0},"title":"UnitOfMemory &#8211; Measure and convert you bytes","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"Not very often do I need to take a number of bytes and convert them into the most appropriate measurement. \u00a0This came up recently on a project and I thought it would be nice to just create a class to handle this for me. \u00a0So I created UnitOfMemory as enum\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2111,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitoftime\/","url_meta":{"origin":3249,"position":1},"title":"UnitOfTime &#8211; Measure and convert you milliseconds","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"Continuing my series on advanced classes that are handy to have around when needed. \u00a0I have created the UnitOfTime. \u00a0This class is used to convert a number of milliseconds over to a more understandable period of time. package name.mymiller.extensions.lang; import java.math.BigDecimal; import java.math.BigInteger; \/** * Measure UnitOfTime based on milliseconds\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3185,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-tips-part-3\/","url_meta":{"origin":3249,"position":2},"title":"Java Tips Part 3","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Continuing my Java Tips from experience that I have learned from code reviews to different programming tasks. Tip 11: Use Hibernate Statistics Now, this is only for your development profiles\/environments, not for production. Hibernate has built-in statistics on your queries. This will break down how much time is spent along\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/12\/sam-dan-truong-rF4kuvgHhU-unsplash-scaled-e1640791434235.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3925,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-data-jpa-java-records-the-ultimate-duo-for-clean-fast-query-projections\/","url_meta":{"origin":3249,"position":3},"title":"Spring Data JPA &#038; Java Records: The Ultimate Duo for Clean, Fast Query Projections","author":"Jeffery Miller","date":"November 20, 2025","format":false,"excerpt":"Architecting Efficient Data Access with Immutability As Spring developers, we spend a significant amount of time optimizing the path between the database and the client. One of the most common performance pitfalls is the over-fetching of data\u2014loading entire, complex JPA entities when all we need is a small subset of\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 3x"},"classes":[]},{"id":3153,"url":"https:\/\/www.mymiller.name\/wordpress\/architecture\/performance-nanoseconds-now-not-milliseconds\/","url_meta":{"origin":3249,"position":4},"title":"Performance?  nanoseconds, not milliseconds!","author":"Jeffery Miller","date":"March 12, 2022","format":false,"excerpt":"In today's time of high-performance computing, we no longer have the luxury of measuring our time in milliseconds, we need to move past this into nanoseconds. When I first started programming in the '90s we measured our code in seconds. That quickly gave way to milliseconds, even before the '90s\u2026","rel":"","context":"In &quot;Architecture&quot;","block_context":{"text":"Architecture","link":"https:\/\/www.mymiller.name\/wordpress\/category\/architecture\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/superhero-g470982bed_640.jpg?fit=640%2C418&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/superhero-g470982bed_640.jpg?fit=640%2C418&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/superhero-g470982bed_640.jpg?fit=640%2C418&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3569,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/integrating-prolog-with-spring-boot\/","url_meta":{"origin":3249,"position":5},"title":"Integrating Prolog with Spring Boot","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"Prolog, a declarative logic programming language, shines in solving specific types of problems that require knowledge representation and logical inference. Integrating Prolog with Spring Boot can bring the power of logic programming to your Java applications. 1. Setting Up Your Environment Add JPL Dependency: Include the Java Prolog Interface (JPL)\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3249"}],"version-history":[{"count":4,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3249\/revisions"}],"predecessor-version":[{"id":3268,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3249\/revisions\/3268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3129"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3249"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}