{"id":5168,"date":"2021-03-23T11:28:02","date_gmt":"2021-03-23T11:28:02","guid":{"rendered":"https:\/\/ded9.com\/?p=5168"},"modified":"2026-02-17T11:07:48","modified_gmt":"2026-02-17T11:07:48","slug":"30-tips-on-java-programming-and-best-practices-for-beginners","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/30-tips-on-java-programming-and-best-practices-for-beginners\/","title":{"rendered":"30 Java Programming Tips and Best Practices Every Beginner Should Master"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Java is one of the most popular programming languages. Java is used in Windows, web, and mobile applications; network and consumer electronics devices; and configuration boxes. It is ubiquitous.<\/span><span id=\"more-57370\"><\/span><\/p>\n<p>More than 3 billion devices work with Java; according to Oracle, 5 billion Java cards are in use. More than 9 million developers have chosen Java to write their code, and it is widely used and the most popular Development platform.<\/p>\n<p>Here are some suggestions on how to look for or get an appointment for Java developers:<\/p>\n<h2><span style=\"font-size: 18pt;\">1. Use empty return sets instead of Null.<\/span><\/h2>\n<p>If the program returns a set with no elements, return an empty set rather than null elements. This causes many &#8220;if-else&#8221; statements to be removed during null checks.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class getLocationName {\r\n\r\n\u00a0\u00a0\u00a0\u00a0return (null == cityName? \u201c\u201d: cityName);\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">2- Use the strings carefully.<\/span><\/h2>\n<p>If two strings are connected in a &#8220;for&#8221; loop using the &#8220;+&#8221; operator, a new String object is created in each loop iteration. This wastes memory and increases Runtime. AlRuntimen is building a String prototype; you should avoid the factory and make it directly. For example:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/ Slower Instantiation\r\n\r\nString bad = new String (\"Yet another string object\");\r\n\r\n\/\/ Faster Instantiation\r\n\r\nString good = \u201cYet another string object\u201d<\/pre>\n<\/div>\n<h2>3. Avoid unnecessary objects.<\/h2>\n<p>Object Creation is one of Java&#8217;s most expensive operations (in terms of memory usage). It is therefore recommended that objects be created or initialized only if necessary. The following code provides an example:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.util.ArrayList;\r\n\r\nimport java.util.List;\r\n\r\npublic class Employees {\r\n\r\n\u00a0\u00a0\u00a0\u00a0private List Employees;\r\n\r\n\u00a0\u00a0\u00a0\u00a0public List getEmployees () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ initialize only when required\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (null == Employees) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Employees = new ArrayList ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return Employees;\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">4- Array between Array and ArrayList<\/span><\/h2>\n<p>It is often difficult for developers to decide whether to use the ArrayList structure for array data types. Both have strengths and weaknesses, and the choice depends on the requirements.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.util.ArrayList;\r\n\r\npublic class arrayVsArrayList {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int [] myArray = new int [6];\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArray [7] = 10; \/\/ ArraysOutOfBoundException\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Declaration of ArrayList. Add and Remove of elements is easy.\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ArrayList &lt;Integer&gt; myArrayList = new ArrayList &lt;&gt; ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArrayList.add (1);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArrayList.add (2);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArrayList.add (3);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArrayList.add (4);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArrayList.add (5);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myArrayList.remove (0);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt;myArrayList.size (); i ++) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cElement:\u201d + myArrayList.get (i));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Multi-dimensional Array\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int [] [] [] multiArray = new int [3] [3] [3];\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<p>1- Arrays have a fixed size, but the size varies in ArrayLists. Because the array size is constant, memory is allocated for the array type variable at declaration and initialization.<\/p>\n<p>As a result, arrays are high-speed.<\/p>\n<p>On the other hand, if the data size is unknown, using an array with more capacity avoids index errors that exceed the array size and reduces wasted storage space.<\/p>\n<p>2- Adding or removing elements from an ArrayList is much easier than from an array.<\/p>\n<p>3- An Array can be multidimensional, but an ArrayList can only have one dimension.<\/p>\n<h2><span style=\"font-size: 18pt;\">5. When does the finally code not run, try?<\/span><\/h2>\n<p>Consider the following code snippet:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class shutDownHooksDemo {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt;5; i ++)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (i == 4) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cInside Try Block.Exiting without executing Finally block.\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.exit (0);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0finally {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cInside Finally Block.\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<p>In the above program, it appears that &#8220;println&#8221; is executed five times in the finally block. However, when running the program, the user notices that the finally block is called only four times.<\/p>\n<p>In the fifth iteration, the exit function is called once and then never called again.<\/p>\n<p>This is because running the System. The exit function stops all running threads, including the current stream. Executing the exit statement does not perform the finally block after the Try.<\/p>\n<p>W Try System. Exit Trycalled, the JVM performs two cleanup tasks before stopping the program:<\/p>\n<ul>\n<li>Initially, it stops all modules registered in Runtimeme.addRuntimenHook. This is very useful because it transfers resources outside of the JVM.<\/li>\n<li>The latter is for Finalizers, whether System.runFinalizerOnExit or Runtime.runFinalizersOnExit. Finishers have long been obsolete. Finalizers can be executed on live objects when invoked by other traders, leading to adverse effects or even deadlock.<\/li>\n<\/ul>\n<blockquote>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class shutDownHooksDemo {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt;5; i ++)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0final int final_i = i;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Runtime.getRuntime (). AddShutdownHook (\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new Thread () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (final_i == 4) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cInside Try Block.Exiting without executing Finally block.\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.exit (0);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0finally {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cInside Finally Block.\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">6- Checking the individuality of a number<\/span><\/h2>\n<p>Please review the following codes and determine if they can accurately identify an individual number.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public boolean oddOrNot (int num) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0return num% 2 == 1;\r\n\r\n}<\/pre>\n<\/div>\n<p>These codes look correct, but return incorrect results every four times (statistically). Consider a negative person number; the remainder divided by 2 will not equal 1. Therefore, the return result will be false, which is incorrect!<\/p>\n<p>It can be fixed as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public boolean oddOrNot (int num) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0return (num &amp; 1)! = 0;\r\n\r\n}<\/pre>\n<\/div>\n<p>This highly optimized code fixes the problem of negative odd numbers. Because arithmetic and logic operations are much faster than division and multiplication, the results obtained in the second part are more rapid.<\/p>\n<h2><span style=\"font-size: 18pt;\">7- The difference between single quotation marks and double quotation marks<\/span><\/h2>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class Haha {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String args []) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0System.out.print (\u201cH\u201d + \u201ca\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0System.out.print ('H' + 'a');\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<p>From the code, it looks like &#8220;HaHa&#8221; is returned, but it actually returns 169 Ha. This is because if a quote with two quotation marks is used, the characters are used as a String, but if single quotation marks are used, the operands (&#8216;H&#8217; and &#8216;a) are processed. The &#8220;Larger Initial Data Converter&#8221; is set to integer values \u200b\u200b(int). After converting the integers, the numbers are added, and 169 is returned.<\/p>\n<h2><span style=\"font-size: 18pt;\">8- Prevent memory loss with simple tricks<\/span><\/h2>\n<p>Memory loss often disrupts software performance. Because Java automatically manages memory, developers have limited control over it. However, there are still some standard ways to prevent memory loss.<\/p>\n<ul>\n<li>Once the request is complete, release the database connections.<\/li>\n<li>Use the Finally block as a permanent feature.<\/li>\n<li>Release dynamic items stored in tables.<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\">9- Prevent deadlocks in Java<\/span><\/h2>\n<p>Deadlocks can occur for a variety of reasons. There is no single instruction to avoid a dead end. A deadlock usually occurs when two coordinated objects wait for each other to release their locks.<\/p>\n<p>Run the following program. This app shows a dead end. There is a stalemate because both traders are waiting for the resources that the other trader has taken. Both are waiting, and no source is released.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class DeadlockDemo {\r\n\r\n\u00a0\u00a0\u00a0public static Object addLock = new Object ();\r\n\r\n\u00a0\u00a0\u00a0public static Object subLock = new Object ();\r\n\r\n\u00a0\u00a0\u00a0public static void main (String args []) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MyAdditionThread add = new MyAdditionThread ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MySubtractionThread sub = new MySubtractionThread ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0add.start ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sub.start ();\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\nprivate static class MyAdditionThread extends Thread {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (addLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = 10, b = 3;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int c = a + b;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cAddition Thread:\u201d + c);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Holding First Lock\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {Thread.sleep (10); }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0catch (InterruptedException e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Addition Thread: Waiting for AddLock\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (subLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Threads: Holding Add and Sub Locks\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0private static class MySubtractionThread extends Thread {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (subLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = 10, b = 3;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int c = a - b;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cSubtraction Thread:\u201d + c);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Holding Second Lock\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {Thread.sleep (10); }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0catch (InterruptedException e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cSubtraction Thread: Waiting for SubLock\u2026\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (addLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Threads: Holding Add and Sub Locks\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<p>Output<\/p>\n<p dir=\"ltr\">=====<\/p>\n<p dir=\"ltr\">Addition Thread: 13<\/p>\n<p dir=\"ltr\">Subtraction Thread: 7<\/p>\n<p dir=\"ltr\">Holding First Lock\u2026<\/p>\n<p dir=\"ltr\">Holding Second Lock\u2026<\/p>\n<p dir=\"ltr\">Addition Thread: Waiting for AddLock\u2026<\/p>\n<p dir=\"ltr\">Subtraction Thread: Waiting for SubLock\u2026<\/p>\n<p>But if the thread order changes, the deadlock will be resolved.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class DeadlockSolutionDemo {\r\n\r\n\u00a0\u00a0\u00a0public static Object addLock = new Object ();\r\n\r\n\u00a0\u00a0\u00a0public static Object subLock = new Object ();\r\n\r\n\u00a0\u00a0\u00a0public static void main (String args []) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MyAdditionThread add = new MyAdditionThread ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MySubtractionThread sub = new MySubtractionThread ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0add.start ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sub.start ();\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\nprivate static class MyAdditionThread extends Thread {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (addLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = 10, b = 3;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int c = a + b;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cAddition Thread:\u201d + c);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Holding First Lock\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {Thread.sleep (10); }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0catch (InterruptedException e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Addition Thread: Waiting for AddLock\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (subLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Threads: Holding Add and Sub Locks\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0private static class MySubtractionThread extends Thread {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (addLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = 10, b = 3;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int c = a - b;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cSubtraction Thread:\u201d + c);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Holding Second Lock\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {Thread.sleep (10); }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0catch (InterruptedException e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cSubtraction Thread: Waiting for SubLock\u2026\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized (subLock) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Threads: Holding Add and Sub Locks\u2026\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<p>Output<\/p>\n<p dir=\"ltr\">=====<\/p>\n<p dir=\"ltr\">Addition Thread: 13<\/p>\n<p dir=\"ltr\">Holding First Lock\u2026<\/p>\n<p dir=\"ltr\">Addition Thread: Waiting for AddLock\u2026<\/p>\n<p dir=\"ltr\">Threads: Holding Add and Sub Locks\u2026<\/p>\n<p dir=\"ltr\">Subtraction Thread: 7<\/p>\n<p dir=\"ltr\">Holding Second Lock\u2026<\/p>\n<p dir=\"ltr\">Subtraction Thread: Waiting for SubLock\u2026<\/p>\n<p dir=\"ltr\">Threads: Holding Add and Sub Locks\u2026<\/p>\n<h2><span style=\"font-size: 18pt;\">10. Reserve memory for Java.<\/span><\/h2>\n<p>Some Java programs may require substantial CPU and RAM. Because of this, they are usually slow. RAM can be reserved for Java to improve the performance of such applications.<\/p>\n<p>For example, if we have a Tomcat web server with 10 GB of RAM. If you wish, we can use the following Command to allocate RAM for Java on this device:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">export JAVA_OPTS = \u201d$ JAVA_OPTS -Xms5000m -Xmx6000m -XX: PermSize = 1024m -XX: MaxPermSize = 2048m\u201d<\/pre>\n<\/div>\n<ul>\n<li>Xms = minimum dedicated memory<\/li>\n<li>Xmx = Maximum allocated memory<\/li>\n<li>XX: PermSize = The initial size assigned at the start of the JVM.<\/li>\n<li>XX: MaxPermSize = Maximum size allocated when setting up JVM.<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\">11- How to work with time in Java<\/span><\/h2>\n<p>There are two standard ways to work with time in Java: System.currentTimeMillis() and System.nanoTime().<\/p>\n<p>The question is which of the two to choose and under what conditions.\u00a0In principle, both perform the same operations but differ in the following ways:<\/p>\n<p><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>System.currentTimeMillis is<\/strong> in the range of 1,000ths to 15,000ths (system-dependent), whereas System.nanoTime is in the range of\u00a01,000,000ths of a second (1,000 nanoseconds).<\/span><\/p>\n<p><strong>2. System.currentTimeMillis<\/strong> has fewer clock cycles for reading operations. The nanotime System, the other System, has over 100 clock cycles.<\/p>\n<p><strong>3. System.currentTimeMillis<\/strong> indicates absolute time (milliseconds since 00:00 (January 1, 1970) (Epoch time)), but System.nanoTime does not necessarily indicate any starting point.<\/p>\n<h2><span style=\"font-size: 18pt;\">12. Choose between Float and Double<\/span><\/h2>\n<table style=\"width: 19.8231%; height: 96px;\">\n<tbody>\n<tr style=\"height: 48px;\">\n<td style=\"height: 48px;\"><strong>Decimal digits<\/strong><\/td>\n<td style=\"height: 48px;\"><strong>Used bytes<\/strong><\/td>\n<td style=\"height: 48px;\"><strong>data type<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px;\">7<\/td>\n<td style=\"height: 24px;\">4<\/td>\n<td style=\"height: 24px;\">Float<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px;\">15<\/td>\n<td style=\"height: 24px;\">8<\/td>\n<td style=\"height: 24px;\">Double<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Double is often preferred to float in software.<\/p>\n<p>Most processors have the same processing time for Float and Double operations. Double offers much greater accuracy.<\/p>\n<h2><span style=\"font-size: 18pt;\">13. Power calculation<\/span><\/h2>\n<p>To calculate power (^), Java performs a dedicated OR or XOR.\u00a0To calculate power, Java offers two options:<\/p>\n<h3><strong>1- Multiplication:<\/strong><\/h3>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">double square = double a * double a; \/\/ Optimized\r\n\r\ndouble cube = double a * double a * double a; \/\/ Non-optimized\r\n\r\ndouble cube = double a * double square; \/\/ Optimized\r\n\r\ndouble quad = double a * double a * double a * double a; \/\/ Non-optimized\r\n\r\ndouble quad = double square * double square; \/\/ Optimized<\/pre>\n<\/div>\n<h3><strong>2- (power, base) pow:<\/strong><\/h3>\n<p>From the &#8216;pow&#8217; method to calculate power where multiplication is not possible, (power ^ base);<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">double cube = Math.pow (base, exponent);<\/pre>\n<\/div>\n<p>Math.pow should be used only when necessary. For example, power is a fractional value. This is because Math.pow is typically 300-600 times slower than multiplication.<\/p>\n<h2><span style=\"font-size: 18pt;\">14. How to manage the Null exception<\/span><\/h2>\n<p>Null pointer exceptions are pretty standard in Java. This exception occurs when we attempt to call a method on a null reference. For example:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">int noOfStudents = school.listStudents (). count;<\/pre>\n<\/div>\n<p>If you received a NullPointerException in the example above, then school or listStudents () is null. It is a good idea to check the Null options as soon as possible to remove them.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">private int getListOfStudents (File [] files) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (files == null)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new NullPointerException (\"File list cannot be null\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">15. Encryption in JSON<\/span><\/h2>\n<p>JSON stands for JavaScript Object Notation, a structure for storing and exchanging data. It is an easier-to-use alternative to <a href=\"https:\/\/ded9.com\/introduction-to-xml\/\">XML<\/a>. Due to its features and low volume, <a href=\"https:\/\/en.wikipedia.org\/wiki\/JSON\" target=\"_blank\" rel=\"noopener\">JSON<\/a> has become very popular on the Internet these days.<\/p>\n<p>A typical data structure can be encrypted in JSON and easily shared on web pages. A JSON parser must be installed before you can start writing code. In the following examples, we have used JSON.<\/p>\n<p dir=\"ltr\">(https:\/\/code.google.com\/p\/json-simple\/)<\/p>\n<p>The following is a simple example of JSON encryption:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import org.json.simple.JSONObject;\r\n\r\nimport org.json.simple.JSONArray;\r\n\r\npublic class JsonEncodeDemo {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0JSONObject obj = new JSONObject ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0obj.put (\"Novel Name\", \"Godaan\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0obj.put (\"Author\", \"Munshi Premchand\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0JSONArray novelDetails = new JSONArray ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0novelDetails.add (\u201cLanguage: Hindi\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0novelDetails.add (\"Year of Publication: 1936\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0novelDetails.add (\u201cPublisher: Lokmanya Press\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0obj.put (\"Novel Details\", novelDetails);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.print (obj);\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h4>Output<\/h4>\n<p dir=\"ltr\">Novel &#8220;Novel Name&#8221;: &#8220;Godaan&#8221;, &#8220;Novel Details&#8221;: [&#8220;Language: Hindi&#8221;, &#8220;Year of Publication: 1936 \u2033,&#8221; Publisher: Lokmanya Press &#8220;],&#8221; Author &#8220;:&#8221; Munshi Premchand &#8220;}<\/p>\n<h2><span style=\"font-size: 18pt;\">16- Simple String search<\/span><\/h2>\n<p>Java suggests a library method called indexOf (). This method is used with a String object and returns the index position of the desired String. If the String is not found, the value one is returned.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class StringSearch {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String myString = \u201cI am a String!\u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (myString.indexOf (\u201cString\u201d) == -1) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cString not Found!\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cString found at:\u201d + myString.indexOf (\u201cString\u201d));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">17. List the contents of a directory<\/span><\/h2>\n<p>The following program can be used to list the contents of a list. The program receives the names of all the subdirectories and files of a folder in an array, which is then followed sequentially to list all the contents.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.io. *;\r\n\r\npublic class ListContents {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0File file = new File (\u201c\/\/ home \/\/ user \/\/ Documents \/\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String [] files = file.list ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\u201cListing contents of\u201d + file.getPath ());\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt;files.length; i ++)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (files [i]);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">18. Simple input and output<\/span><\/h2>\n<p>Java offers the FileInputStream and FileOutputStream classes for reading from and writing to files. The FileInputStream constructor accepts the input File path as an argument and creates the File input stream. Similarly, the FileOutputStream constructor agrees with the output File path as an argument and creates the output File stream. After working with the File, it is essential to &#8220;close&#8221; the streams.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.io. *;\r\n\r\npublic class myIODemo {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String args []) throws IOException {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0FileInputStream in = null;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0FileOutputStream out = null;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0in = new FileInputStream (\u201c\/\/ home \/\/ user \/\/ Documents \/\/ InputFile.txt\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0out = new FileOutputStream (\"\/\/ home \/\/ user \/\/ Documents \/\/ OutputFile.txt\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int c;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while ((c = in.read ())! = -1) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0out.write (c);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} finally {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (in! = null) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0in.close ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (out! = null) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0out.close ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">19. Execute the shell Command from Java<\/span><\/h2>\n<p>Java provides the Runtime class for executing Shell commands. Because these are external commands, exception handling is critical. In the following example, we show this with a simple example. Here we are trying to open a PDF File with the Shell Command.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.io.BufferedReader;\r\n\r\nimport java.io.InputStream;\r\n\r\nimport java.io.InputStreamReader;\r\n\r\npublic class ShellCommandExec {\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String gnomeOpenCommand = \"gnome-open \/\/home\/\/user\/\/Documents\/\/MyDoc.pdf\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Runtime rt = Runtime.getRuntime ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Process processObj = rt.exec (gnomeOpenCommand);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0InputStream stdin = processObj.getErrorStream ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0InputStreamReader isr = new InputStreamReader (stdin);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0BufferedReader br = new BufferedReader (isr);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String myoutput = \u201c\u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while ((myoutput = br.readLine ())! = null) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0myoutput = myoutput + \u201d\\ n\u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (myoutput);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0catch (Exception e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0e.printStackTrace ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">20. Use regular expressions<\/span><\/h2>\n<p>Summary of Regular Phrase Structures (Source: Oracle Website)<\/p>\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\"><strong>character<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Character x<\/td>\n<td>\n<p dir=\"ltr\">X<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Back Slash Character<\/td>\n<td>\n<p dir=\"ltr\">\\\\<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>A character with an octal value or a base of eight 0n (0 &lt;= n &lt;= 7)<\/td>\n<td>\n<p dir=\"ltr\">\\ \u06f0n<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Character with octal value or base of eight 0nn (0 &lt;= n &lt;= 7)<\/td>\n<td>\n<p dir=\"ltr\">\\ 0nn<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Character with octal value or base of eight 0mnn (0 &lt;= m &lt;= 3, 0 &lt;= n &lt;= 7)<\/td>\n<td>\n<p dir=\"ltr\">\\ 0mnn<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>A character with a hexadecimal value or a base of sixteen, 0xh<\/td>\n<td>\n<p dir=\"ltr\">\\ xhh<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Character with hexadecimal value or base sixteen 0xhhhh<\/td>\n<td>\n<p dir=\"ltr\">\\ uhhhh<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Character with hexadecimal value or base sixteen 0xh h(Character.MIN_CODE_POINT &lt;= 0xh\u2026 h &lt;= Character.MAX_CODE_POINT)<\/td>\n<td>\n<p dir=\"ltr\">\\ x {h\u2026 h}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Tab character (&#8216;\\ u0009&#8217;)<\/td>\n<td>\n<p dir=\"ltr\">\\ t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>New Line Character &#8211; Line Swap (&#8216;\\ u000A&#8217;)<\/td>\n<td>\n<p dir=\"ltr\">\\ n<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Return transport character (&#8216;\\ u000D&#8217;)<\/td>\n<td>\n<p dir=\"ltr\">\\ r<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\u00a0Form feed character (&#8216;\\ u000C&#8217;)<\/td>\n<td>\n<p dir=\"ltr\">\\ f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Alarm character (alarm) (&#8216;\\ u0007&#8217;)<\/td>\n<td>\n<p dir=\"ltr\">\\ a<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Skip character (&#8216;\\ u001B&#8217;)<\/td>\n<td>\n<p dir=\"ltr\">\\ e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Control character related to x<\/td>\n<td>\n<p dir=\"ltr\">\\ cx<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Character classes<\/td>\n<\/tr>\n<tr>\n<td>a, b, or c (simple class)<\/td>\n<td>\n<p dir=\"ltr\">[abc]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>All characters except a, b, or c (contradictory)<\/td>\n<td>\n<p dir=\"ltr\">[^ abc]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>a to z or A to Z, including (range)<\/td>\n<td>\n<p dir=\"ltr\">[a-zA-Z]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>a to d or m to: p [a-dm-p] (community)<\/td>\n<td>\n<p dir=\"ltr\">[ad [mp]]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>d, e, or f (subscription)<\/td>\n<td>\n<p dir=\"ltr\">[az &amp;&amp; [def]]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>A to z except b and: c [ad-z] (subtraction)<\/td>\n<td>\n<p dir=\"ltr\">[az &amp;&amp; [^ bc]]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>a to z and not m to p: [a-lq-z] (subtraction)<\/td>\n<td>\n<p dir=\"ltr\">[az &amp;&amp; [^ mp]]<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Character classes defined<\/td>\n<\/tr>\n<tr>\n<td>Each character may or may not match the endpoint of the line.<\/td>\n<td>.<\/td>\n<\/tr>\n<tr>\n<td>One digit: [9-0]<\/td>\n<td>\n<p dir=\"ltr\">\\ d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Non-numeric character: [9-0%]<\/td>\n<td>\n<p dir=\"ltr\">\\ D<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>An empty space character: [\\ t \\ n \\ x0B \\ f \\ r]<\/td>\n<td>\n<p dir=\"ltr\">\\ s<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>No space characters: [S\\^ ^]<\/td>\n<td>\n<p dir=\"ltr\">\\ S<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>A word character: [a-zA-Z_0-9]<\/td>\n<td>\n<p dir=\"ltr\">\\ w<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>A non-verbal character: [w \\ ^]<\/td>\n<td>\n<p dir=\"ltr\">\\ W<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>Boundary adapters<\/strong><\/td>\n<\/tr>\n<tr>\n<td>The beginning of a line<\/td>\n<td>\n<p dir=\"ltr\">^<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>End of a line<\/td>\n<td>\n<p dir=\"ltr\">$<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Word border<\/td>\n<td>\n<p dir=\"ltr\">\\ b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Non-word border<\/td>\n<td>\n<p dir=\"ltr\">\\ B<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Start input<\/td>\n<td>\n<p dir=\"ltr\">\\ A<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>End of previous matching<\/td>\n<td>\n<p dir=\"ltr\">\\ G<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>Input end, but if there is a last endpoint<\/td>\n<td>\n<p dir=\"ltr\">\\ Z<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>End of input<\/td>\n<td>\n<p dir=\"ltr\">\\ z<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.util.regex.Matcher;\r\n\r\nimport java.util.regex.Pattern;\r\n\r\npublic class RegexMatches\r\n\r\n{\r\n\r\n\u00a0\u00a0\u00a0\u00a0private static String pattern = \u201c^ [_ A-Za-z0-9 -] + (\\\\. [_ A-Za-z0-9 -] +) * @ [A-Za-z0-9] + (\\\\. [A-Za-z0-9] +) * (\\\\. [A-Za-z] {2,}) $ \u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0private static Pattern mypattern = Pattern.compile (pattern);\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String args []) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String valEmail1 = \u201ctestemail@domain.com\u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String invalEmail1 = \u201c@. @ Domain.com\u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String invalEmail2 = \". $$ %% @ domain.com\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String valEmail2 = \u201ctest.email@domain.com\u201d;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Is Email ID1 valid?\" + ValidateEMailID (valEmail1));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Is Email ID1 valid?\" + ValidateEMailID (invalEmail1));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Is Email ID1 valid?\" + ValidateEMailID (invalEmail2));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println (\"Is Email ID1 valid?\" + ValidateEMailID (valEmail2));\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static boolean validateEMailID (String emailID) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Matcher mtch = mypattern.matcher (emailID);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (mtch.matches ()) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return true;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return false;\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">22. Get output in PDF Format<\/span><\/h2>\n<p>Exporting a spreadsheet to PDF is a common requirement in Java applications. Using iTextPDF makes it easy to obtain PDF output.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.io.FileOutputStream;\r\n\r\nimport com.itextpdf.text.Document;\r\n\r\nimport com.itextpdf.text.Paragraph;\r\n\r\nimport com.itextpdf.text.pdf.PdfPCell;\r\n\r\nimport com.itextpdf.text.pdf.PdfPTable;\r\n\r\nimport com.itextpdf.text.pdf.PdfWriter;\r\n\r\npublic class DrawPdf {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public static void main (String [] args) throws Exception {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Document document = new Document ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PdfWriter.getInstance (document, new FileOutputStream (\u201cEmployee.pdf\u201d));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0document.open ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Paragraph para = new Paragraph (\u201cEmployee Table\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0para.setSpacingAfter (20);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0document.add (para);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PdfPTable table = new PdfPTable (3);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PdfPCell cell = new PdfPCell (new Paragraph (\"First Name\"));\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (cell);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cLast Name\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cGender\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cRam\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\"Kumar\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cMale\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cLakshmi\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cDevi\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0table.addCell (\u201cFemale\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0document.add (table);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0document.close ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">23. Measuring time<\/span><\/h2>\n<p>Many applications require very accurate time measurements.\u00a0For this purpose, Java provides static methods in the System class:<\/p>\n<p>1\u2014currentTimeMillis (): Returns the time in milliseconds of the Epoch time as a long.<\/p>\n<blockquote>\n<p dir=\"ltr\">1. long startTime = System.currentTimeMillis ();<\/p>\n<p dir=\"ltr\">2 \u066b long estimatedTime = System.currentTimeMillis () &#8211; startTime;<\/p>\n<\/blockquote>\n<p>2- nanoTime (): Converts the current value of the most accurate timer in the System to nanoseconds as long as the data. In contrast to providing absolute timing, nanoTime measures relative time intervals.<\/p>\n<blockquote>\n<p dir=\"ltr\">1. long startTime = System.nanoTime ();<\/p>\n<p dir=\"ltr\">2 estimated long estimatedTime = System.nanoTime () &#8211; startTime;<\/p>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">24. Resize the image<\/span><\/h2>\n<p>An image can be resized using AffineTransform. First, the image buffer is created from the input image, and then the image is resized.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.awt.Graphics2D;\r\n\r\nimport java.awt.geom.AffineTransform;\r\n\r\nimport java.awt.image.BufferedImage;\r\n\r\nimport java.io.File;\r\n\r\nimport javax.imageio.ImageIO;\r\n\r\npublic class RescaleImage {\r\n\r\n\u00a0\u00a0public static void main (String [] args) throws Exception {\r\n\r\n\u00a0\u00a0\u00a0\u00a0BufferedImage imgSource = ImageIO.read (new File (\"images \/\/ Image3.jpg\"));\r\n\r\n\u00a0\u00a0\u00a0\u00a0BufferedImage imgDestination = new BufferedImage (100, 100, BufferedImage.TYPE_INT_RGB);\r\n\r\n\u00a0\u00a0\u00a0\u00a0Graphics2D g = imgDestination.createGraphics ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0AffineTransform affinetransformation = AffineTransform.getScaleInstance (2, 2);\r\n\r\n\u00a0\u00a0\u00a0\u00a0g.drawRenderedImage (imgSource, affinetransformation);\r\n\r\n\u00a0\u00a0\u00a0\u00a0ImageIO.write (imgDestination, \u201cJPG\u201d, new File (\u201coutImage.jpg\u201d));\r\n\r\n\u00a0\u00a0}\r\n\r\n}\r\n\r\n\r\n<\/pre>\n<\/div>\n<h2 dir=\"ltr\"><span style=\"font-size: 18pt;\">25. Get the floating coordinates of the mouse<\/span><\/h2>\n<p>You can record mouse events by implementing the MouseMotionListener interface. When the mouse enters a specific area, the MouseMoving event starts, and the movement coordinates can be recorded. The following example illustrates this:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import java.awt.event. *;\r\n\r\nimport javax.swing. *;\r\n\r\npublic class MouseCaptureDemo extends JFrame implements MouseMotionListener\r\n\r\n{\r\n\r\n\u00a0\u00a0\u00a0\u00a0public JLabel mouseHoverStatus;\r\n\r\n\u00a0\u00a0\u00a0\u00a0public static void main (String args [])\r\n\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new MouseCaptureDemo ();\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0MouseCaptureDemo ()\r\n\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0setSize (500, 500);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0setTitle (\u201cFrame displaying Coordinates of Mouse Motion\u201d);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mouseHoverStatus = new JLabel (\"No Mouse Hover Detected.\", JLabel.CENTER);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0add (mouseHoverStatus);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0addMouseMotionListener (this);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0setVisible (true);\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0public void mouseMoved (MouseEvent e)\r\n\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mouseHoverStatus.setText (\u201cMouse Cursor Coordinates =&gt; X:\u201d + e.getX () + \u201d| Y:\u201d + e.getY ());\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0public void mouseDragged (MouseEvent e)\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">26. FileOutputStream vs. FileWriter<\/span><\/h2>\n<p>Writing to a File in Java is often done using either a FileOutputStream or a FileWriter. Sometimes, developers are challenged to choose one. This example helps them to select each one under the required conditions. Let&#8217;s take a look at the performance:<\/p>\n<h3><strong>Use: FileOutputStream<\/strong><\/h3>\n<blockquote>\n<p dir=\"ltr\">File foutput = new File (file_location_string);<\/p>\n<p dir=\"ltr\">FileOutputStream fos = new FileOutputStream (foutput);<\/p>\n<p dir=\"ltr\">BufferedWriter output = new BufferedWriter (new OutputStreamWriter (fos));<\/p>\n<h4 dir=\"ltr\">output.write (&#8220;Buffered Content&#8221;);<\/h4>\n<\/blockquote>\n<h3><strong>Use: FileWriter<\/strong><\/h3>\n<blockquote>\n<p dir=\"ltr\">FileWriter fstream = new FileWriter (file_location_string);<\/p>\n<p dir=\"ltr\">BufferedWriter output = new BufferedWriter (fstream);<\/p>\n<p dir=\"ltr\">output.write (&#8220;Buffered Content&#8221;);<\/p>\n<\/blockquote>\n<h3><strong>According to Java API specifications:<\/strong><\/h3>\n<p>FileOutputStream is for writing raw byte streams, such as image data. Use FileWriter to write character streams.<\/p>\n<p>Use FileOutputStream for image data and FileWriter for text data.<\/p>\n<h2><span style=\"font-size: 18pt;\">More suggestions<\/span><\/h2>\n<h3><span style=\"font-size: 14pt;\">1. Use Collections.<\/span><\/h3>\n<p>Java has several built-in classes, such as Vector, Stack, Hashtable, and Array. Developers are advised to use collections as much as possible for the following reasons:<\/p>\n<p>1- Using the set makes the code reusable and changeable.<\/p>\n<p>2- Collections make the code more structured and easier to understand and maintain.<\/p>\n<p>3- The set classes are well tested from different aspects, so the code quality is good.<\/p>\n<h3><span style=\"font-size: 14pt;\">2- Law 500-50-10<\/span><\/h3>\n<p>Code retention becomes very challenging in large software packages. Newcomer developers added during support projects often complain about integrated or spaghetti code. To avoid this problem or to keep the code clean, there is a straightforward Rule: 500-50-10\u066b.<\/p>\n<ul>\n<li>10: No package can have more than 10 classes.<\/li>\n<li>50: No method can have more than 50 lines of code.<\/li>\n<li>500: No class can have more than 500 lines of code.<\/li>\n<\/ul>\n<h3><span style=\"font-size: 14pt;\">3- SOLID class design principles<\/span><\/h3>\n<p>SOLID is based on design principles developed by Robert Martin.\u00a0According to this law:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Description<\/strong><\/td>\n<td><strong>Law<\/strong><\/td>\n<\/tr>\n<tr>\n<td>A class should have only one task or responsibility. If the class does more than one task, it can lead to confusion.<\/td>\n<td>Single responsibility principle(Principle of unit responsibility)<\/td>\n<\/tr>\n<tr>\n<td>Developers need to focus more on expanding software institutions rather than changing them.<\/td>\n<td>Open \/ closed principle(Open \/ closed principle)<\/td>\n<\/tr>\n<tr>\n<td>The derived class can be replaced with the base class.<\/td>\n<td>Liskov substitution principle(Principle of Liskov replacement)<\/td>\n<\/tr>\n<tr>\n<td>It is the same as the principle of unit responsibility, but it applies to interfaces. Each interface must be responsible for a specific task. Developers need to implement methods that they do not need.<\/td>\n<td>Interface segregation principle(Interface separation principle)<\/td>\n<\/tr>\n<tr>\n<td>It depends on the abstraction; it does not rely on the link. This means that the modules must be separated from each other using the abstract layer that connects them.<\/td>\n<td>Dependency inversion principle(Principle of dependence inversion)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>4- Using design patterns<\/h3>\n<p>Design templates help developers apply the best software design principles to their code. They also provide a common platform for developers worldwide and standard terms that facilitate collaboration and communication.<\/p>\n<h3>5- Documentation ideas<\/h3>\n<p>Don&#8217;t just start writing code: strategy, preparation, document, review, and implementation. Write down your needs first. Provide a design document. Mention the hypotheses correctly. Scrub the documents, verify, and complete them.<\/p>\n<h2>6- Using two equals ==<\/h2>\n<p>The == operator also compares the object references. Checks if two operands point to the same object (not exactly the equivalent of the same object). On the other hand, it makes a real comparison between the two disciplines.<\/p>\n<h2>7- Avoid decimal numbers with two decimal places<\/h2>\n<p>Use two-decimal-place numbers only when necessary. For example, using decimal numbers for currencies can be problematic; the BigDecimal data type should be preferred instead. Numbers with two decimal places are more useful for measurements.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are the most important Java best practices for beginners?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Focus on clean code structure, proper naming conventions, OOP principles, and effective exception handling to build maintainable applications.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why are naming conventions important in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>They improve readability, maintain consistency, and make code easier for teams to understand and maintain.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How can beginners improve Java code performance?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use efficient tools like collections, optimize loops, and apply performance techniques such as StringBuilder for heavy string operations.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java is one of the most popular programming languages. Java is used in Windows, web, and mobile applications; network and consumer electronics devices; and configuration boxes. It is ubiquitous. More than 3 billion devices work with Java; according to Oracle, 5 billion Java cards are in use. More than 9 million developers have chosen Java [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":266363,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840,1707],"class_list":["post-5168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java","tag-xml"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5168"}],"version-history":[{"count":9,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5168\/revisions"}],"predecessor-version":[{"id":267019,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5168\/revisions\/267019"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/266363"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}