{"id":363,"date":"2020-01-22T09:58:08","date_gmt":"2020-01-22T09:58:08","guid":{"rendered":"https:\/\/www.javaindia.in\/blog\/?p=363"},"modified":"2020-06-11T11:40:18","modified_gmt":"2020-06-11T11:40:18","slug":"5-mistakes-java-developers-make-while-app-development","status":"publish","type":"post","link":"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/","title":{"rendered":"5 Common Mistakes That Java Developers Make While App Development"},"content":{"rendered":"<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Java is a reputed programming language serving business enterprises from the last two decades. Designed with object-oriented programming features, eliminating the complexities of other languages including C or C++, garbage collection, and a virtual machine. Java created a different way of programming and follows \u201cWrite once, run everywhere\u201d concept. This has completely changed the web application development process, but Java problems are still present. In this post, a few common mistakes are discussed that developers usually commit while development. These mistakes if addressed properly can boost the <a href=\"https:\/\/www.javaindia.in\/services\/java-web-application-development\" target=\"_blank\" rel=\"noopener\"><strong>Java web development company<\/strong><\/a> productivity to a great extent.<\/span><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#1_Ignoring_Existing_Libraries\" >1. Ignoring Existing Libraries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#2_Wrong_Switch-Case_Syntax\" >2. Wrong Switch-Case Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#3_Keeping_Resources_Engaged\" >3. Keeping Resources Engaged<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#4_Memory_Leaks\" >4. Memory Leaks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#5_Ignoring_Exceptions\" >5. Ignoring Exceptions<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#1_What_are_the_errors_in_Java\" >1. What are the errors in Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#2_What_are_the_three_types_of_errors_in_Java\" >2. What are the three types of errors in Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#3_Why_is_Java_so_important\" >3. Why is Java so important?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#4_Why_Java_is_Object-Oriented\" >4. Why Java is Object-Oriented?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.javaindia.in\/blog\/5-mistakes-java-developers-make-while-app-development\/#Wrapping_Up\" >Wrapping Up<\/a><\/li><\/ul><\/nav><\/div>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"1_Ignoring_Existing_Libraries\"><\/span><span style=\"color: #000000;\"><strong>1. Ignoring Existing Libraries<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">It\u2019s definitely a huge mistake for developers to ignore the vast collection of libraries written in Java. Before writing any code, it is important to search for available libraries, several of them have been improved over the years and are free to use. Different sorts of logging libraries, like logback and Log4j, and network related libraries, like Netty or Akka are available to ease the development work.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"2_Wrong_Switch-Case_Syntax\"><\/span><span style=\"color: #000000;\"><strong>2. Wrong Switch-Case Syntax<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">This Java issue is very embarrassing for the developers and sometimes remain hidden until executed. Fallthrough behavior present in switch statements is highly useful, but missing the \u201cbreak\u201d keyword in the syntax can lead to disastrous results. If \u201cbreak\u201d is forgotten in \u201ccase 0\u201d as mentioned below, the code will execute \u201cZero\u201d followed by \u201cOne\u201d. The control flows present inside will compile through the whole \u201cswitch\u201d statement until \u201cbreak\u201d appears. For example:<\/span><\/p>\n<pre class=\"le lf lg lh li hk fj ci\"><span style=\"color: #000000;\"><span id=\"534a\" class=\"nu mx dx aq ns b ea nv nw l nx\" data-selectable-paragraph=\"\">public static void switchCasePrimer() {\r\nint caseIndex = 0;\r\nswitch (caseIndex) {\r\ncase 0:\r\nSystem.out.println(\"Zero\");\r\ncase 1:\r\nSystem.out.println(\"One\");\r\nbreak;\r\ncase 2:\r\nSystem.out.println(\"Two\");\r\nbreak;\r\ndefault:\r\nSystem.out.println(\"Default\");\r\n}\r\n}<\/span><\/span><\/pre>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Such mistakes can be identified using the static code analyzers and the perfect solution is to use polymorphism and move code into separate classes.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"3_Keeping_Resources_Engaged\"><\/span><span style=\"color: #000000;\"><strong>3. Keeping Resources Engaged<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Every time a program uses a file or network connection, it is highly crucial for the Java developers to free the resources once utilized. Take necessary measures if any exception were to be made during operations on those resources. You might be thinking that the FileInputStream has a finalizer that blocks the close() method on the garbage collection event, but this can\u2019t be assured. It has been noticed that when a garbage collection starts, the input stream can access the computer resources for an indefinite period of time. <a href=\"https:\/\/www.javaindia.in\/\" target=\"_blank\" rel=\"noopener\"><strong>Java development services<\/strong><\/a> deal with this problem, by implementing a try-with-resources statement to streamline the application development work.<\/span><\/p>\n<pre class=\"le lf lg lh li hk fj ci\"><span style=\"color: #000000;\"><span id=\"534a\" class=\"nu mx dx aq ns b ea nv nw l nx\" data-selectable-paragraph=\"\">private static void printFileJava7() throws IOException {\r\ntry(FileInputStream input = new FileInputStream(\"file.txt\")) {\r\nint data = input.read();\r\nwhile(data != -1){\r\nSystem.out.print((char) data);\r\ndata = input.read();\r\n}\r\n}\r\n}<\/span><\/span><\/pre>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"4_Memory_Leaks\"><\/span><span style=\"color: #000000;\"><strong>4. Memory Leaks<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Java implements automatic memory management and is a big relief to allocate and free memory manually. But this doesn\u2019t mean that Java developer should not be aware of memory management in the application. Problems with memory allocations usually happen and as long as the program creates references to objects which are useless, it will not be freed. This results in a memory leak and it can happen in multiple ways, but the major reason is everlasting object references. The garbage collector can\u2019t clear objects while there are still references to them. Another possible reason for memory leaks is a group of objects referencing each other, leading to circular dependencies.<\/span><\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"color: #000000;\">A common example of a memory leak could look like the following:<\/span><\/strong><\/p>\n<pre class=\"le lf lg lh li hk fj ci\"><span style=\"color: #000000;\"><span id=\"534a\" class=\"nu mx dx aq ns b ea nv nw l nx\" data-selectable-paragraph=\"\">final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);\r\nfinal Deque numbers = new LinkedBlockingDeque&lt;&gt;();\r\nfinal BigDecimal divisor = new BigDecimal(51);\r\nscheduledExecutorService.scheduleAtFixedRate(() -&gt; {\r\nBigDecimal number = numbers.peekLast();\r\nif (number != null &amp;&amp; number.remainder(divisor).byteValue() == 0) {\r\nSystem.out.println(\"Number: \" + number);\r\nSystem.out.println(\"Deque size: \" + numbers.size());\r\n}\r\n}, 10, 10, TimeUnit.MILLISECONDS);\r\n\r\nscheduledExecutorService.scheduleAtFixedRate(() -&gt; {\r\nnumbers.add(new BigDecimal(System.currentTimeMillis()));\r\n}, 10, 10, TimeUnit.MILLISECONDS);\r\ntry {\r\nscheduledExecutorService.awaitTermination(1, TimeUnit.DAYS);\r\n} catch (InterruptedException e) {\r\ne.printStackTrace();\r\n}<\/span><\/span><\/pre>\n<blockquote><p><strong>Also Read:\u00a0<a href=\"https:\/\/www.javaindia.in\/blog\/top-features-expected-java-14\/\" target=\"_blank\" rel=\"noopener\">Top 5 Advanced Features Expected In Java 14 From Oracle<\/a><\/strong><\/p><\/blockquote>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"5_Ignoring_Exceptions\"><\/span><span style=\"color: #000000;\"><strong>5. Ignoring Exceptions<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">It is always soothing to leave exceptions unhandled. But the best practice for naive and experienced Java developers is to handle them. Exceptions are thrown on purpose, hence in most scenarios, we need to address the problems causing these exceptions. Do not overlook these, if necessary, you can simply rethrow it, show an error dialog to the user, or simply add a message to the log. At the very least, it should be explained why the exception has not been handled in order to let other developers know the reason.<\/span><\/p>\n<pre class=\"le lf lg lh li hk fj ci\"><span style=\"color: #000000;\"><span id=\"534a\" class=\"nu mx dx aq ns b ea nv nw l nx\" data-selectable-paragraph=\"\">selfie = person.shootASelfie();\r\ntry {\r\nselfie.show();\r\n} catch (NullPointerException e) {\r\n\r\n}\r\nA better approach of highlighting the exceptions\u2019 insignificance is to encode this message into the exceptions\u2019 variable name, like this:\r\ntry { selfie.delete(); } catch (NullPointerException unimportant) { }<\/span><\/span><\/pre>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span><span style=\"color: #000000;\"><strong>Frequently Asked Questions<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-390\" src=\"https:\/\/www.javaindia.in\/blog\/wp-content\/uploads\/2020\/02\/Frequently-asked-questions.jpg\" alt=\"frequently asked questions\" width=\"700\" height=\"350\" srcset=\"https:\/\/www.javaindia.in\/blog\/wp-content\/uploads\/2020\/02\/Frequently-asked-questions.jpg 700w, https:\/\/www.javaindia.in\/blog\/wp-content\/uploads\/2020\/02\/Frequently-asked-questions-300x150.jpg 300w, https:\/\/www.javaindia.in\/blog\/wp-content\/uploads\/2020\/02\/Frequently-asked-questions-360x180.jpg 360w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"1_What_are_the_errors_in_Java\"><\/span><span style=\"color: #000000;\"><strong>1. What are the errors in Java?<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">An Error \u201crefers to serious problems that an application can\u2019t catch.\u201d Both Errors and Exceptions are the subclasses of java.lang.Throwable class. Errors are the situations which are difficult to recover by any handling techniques. It surely causes program termination in an abnormal manner.<\/span><\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"2_What_are_the_three_types_of_errors_in_Java\"><\/span><span style=\"color: #000000;\"><strong>2. What are the three types of errors in Java?<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">There are three types of errors: syntax errors, runtime errors, and logic errors.<\/span><\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"3_Why_is_Java_so_important\"><\/span><span style=\"color: #000000;\"><strong>3. Why is Java so important?<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Java is a renowned programming language used to create Web and mobile applications. It was designed for flexibility and scalability, allowing developers to write code that has a feature to run on any machine, regardless of the architecture or platform.<\/span><\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"4_Why_Java_is_Object-Oriented\"><\/span><span style=\"color: #000000;\"><strong>4. Why Java is Object-Oriented?<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Java is purely object-oriented programming language because without class and object it is impossible to write any Java program. Because Java supports non-primitive data types like int, float, boolean, double, long, etc.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Wrapping_Up\"><\/span><span style=\"color: #000000;\"><strong>Wrapping Up<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #000000;\">Java as a platform simplifies multiple things in software development, depending on JVM and the language itself. The features like removing manual memory management don\u2019t fix all the problems and issues, it is important to <a href=\"https:\/\/www.javaindia.in\/hire-java-developer\" target=\"_blank\" rel=\"noopener\"><strong>hire Java developer<\/strong><\/a> having expertise in building secure &amp; engaging web application. In addition to that knowledge, practice and Java tutorials are available to avoid and address application errors. Don\u2019t forget to use static code analyzers, as they point to the real bugs.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a reputed programming language serving business enterprises from the last two decades. Designed with object-oriented programming features, eliminating the complexities of other languages including C or C++, garbage collection, and a virtual machine. Java created a different way of programming and follows \u201cWrite once, run everywhere\u201d concept. This has completely changed the web application development process, but Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/posts\/363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":8,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions\/525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/media\/368"}],"wp:attachment":[{"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javaindia.in\/blog\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}