{"id":2792,"date":"2018-03-25T15:31:33","date_gmt":"2018-03-25T15:31:33","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2792"},"modified":"2018-03-25T17:01:26","modified_gmt":"2018-03-25T17:01:26","slug":"avoid-creating-unnecessary-object-creation","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/avoid-creating-unnecessary-object-creation\/","title":{"rendered":"Avoid Creating Unnecessary Object Creation"},"content":{"rendered":"<p><strong>Reusing Immutable Object<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\n              \/\/Dont Use this\r\n              String strName = new String(&quot;Mugil&quot;);\r\n\r\n              \/\/Use this\r\n              String strName = &quot;Mugil&quot;;\r\n         <\/pre>\n<p>The Best Example of Immutable Object Reuse is Integer Caching in Java.Lets take the following Example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Scratch\r\n{\r\n   public static void main(String&#x5B;] args)\r\n    {\r\n        Integer a = 1000, b = 1000;  \/\/1\r\n        System.out.println(a == b);\r\n\r\n        Integer c = 100, d = 100;  \/\/2\r\n        System.out.println(c == d);\r\n   }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nfalse\r\ntrue\r\n<\/pre>\n<p>Integer class keeps a cache of Integer instances in the range of -128 to 127, and all autoboxing, literals and uses of Integer.valueOf() will return instances from that cache for the range it covers.<\/p>\n<p><strong>Note <\/strong><em>that the cache only works if you use auto-boxing or the static method Integer.valueOf(). Calling the constructor will always result in a new instance of integer, even if the value of that instance is in the -128 to 127 range<\/em>. Integer.valueOf(int). It will return the same Integer object for inputs less than 256.<\/p>\n<p><strong>Reusing Mutable Object<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org.ej;\r\n\r\nimport java.text.ParseException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\n\r\npublic class Item5 \r\n{\r\n\tpublic static void main(String&#x5B;] args) throws ParseException \r\n\t{\r\n\t\tPerson objPerson = new Person();\r\n\t\tobjPerson.initializeDates();\r\n\t\t\r\n\t\t\r\n\t\tSimpleDateFormat sdf = new SimpleDateFormat(&quot;dd-M-yyyy&quot;);\r\n\t\tString endDate = &quot;31-03-2019&quot;;\r\n\t\tDate financialYrEndDate = sdf.parse(endDate);\r\n\t\t\r\n\t\tif(financialYrEndDate.after(objPerson.getFinancialYrStartDate()))\r\n\t\t{\r\n\t\t\tSystem.out.println(&quot;Valid End Date&quot;);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\nclass Person\r\n{\r\n\tprivate Date financialYrStartDate;\t\r\n\t\r\n\tpublic void initializeDates() throws ParseException\r\n\t{\r\n\t\tSimpleDateFormat sdf = new SimpleDateFormat(&quot;dd-M-yyyy&quot;);\r\n\t\tString dateInString = &quot;01-04-2018&quot;;\r\n\t\tfinancialYrStartDate = sdf.parse(dateInString);\r\n\t}\r\n\r\n\tpublic Date getFinancialYrStartDate() {\r\n\t\treturn financialYrStartDate;\r\n\t}\r\n\r\n\tpublic void setFinancialYrStartDate(Date financialYrStartDate) {\r\n\t\tthis.financialYrStartDate = financialYrStartDate;\r\n\t}\t\r\n}\r\n<\/pre>\n<p>In the above example I know for Sure that the Financial Year End Date should be after Start Date and the Start Date is going to be same for Every Year<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org.ej;\r\n\r\nimport java.text.ParseException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\n\r\npublic class Item5 \r\n{\r\n\tpublic static void main(String&#x5B;] args) throws ParseException \r\n\t{\t\t\t\r\n\t\tSimpleDateFormat sdf = new SimpleDateFormat(&quot;dd-M-yyyy&quot;);\r\n\t\tString endDate = &quot;31-03-2019&quot;;\r\n\t\tDate financialYrEndDate = sdf.parse(endDate);\r\n\t\t\r\n\t\tif(financialYrEndDate.after(Person.financialYrStartDate ))\r\n\t\t{\r\n\t\t\tSystem.out.println(&quot;Valid End Date&quot;);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\nclass Person\r\n{\r\n\tstatic Date financialYrStartDate;\r\n\t\r\n\tstatic\r\n\t{\t\r\n\t\tSimpleDateFormat sdf = new SimpleDateFormat(&quot;dd-M-yyyy&quot;);\r\n\t\tString dateInString = &quot;01-04-2018&quot;;\t\t\r\n\t\ttry {\r\n\t\t\tfinancialYrStartDate = sdf.parse(dateInString);\r\n\t\t} catch (ParseException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\t\t\r\n}\r\n<\/pre>\n<p>Since financialYrStartDate  is going to be same it is made as Class Variable which helps to prevent unnecessary Object Creation.<\/p>\n<p><strong>Use Primitives instead of Wrapper Class<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) {\r\n    Long sum = 0L; \/\/ uses Long, not long\r\n    for (long i = 0; i &lt;= Integer.MAX_VALUE; i++) {\r\n        sum += i;\r\n    }\r\n    System.out.println(sum);\r\n}\r\n<\/pre>\n<p>It takes 43 seconds to run as Long and long primitive brings it down to 6.8 seconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reusing Immutable Object \/\/Dont Use this String strName = new String(&quot;Mugil&quot;); \/\/Use this String strName = &quot;Mugil&quot;; The Best Example of Immutable Object Reuse is Integer Caching in Java.Lets take the following Example public class Scratch { public static void main(String&#x5B;] args) { Integer a = 1000, b = 1000; \/\/1 System.out.println(a == b); Integer&hellip; <a href=\"https:\/\/codethataint.com\/blog\/avoid-creating-unnecessary-object-creation\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[245],"tags":[244],"class_list":["post-2792","post","type-post","status-publish","format-standard","hentry","category-effective-java","tag-effective-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2792"}],"version-history":[{"count":8,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2792\/revisions"}],"predecessor-version":[{"id":2800,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2792\/revisions\/2800"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}