tag:blogger.com,1999:blog-81444374713001906132017-05-16T21:38:10.434-07:00JavaTechWorldLearn And Codeajaynmdnoreply@blogger.comBlogger16125tag:blogger.com,1999:blog-8144437471300190613.post-71846134119707701272017-05-16T09:06:00.000-07:002017-05-16T21:38:10.458-07:00How hash map works in java or How get() and put() method works internally <div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Hash Map in Java-</b>&nbsp;<span style="background-color: white; text-align: justify;">Java HashMap class implements the map interface by using a hashtable. It inherits AbstractMap class and implements Map interface. it store value in key, value pair format. HashMap is known as HashMap because one ot it's technique i.e hashing.</span></span><br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-dnX2zTuSa00/WRsYtkpHClI/AAAAAAAAH_8/zf5XmiMYC-I9RCP7St00LUtK4t1airstwCLcB/s1600/HashMap.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: left;"><img alt="HashMap" border="0" height="270" src="https://4.bp.blogspot.com/-dnX2zTuSa00/WRsYtkpHClI/AAAAAAAAH_8/zf5XmiMYC-I9RCP7St00LUtK4t1airstwCLcB/s320/HashMap.PNG" width="320" /></a></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Internal Implementation of HashMap-</b></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Hashing- </b>Hashing is basically a technique where you transform a large string into or object into a short fixed length value.</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In java every object has a method <span style="color: blue;">public</span><span style="color: #cc0000;"> int</span> <span style="color: orange;">hashcode()</span> that will return a hash value for given object. And there is <span style="color: orange;">equals()</span> and <span style="color: orange;">hashcode()</span><span style="color: magenta;"> </span>contract in java which says if two object are equal they should have same hashcode as well that means it is very important to have implementation of <span style="color: orange;">equals()</span><span style="color: lime;"> </span>and <span style="color: orange;">hashcode() </span>method in your classes.&nbsp;</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="background-color: white;"><span style="color: blue;">To learn how to override equals and hascode method in java</span></span>&nbsp;<a href="https://javatechworld.com/2017/02/how-to-override-equals-and-hashcode.html" target="_blank">Click Here</a></span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-FtalUhofweY/WRvToQ7z_gI/AAAAAAAAIBE/-8Yoiv-q5kgh3TcsxRNyUzX7jmvVZlzNwCLcB/s1600/HashMap1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="284" src="https://4.bp.blogspot.com/-FtalUhofweY/WRvToQ7z_gI/AAAAAAAAIBE/-8Yoiv-q5kgh3TcsxRNyUzX7jmvVZlzNwCLcB/s320/HashMap1.PNG" width="320" /></a></div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">If you check the above diagram HashMap has a table an array of nodes and nodes are basically int hash, and the key that you sent to the HashMap and the value that you add to the table and pointer to the next node so basically the node itself is a linked list inside the table and each index of an array has it own linked list implementation.</span>&nbsp;</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>How put() and get() operation work in HashMap- </b>Using below given diagram we will understan how put() method internally work in HashMap Here we are trying to insert person scoer into HashMap and by defaul the table size is 16 (2*n) so the index of table ranges from 0 to 15.</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-TxNsuZ4qObc/WRtF1yWAxQI/AAAAAAAAIAg/-LvAIPthjGQHE86Vi21W2vZp1_oXKi7DgCLcB/s1600/Putmehtod.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="312" src="https://2.bp.blogspot.com/-TxNsuZ4qObc/WRtF1yWAxQI/AAAAAAAAIAg/-LvAIPthjGQHE86Vi21W2vZp1_oXKi7DgCLcB/s400/Putmehtod.PNG" width="400" /></a></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">First we are trying to put key <b>varun</b>&nbsp;and value <b>200</b> into the HashMap so the put api called and put api compute hash of key (Ex- hash('varun') - 2306996 ) then index will be calculatecd based on hashcode (Ex- index = 2306996 &amp; (15-1) = 4) entry will go into index 4 as a node In the similar fashion all the values will be inserted to each index and there is posibilites that each index can have more than one entry because each index implement oun linked list in such case entry will be made next to the existing entry.</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b style="color: blue;">Note:- &nbsp;</b>Java HashMap allows null key, which always goes to index 0 as hash of 'null' is zero.</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>How get operation work in java:-</b> In get operation we will call get('varun') then we will find hash of the key after that we will calculate index and check in which index value exist we will compare both hashcode and key at particular index once this is verified value will be returned.</span></div><div class="separator" style="clear: both; font-weight: bold; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><a href="https://3.bp.blogspot.com/-BK0eBlphHn4/WRtPXFP7OlI/AAAAAAAAIA0/zV-KANGn-bgDtccwfrPpeUJXe2lV5sT7QCLcB/s1600/last.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="321" src="https://3.bp.blogspot.com/-BK0eBlphHn4/WRtPXFP7OlI/AAAAAAAAIA0/zV-KANGn-bgDtccwfrPpeUJXe2lV5sT7QCLcB/s400/last.PNG" width="400" /></a></span></div><div class="separator" style="clear: both; font-weight: bold; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><br /><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Hope you have like this blog kindly suggest on the comment box for any other query contact me&nbsp;<a href="https://javatechworld.com/p/contact-me.html" target="_blank">Here</a></span></div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><br /></div></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-8144437471300190613.post-44029628908432185552017-05-14T12:03:00.000-07:002017-05-14T22:41:16.835-07:00What is Automation Testing ?<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Hi Guys Before jumping to the acceptance testing first we will discuss about manual testing and automation testing.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Manual Testing- </b>Manual Testing is performed by human sitting in front of computer carefully executing the test steps.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Automation Testing-</b> Automation Testing means using an automation testing tool to execute your test case suite.The automation software can also enter test data to the system and it will compare the expected and actual result.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">The goal of automation testing is to reduce number of test cases to be run manually&nbsp;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Why Automation Testing is required-</b> Automation Testing is important due to following reasons;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">1. Manual Testing of all work flow all fields all negative scenario is time and costly.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">2. Automation does not require human&nbsp;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">3. You can run automation test unattended.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">4. Automation increase speed of test execution</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">5. Manual testing can become boaring and error prone.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Which test case we have to automate-&nbsp;</b></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">1. Test cases that are executed repeatedly.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">2. Test cases that are very difficult to perform manually.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">3. Test cases which are time consuming.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Which test case we should not include in automation testing-</b></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">1. Test cases which are newly designed and not executed manually atleast once.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">2. Test cases for which requirement is changing frequently.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">3. Test cases which are executed on ad-hoc basis.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Automation Tools-</b></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>QTP- </b>QuickTest Professional (QTP), an automated functional testing tool that helps testers to perform automated regression testing in order to identify any gaps, errors/defects in contrary to the actual/desired results of the application under test.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Rational Robot-</b> It is IBM tool used to automate regression, functional and configuration tests.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Selenium-</b> It is open source web automation tool.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-8144437471300190613.post-31994205589749899272017-05-13T15:35:00.000-07:002017-05-14T23:24:04.990-07:00Binary Search Tree Complete Implementation.<div dir="ltr" style="text-align: left;" trbidi="on"><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Binary Tree :</b> A data struc­ture in which we have nodes con­tain­ing data and two ref­er­ences to other nodes, one on the left and one on the right.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Binary Tree con­sist of Nodes</span></div><div><div class="separator" style="clear: both; text-align: center;"></div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Nodes are noth­ing but objects of a class and each node has data and a link to the left node and right node.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Usu­ally we call the start­ing node of a tree as root.</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp;Left and right node of a Leaf node points to&nbsp;<span class="caps" style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">NULL</span>&nbsp;so you will know that you have reached to the end of the&nbsp;tree.</span></li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-eO5-UVhF9cs/WReB78OJS8I/AAAAAAAAH_M/Fj8FtxLcw1099l0SolLm18HfMinH7tT5QCEw/s1600/BST1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="291" src="https://4.bp.blogspot.com/-eO5-UVhF9cs/WReB78OJS8I/AAAAAAAAH_M/Fj8FtxLcw1099l0SolLm18HfMinH7tT5QCEw/s640/BST1.PNG" width="640" /></a></div><div><div style="background-color: white; border: 0px; color: #353535; font-stretch: inherit; font-variant-numeric: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><span style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: 600; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Oper­a­tions:</span></span></div><div style="background-color: white; border: 0px; color: #353535; font-stretch: inherit; font-variant-numeric: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: 600; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Insert(int n) :</span>&nbsp;Add a node the tree with value n. Its&nbsp;O(lgn)</span></div><div style="background-color: white; border: 0px; color: #353535; font-stretch: inherit; font-variant-numeric: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: 600; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Find(int n) :</span>&nbsp;Find a node the tree with value n. Its&nbsp;O(lgn)</span></div><div style="background-color: white; border: 0px; color: #353535; font-stretch: inherit; font-variant-numeric: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: 600; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Delete (int n)&nbsp;</span>: Delete a node the tree with value n. Its&nbsp;O(lgn)</span></div><div style="background-color: white; border: 0px; color: #353535; font-stretch: inherit; font-variant-numeric: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: 600; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Dis­play</span>(): Prints the entire tree in increas­ing order. O(n).</span></div><div style="background-color: white; border: 0px; font-stretch: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><div style="color: #353535;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Detail Expla­na­tions for the Operations given below.</span></div><div style="border: 0px; color: #353535; font-stretch: inherit; font-variant-numeric: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><span style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: 600; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Insert(int n):</span></span></div><div style="color: #353535;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span></div><ul style="border: 0px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; list-style: none; margin: 0px 0px 15px 30px; padding: 0px; vertical-align: baseline;"><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Very much sim­i­lar to find() operation.</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">To insert a node our first task is to find the place to insert the&nbsp;node.</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Take cur­rent =&nbsp;root .</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">start from the cur­rent and com­pare root.data with&nbsp;n</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">if current.data is greater than n that means we need to go to the left of the&nbsp;root.</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">if current.data is smaller than n that means we need to go to the right of the&nbsp;root.</span></li><li style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style: square; margin: 0px; padding: 0px; vertical-align: baseline;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">if any point of time cur­rent is null that means we have reached to the leaf node, insert your node here with the help of par­ent node. (See&nbsp;code)</span></li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-toS9JEFQr10/WReILVuQYGI/AAAAAAAAH_c/25-bo-6Sw3guPCfNWo_0xDKrqhLAZxO_wCLcB/s1600/bfsinsert.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="625" src="https://2.bp.blogspot.com/-toS9JEFQr10/WReILVuQYGI/AAAAAAAAH_c/25-bo-6Sw3guPCfNWo_0xDKrqhLAZxO_wCLcB/s640/bfsinsert.PNG" width="640" /></a></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div></div><div style="background-color: white; border: 0px; font-stretch: inherit; line-height: 1.8; margin-bottom: 1em; padding: 0px; vertical-align: baseline;"><input type="hidden" name="IL_IN_ARTICLE"><br /><span style="color: #353535; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Binary Search Tree Java Code Example-</b></span></div><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: grey; font-style: italic;">/**</span><span style="color: grey; font-style: italic;"> </span></span></pre><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: grey; font-style: italic;">Binary Tree Implementation in java with insert and traversing <br />process step by step procedure</span></span></pre><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: grey; font-style: italic;"> </span><span style="color: grey; font-style: italic;"> */</span></span></pre><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">package </span>tree_impl;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.LinkedList;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.List;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.Queue;<br /><span style="color: navy; font-weight: bold;">import </span>java.util.Stack;<br /><br /><span style="color: grey; font-style: italic;">/**</span><span style="color: grey; font-style: italic;"> * </span><span style="color: grey; font-style: italic; font-weight: bold;">@author namdeva</span><span style="color: grey; font-style: italic;"><br /></span><span style="color: grey; font-style: italic;"> * </span><span style="color: grey; font-style: italic;"> */</span><br /><span style="color: navy; font-weight: bold;">public class </span>Tree_Implementation <span style="color: navy; font-weight: bold;">extends </span>Tree_Node {<br /> <span style="color: navy; font-weight: bold;">int </span>count = <span style="color: blue;">0</span>;<br /> Tree_Node parent;<br /> Tree_Node root;<br /> Tree_Node current;<br /> List&lt;Integer&gt; list;<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>insert(<span style="color: navy; font-weight: bold;">int </span>data) {<br /> Tree_Node newNode = <span style="color: navy; font-weight: bold;">new </span>Tree_Node();<br /> <span style="color: navy; font-weight: bold;">if </span>(parent == <span style="color: navy; font-weight: bold;">null</span>) {<br /> newNode.setData(data);<br /> parent = newNode;<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> root = parent;<br /> <span style="color: navy; font-weight: bold;">while </span>(<span style="color: navy; font-weight: bold;">true</span>) {<br /> current = root;<br /> <span style="color: navy; font-weight: bold;">if </span>(data &lt; root.getData()) {<br /> root = root.getLeft_child();<br /> <span style="color: navy; font-weight: bold;">if </span>(root == <span style="color: navy; font-weight: bold;">null</span>) {<br /> newNode.setData(data);<br /> current.setLeft_child(newNode);<br /> root = newNode;<br /> count++;<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> root = root.getRight_child();<br /> <span style="color: navy; font-weight: bold;">if </span>(root == <span style="color: navy; font-weight: bold;">null</span>) {<br /> newNode.setData(data);<br /> current.setRight_child(newNode);<br /> root = newNode;<br /> count++;<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> }<br /> }<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>traverse() {<br /> System.out.print(<span style="color: green; font-weight: bold;">"preOrder : "</span>);<br /> preOrder(parent);<br /> System.out.println(<span style="color: green; font-weight: bold;">""</span>);<br /> System.out.print(<span style="color: green; font-weight: bold;">"postOrder : "</span>);<br /> postOrder(parent);<br /> System.out.println(<span style="color: green; font-weight: bold;">""</span>);<br /> System.out.print(<span style="color: green; font-weight: bold;">"inOrder : "</span>);<br /> inOrder(parent);<br /> System.out.println(<span style="color: green; font-weight: bold;">""</span>);<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>preOrder(Tree_Node node) {<br /> <span style="color: navy; font-weight: bold;">if </span>(node != <span style="color: navy; font-weight: bold;">null</span>) {<br /> System.out.print(node.getData() + <span style="color: green; font-weight: bold;">" "</span>);<br /> preOrder(node.getLeft_child());<br /> preOrder(node.getRight_child());<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>postOrder(Tree_Node node) {<br /> <span style="color: navy; font-weight: bold;">if </span>(node != <span style="color: navy; font-weight: bold;">null</span>) {<br /> postOrder(node.getLeft_child());<br /> postOrder(node.getRight_child());<br /> System.out.print(node.getData() + <span style="color: green; font-weight: bold;">" "</span>);<br /> }<br /><br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>inOrder(Tree_Node node) {<br /> <span style="color: navy; font-weight: bold;">if </span>(node != <span style="color: navy; font-weight: bold;">null</span>) {<br /> inOrder(node.getLeft_child());<br /> System.out.print(node.getData() + <span style="color: green; font-weight: bold;">" "</span>);<br /> inOrder(node.getRight_child());<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public boolean </span>find(<span style="color: navy; font-weight: bold;">int </span>data) {<br /> <span style="color: navy; font-weight: bold;">boolean </span>flag = <span style="color: navy; font-weight: bold;">false</span>;<br /> Tree_Node temp = parent;<br /> <span style="color: navy; font-weight: bold;">while </span>(temp != <span style="color: navy; font-weight: bold;">null</span>) {<br /> <span style="color: navy; font-weight: bold;">if </span>(data &lt; temp.getData()) {<br /> <span style="color: navy; font-weight: bold;">if </span>(data == temp.getData()) {<br /> flag = <span style="color: navy; font-weight: bold;">true</span>;<br /> <span style="color: navy; font-weight: bold;">return </span>flag;<br /> }<br /> temp = temp.getLeft_child();<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> <span style="color: navy; font-weight: bold;">if </span>(data == temp.getData()) {<br /> flag = <span style="color: navy; font-weight: bold;">true</span>;<br /> <span style="color: navy; font-weight: bold;">return </span>flag;<br /> }<br /> temp = temp.getRight_child();<br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">return </span>flag;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>preOrderTraverse() {<br /> Tree_Node temp = parent;<br /> Stack&lt;Tree_Node&gt; st = <span style="color: navy; font-weight: bold;">new </span>Stack&lt;Tree_Node&gt;();<br /> st.push(parent);<br /> System.out.print(<span style="color: green; font-weight: bold;">"preOrder : "</span>);<br /> <span style="color: navy; font-weight: bold;">while </span>(<span style="color: navy; font-weight: bold;">true</span>) {<br /> <span style="color: navy; font-weight: bold;">if </span>(temp != <span style="color: navy; font-weight: bold;">null</span>) {<br /> System.out.print(temp.getData() + <span style="color: green; font-weight: bold;">" "</span>);<br /> st.push(temp);<br /> temp = temp.getLeft_child();<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> temp = st.pop();<br /> temp = temp.getRight_child();<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(st.isEmpty()) {<br /> System.out.println(<span style="color: green; font-weight: bold;">""</span>);<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /><br /> }<br /><br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>inOrderTraverse() {<br /> Tree_Node temp = parent;<br /> Stack&lt;Tree_Node&gt; st = <span style="color: navy; font-weight: bold;">new </span>Stack&lt;Tree_Node&gt;();<br /> st.push(parent);<br /> System.out.print(<span style="color: green; font-weight: bold;">"inOrder : "</span>);<br /> <span style="color: navy; font-weight: bold;">while </span>(<span style="color: navy; font-weight: bold;">true</span>) {<br /> <span style="color: navy; font-weight: bold;">if </span>(temp != <span style="color: navy; font-weight: bold;">null</span>) {<br /> st.push(temp);<br /> temp = temp.getLeft_child();<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> temp = st.pop();<br /> <span style="color: navy; font-weight: bold;">if </span>((temp == parent) &amp;&amp; (st.isEmpty() == <span style="color: navy; font-weight: bold;">true</span>)) {<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> System.out.print(temp.getData() + <span style="color: green; font-weight: bold;">" "</span>);<br /> temp = temp.getRight_child();<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(st.isEmpty()) {<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>postOrderTraverse() {<br /> Stack&lt;Tree_Node&gt; st = <span style="color: navy; font-weight: bold;">new </span>Stack&lt;Tree_Node&gt;();<br /> st.push(parent);<br /> System.out.println(<span style="color: green; font-weight: bold;">""</span>);<br /> System.out.print(<span style="color: green; font-weight: bold;">"postOrder : "</span>);<br /> Tree_Node prev = <span style="color: navy; font-weight: bold;">null</span>;<br /> <span style="color: navy; font-weight: bold;">while </span>(!st.isEmpty()) {<br /> Tree_Node curr = st.peek();<br /> <span style="color: navy; font-weight: bold;">if </span>(prev == <span style="color: navy; font-weight: bold;">null </span>|| prev.getLeft_child() == curr || prev.getRight_child() == curr) {<br /> <span style="color: navy; font-weight: bold;">if </span>(curr.getLeft_child() != <span style="color: navy; font-weight: bold;">null</span>) {<br /> st.push(curr.getLeft_child());<br /> } <span style="color: navy; font-weight: bold;">else if </span>(curr.getRight_child() != <span style="color: navy; font-weight: bold;">null</span>) {<br /> st.push(curr.getRight_child());<br /> }<br /><br /> } <span style="color: navy; font-weight: bold;">else if </span>(curr.getLeft_child() == prev) {<br /> <span style="color: navy; font-weight: bold;">if </span>(curr.getRight_child() != <span style="color: navy; font-weight: bold;">null</span>) {<br /> st.push(curr.getRight_child());<br /> }<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> System.out.print(curr.getData() + <span style="color: green; font-weight: bold;">" "</span>);<br /> st.pop();<br /> }<br /> prev = curr;<br /> }<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public void </span>BFS(){<br /> Queue&lt;Tree_Node&gt; q1 =<span style="color: navy; font-weight: bold;">new </span>LinkedList&lt;Tree_Node&gt;();<br /> Queue&lt;Tree_Node&gt; q2 =<span style="color: navy; font-weight: bold;">new </span>LinkedList&lt;Tree_Node&gt;();<br /> Tree_Node temp=parent;<br /> q1.offer(temp);<br /> System.out.println(<span style="color: green; font-weight: bold;">""</span>);<br /> System.out.print(<span style="color: green; font-weight: bold;">"BFS : "</span>);<br /> System.out.print(temp.getData()+ <span style="color: green; font-weight: bold;">" "</span>);<br /> <span style="color: navy; font-weight: bold;">while</span>(!(q1.isEmpty()) || !(q2.isEmpty())){<br /> <span style="color: navy; font-weight: bold;">while</span>(q1!=<span style="color: navy; font-weight: bold;">null </span>&amp;&amp; !q1.isEmpty()){<br /> temp=q1.peek();<br /> <span style="color: navy; font-weight: bold;">if</span>(temp.getLeft_child()!=<span style="color: navy; font-weight: bold;">null</span>){<br /> q2.offer(temp.getLeft_child());<br /> System.out.print(temp.getLeft_child().getData() +<span style="color: green; font-weight: bold;">" "</span>);<br /> }<br /> <span style="color: navy; font-weight: bold;">if</span>(temp.getRight_child()!=<span style="color: navy; font-weight: bold;">null</span>){<br /> q2.offer(temp.getRight_child());<br /> System.out.print(temp.getRight_child().getData() +<span style="color: green; font-weight: bold;">" "</span>);<br /> }<br /> q1.poll();<br /> }<br /> <span style="color: navy; font-weight: bold;">while</span>(q2!=<span style="color: navy; font-weight: bold;">null </span>&amp;&amp; !q2.isEmpty()){<br /> temp=q2.peek();<br /> <span style="color: navy; font-weight: bold;">if</span>(temp.getLeft_child()!=<span style="color: navy; font-weight: bold;">null</span>){<br /> q1.offer(temp.getLeft_child());<br /> System.out.print(temp.getLeft_child().getData() +<span style="color: green; font-weight: bold;">" "</span>);<br /> }<br /> <span style="color: navy; font-weight: bold;">if</span>(temp.getRight_child()!=<span style="color: navy; font-weight: bold;">null</span>){<br /> q1.offer(temp.getRight_child());<br /> System.out.print(temp.getRight_child().getData() +<span style="color: green; font-weight: bold;">" "</span>);<br /> <br /> }<br /> q2.poll();<br /> }<br /> <br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">public int </span>findHeight() {<br /> <span style="color: navy; font-weight: bold;">int </span>h=<span style="color: blue;">0</span>;<br /> Tree_Node temp=parent;<br /> h= height(temp);<br /> <span style="color: navy; font-weight: bold;">return </span>h;<br /> }<br /> <br /> <span style="color: navy; font-weight: bold;">public int </span>height(Tree_Node root){<br /> <span style="color: navy; font-weight: bold;">if</span>(root==<span style="color: navy; font-weight: bold;">null</span>){<br /> <span style="color: navy; font-weight: bold;">return </span><span style="color: blue;">0</span>;<br /> }<br /> <span style="color: navy; font-weight: bold;">int </span>left=height(root.getLeft_child());<br /> <span style="color: navy; font-weight: bold;">int </span>right=height(root.getRight_child());<br /> <span style="color: navy; font-weight: bold;">return </span>left&gt;right?left+<span style="color: blue;">1</span>:right+<span style="color: blue;">1</span>;<br /> }<br /><br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String args[]) {<br /> Tree_Implementation tree = <span style="color: navy; font-weight: bold;">new </span>Tree_Implementation();<br /> tree.insert(<span style="color: blue;">10</span>);<br /> tree.insert(<span style="color: blue;">8</span>);<br /> tree.insert(<span style="color: blue;">6</span>);<br /> tree.insert(<span style="color: blue;">9</span>);<br /> tree.insert(<span style="color: blue;">13</span>);<br /> tree.insert(<span style="color: blue;">11</span>);<br /> tree.insert(<span style="color: blue;">14</span>);<br /> tree.traverse();<br /> System.out.println(tree.find(<span style="color: blue;">15</span>));<br /> tree.preOrderTraverse();<br /> tree.inOrderTraverse();<br /> tree.postOrderTraverse();<br /> tree.BFS();<br /> System.out.println(tree.findHeight());<br /> }<br /><br />}</span><span style="font-family: &quot;courier new&quot;;"><br /></span></pre></div></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-8144437471300190613.post-17069268368540168682017-05-11T12:40:00.000-07:002017-05-13T09:02:57.481-07:00Java Programming Interview Questions And Answers For Freshers I<div dir="ltr" style="text-align: left;" trbidi="on"><span id="goog_389462166"></span><span id="goog_389462167"></span><a href="https://draft.blogger.com/"></a><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Hi Folks, In today blog am going to share basic programming interview question on java which interviewer asking in campus selection process This is one round of 10 questions i am sharing with you guys soon i will post 10 more java programming questions which recently asked in campus interview.</span><br /><h4 style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Program 1:- </span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-weight: normal;">Factorial Program in Java</span></h4><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Factorial of n is donated by n!. For example. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><br /><div style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4! = 4*3*2*1 = 24 &nbsp;</span></div><div style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5! = 5*4*3*2*1 = 120 &nbsp;</span></div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span> <span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-&nbsp;</b></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span> <br /><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">public class class </span>Factorial{<br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String args[]){<br /> <span style="color: navy; font-weight: bold;">int </span>i,fact=<span style="color: blue;">1</span>;<br /> <span style="color: navy; font-weight: bold;">int </span><span style="background-color: #ffe4ff;">number</span>=<span style="color: blue;">5</span>;<span style="color: grey; font-style: italic;">//It is the number to calculate factorial</span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">for</span>(i=<span style="color: blue;">1</span>;i&lt;=<span style="background-color: #e4e4ff;">number</span>;i++){<br /> fact=fact*i;<br /> }<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Factorial of "</span>+<span style="background-color: #e4e4ff;">number</span>+<span style="color: green; font-weight: bold;">" is: "</span>+fact);<br /> }<br />}</span></pre><h4 style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Program 2:- &nbsp;<span style="font-weight: normal;">Fibnocci series program in Java</span></span></h4><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc.&nbsp;</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-&nbsp;</b></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span></div><div><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">class </span>FibonacciSeriesExample1{<br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String args[])<br /> {<br /> <span style="color: navy; font-weight: bold;">int </span>n1=<span style="color: blue;">0</span>,n2=<span style="color: blue;">1</span>,n3,i,count=<span style="color: blue;">10</span>;<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.print(n1+<span style="color: green; font-weight: bold;">" "</span>+n2);<span style="color: grey; font-style: italic;">//printing 0 and 1 </span><span style="color: grey; font-style: italic;"><br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">for</span>(i=<span style="color: blue;">2</span>;i&lt;count;++i)<span style="color: grey; font-style: italic;">//loop starts from 2 because 0 and 1 are already printed </span><span style="color: grey; font-style: italic;"> </span>{<br /> n3=n1+n2;<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.print(<span style="color: green; font-weight: bold;">" "</span>+n3);<br /> n1=n2;<br /> n2=n3;<br /> }<br /><br /> }<br />} </span><span style="font-family: &quot;courier new&quot;; font-size: 9pt;"> </span></pre><pre style="background-color: white;"></pre><h4 style="background-color: white; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Problem 3:-</b></span><span style="font-family: &quot;courier new&quot;; font-size: 12px;"> </span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-weight: normal;">Armstrong Number in Java</span></h4><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Armstrong Number in Java: Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.</span></div><div><br /><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">153 = (1*1*1)+(5*5*5)+(3*3*3) &nbsp;</span></div></div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"> </span> <br /><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">where: &nbsp;</span></div><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">(1*1*1)=1 &nbsp;</span></div><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">(5*5*5)=125 &nbsp;</span></div><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">(3*3*3)=27 &nbsp;</span></div><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">So: &nbsp;</span></div><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">1+125+27=153 &nbsp;</span></div><div style="text-align: left;"><b style="font-family: arial, helvetica, sans-serif;">Solution-</b></div><div style="text-align: left;"><span style="background-color: white; color: navy; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-weight: bold;"><br /></span> <span style="background-color: white; color: navy; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-weight: bold;">class </span><span style="background-color: white; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ArmstrongNumber{</span></div><div style="text-align: left;"><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"> <span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {<br /> <span style="color: navy; font-weight: bold;">int </span>c=<span style="color: blue;">0</span>,a,temp;<br /> <span style="color: navy; font-weight: bold;">int </span>n=<span style="color: blue;">371</span>;<span style="color: grey; font-style: italic;">//It is the number for which we are checking armstrong</span><span style="color: grey; font-style: italic;"> </span>temp=n;<br /> <span style="color: navy; font-weight: bold;">while</span>(n&gt;<span style="color: blue;">0</span>)<br /> {<br /> a=n%<span style="color: blue;">10</span>;<br /> n=n/<span style="color: blue;">10</span>;<br /> c=c+(a*a*a);<br /> }<br /> <span style="color: navy; font-weight: bold;">if</span>(temp==c)<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"armstrong number"</span>);<br /> <span style="color: navy; font-weight: bold;">else</span><span style="color: navy; font-weight: bold;"> </span></span></span></pre><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Not armstrong number"</span>);<br /> }<br />}</span></span></pre></div></div><h4 style="background-color: white; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 4:-</span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"> </span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Palindrome Program in Java</span></h4><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-</b></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span></div><div><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">class </span>PalindromeNumber{<br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String args[]){<br /> <span style="color: navy; font-weight: bold;">int </span>r,sum=<span style="color: blue;">0</span>,temp;<br /> <span style="color: navy; font-weight: bold;">int </span>n=<span style="color: blue;">454</span>;<span style="color: grey; font-style: italic;">//It is the number variable to be checked for palindrome </span><span style="color: grey; font-style: italic;"><br /></span><span style="color: grey; font-style: italic;"> </span>temp=n;<br /> <span style="color: navy; font-weight: bold;">while</span>(n&gt;<span style="color: blue;">0</span>){<br /> r=n%<span style="color: blue;">10</span>; <span style="color: grey; font-style: italic;">//getting remainder </span><span style="color: grey; font-style: italic;"> </span>sum=(sum*<span style="color: blue;">10</span>)+r;<br /> n=n/<span style="color: blue;">10</span>;<br /> }<br /> <span style="color: navy; font-weight: bold;">if</span>(temp==sum)<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"true "</span>);<br /> <span style="color: navy; font-weight: bold;">else</span><span style="color: navy; font-weight: bold;"> </span>System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"false"</span>);<br /> }<br />} </span></pre></div><pre style="background-color: white;"></pre><h4 style="background-color: white; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 5:- <span style="font-weight: normal;">String Reverse</span></span></h4><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-</b></span></div><br /><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">public class </span>reverseString {<br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {<br /> String input=<span style="color: green; font-weight: bold;">"Javatechworld"</span>;<br /> StringBuilder input1 = <span style="color: navy; font-weight: bold;">new </span>StringBuilder();<br /> input1.append(input);<br /> input1=input1.reverse();<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i=<span style="color: blue;">0</span>;i&lt;input1.length();i++)<br /> </span></pre><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.print(input1.charAt(i));<br /> }<br />}</span></pre><h4 style="background-color: white; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 6:- <span style="font-weight: normal;">Prime Number</span></span></h4><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="background-color: white; text-align: justify;">Prime number</span><span style="background-color: white; text-align: justify;">&nbsp;is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.</span></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="background-color: white; text-align: justify;"><br /></span></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="background-color: white; text-align: justify;"><b>Solution-</b></span></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="background-color: white; text-align: justify;"><b><br /></b></span></span></div><div><pre style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">class </span>PrimeNumber{<br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String args[]){<br /> <span style="color: navy; font-weight: bold;">int </span>i,m=<span style="color: blue;">0</span>,flag=<span style="color: blue;">0</span>;<br /> <span style="color: navy; font-weight: bold;">int </span>n=<span style="color: blue;">17</span>;<span style="color: grey; font-style: italic;">//it is the number to be checked </span><span style="color: grey; font-style: italic;"> </span>m=n/<span style="color: blue;">2</span>;<br /> <span style="color: navy; font-weight: bold;">for</span>(i=<span style="color: blue;">2</span>;i&lt;=m;i++){<br /> <span style="color: navy; font-weight: bold;">if</span>(n%i==<span style="color: blue;">0</span>){<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Number is not prime"</span>);<br /> flag=<span style="color: blue;">1</span>;<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">if</span>(flag==<span style="color: blue;">0</span>)<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Number is prime"</span>);<br /> }<br />} </span></pre><h4 style="background-color: white; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 7:-&nbsp;</span><span style="color: #333333; font-weight: normal;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Java Program to Reverse Array In Place</span></span></h4></div><span style="color: #333333;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-</b></span></span><br /><pre style="background-color: white;"><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">import </span>java.util.Arrays;<br /><span style="color: navy; font-weight: bold;">public class </span>ArrayReversalProgram {<br /> <span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {<br /> <span style="color: navy; font-weight: bold;">int</span>[] numbers = {<span style="color: blue;">1</span>, <span style="color: blue;">2</span>, <span style="color: blue;">3</span>, <span style="color: blue;">4</span>, <span style="color: blue;">5</span>, <span style="color: blue;">6</span>, <span style="color: blue;">7</span>};<br /> <span style="font-style: italic;">reverse</span>(numbers);</span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"> }<br /> <span style="color: grey; font-style: italic;">//reverse the given array in place </span><span style="color: grey; font-style: italic;"> </span></span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy; font-weight: bold;">public static void </span>reverse(<span style="color: navy; font-weight: bold;">int</span>[] input) {<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"original array : " </span>+ Arrays.<span style="font-style: italic;">toString</span>(input));<br /> <span style="color: grey; font-style: italic;">// handling null, empty and one element array</span><span style="color: grey; font-style: italic;"> </span></span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">if</span>(input == <span style="color: navy; font-weight: bold;">null </span>|| input.<span style="color: #660e7a; font-weight: bold;">length </span>&lt;= <span style="color: blue;">1</span>){<br /> <span style="color: navy; font-weight: bold;">return</span>;<br /> }<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i = <span style="color: blue;">0</span>; i &lt; input.<span style="color: #660e7a; font-weight: bold;">length </span>/ <span style="color: blue;">2</span>; i++)<br /> {<br /> <span style="color: navy; font-weight: bold;">int </span>temp = input[i];<br /> <span style="color: grey; font-style: italic;">// swap numbers</span><span style="color: grey; font-style: italic;"> </span>input[i] = input[input.<span style="color: #660e7a; font-weight: bold;">length </span>- <span style="color: blue;">1 </span>- i];<br /> input[input.<span style="color: #660e7a; font-weight: bold;">length </span>- <span style="color: blue;">1 </span>- i] = temp;<br /> }<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"reversed array : " </span>+ Arrays.<span style="font-style: italic;">toString</span>(input));<br /> }<br />}</span></pre><pre><h4 style="text-align: left;"><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 8:- Determine If Year Is Leap Year Java Example</span></h4><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">A leap year has 366 days. Algorithm to find a leap year <br />not a leap year else if year % 4 = 0,<br />if year % 400 = 0, leap year else if year % 100 = 0, <br />leap year <br />else, <br />not a leap year.</span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-</b></span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: #674ea7;"><b>public class</b> </span>LeapYearExample {<br /> <b><span style="color: #674ea7;">public static void</span></b> main(String[] args) {<br /> //year we want to check <br /> <b><span style="color: #674ea7;">int </span></b>year = 2004;<br /> //if year is divisible by 4, it is a leap year<br /> <b><span style="color: #674ea7;">if</span></b>((<b><span style="color: #674ea7;">year</span></b> % 400 == 0) || ((<b><span style="color: #674ea7;">year</span></b> % 4 == 0) &amp;&amp; (<span style="color: #674ea7;"><b>year </b></span>% 100 != 0)))<br /> System.<b><span style="color: #674ea7;">out</span></b>.println("<b><span style="color: #6aa84f;">Year</span></b> " + year + "<b><span style="color: lime;"> </span><span style="color: #6aa84f;">is a leap year</span></b>");<br /> else System.out.println("<b><span style="color: #6aa84f;">Year</span></b> " + year + " <b><span style="color: #6aa84f;">is not a leap year</span></b>");<br /> }<br />}</span></pre><pre></pre><h4 style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 9:-</span> <span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Check whether string is palindrome or not</span></h4><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-</b></span></div><div><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy;"><b>import</b> </span>java.io.*;</span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy;"><b>import</b> </span>java.util.Scanner;</span></pre><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: navy;"><b>public class</b> </span>Palindrom {<br /> <span style="color: navy;"><b>public static void</b> </span>main(String[] args) {<br /> Scanner scn = <span style="color: navy;"><b>new</b> </span>Scanner(System.<span style="color: #660e7a; font-style: italic;"><b>in</b></span>);<br /> String s1 = scn.nextLine();<br /> String temp = s1;<br /> StringBuffer sb = <span style="color: navy;"><b>new</b> </span>StringBuffer(s1);<br /> sb.reverse();<br /> s1 = sb.toString();<br /> <b> </b><span style="color: navy;"><b>if</b> </span>(temp.equalsIgnoreCase(s1)) {<br /> System.<span style="color: #660e7a; font-style: italic;"><b>out</b></span>.println(temp + <span style="color: green;">" <b>is palindrome</b> "</span>);<br /> }<br /> <b> <span style="color: navy;">else </span></b>{<br /> System.<span style="color: #660e7a; font-style: italic;"><b>out</b></span>.println(temp+ <span style="color: green;">" <b>is not palindrome</b>"</span>);<br /> }<br /> }<br />}</span></pre><h4 style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Problem 10:- <span style="font-weight: normal;">Java program to remove duplicate words in a string</span></span></h4></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Solution-</b></span></div><div><pre><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="color: grey; font-style: italic;">/* Java Program to remove duplicate characters from String. */</span><br /><span style="color: navy; font-weight: bold;">public class </span>RemoveDuplicateCharacters{ <br /><span style="color: navy; font-weight: bold;">public static void </span>main(String args[]) {<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Call removeDuplicates(String word) method ...."</span>);<br /> String[] testdata = {<span style="color: green; font-weight: bold;">"aabscs"</span>, <span style="color: green; font-weight: bold;">"abcd"</span>, <span style="color: green; font-weight: bold;">"aaaa"</span>, <span style="color: navy; font-weight: bold;">null</span>, <span style="color: green; font-weight: bold;">""</span>, <span style="color: green; font-weight: bold;">"aaabbb"</span>, <span style="color: green; font-weight: bold;">"abababa"</span>};<br /> <span style="color: navy; font-weight: bold;">for </span>(String input : testdata) {<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.printf(<span style="color: green; font-weight: bold;">"Input : %s Output: %s %n"</span>, input, <span style="font-style: italic;">removeDuplicates</span>(input));<br /> }<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"Calling removeDuplicatesFromString(String str)."</span>);<br /> <span style="color: navy; font-weight: bold;">for </span>(String input : testdata) {<br /> System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.printf(<span style="color: green; font-weight: bold;">"Input : %s Output: %s %n"</span>,<br /> input, <span style="font-style: italic;">removeDuplicatesFromString</span>(input));<br /><br /> }<br />}<br /> <span style="color: navy; font-weight: bold;">public static </span>String removeDuplicates(String word) {<br /> <span style="color: navy; font-weight: bold;">if </span>(word == <span style="color: navy; font-weight: bold;">null </span>|| word.length() &lt; <span style="color: blue;">2</span>) {<br /> <span style="color: navy; font-weight: bold;">return </span>word;<br /> }<br /> <span style="color: navy; font-weight: bold;">int </span>pos = <span style="color: blue;">1</span>;<br /> <span style="color: grey; font-style: italic;">// possible position of duplicate character </span><span style="color: grey; font-style: italic;"><br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">char</span>[] characters = word.toCharArray();<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i = <span style="color: blue;">1</span>; i &lt; word.length(); i++) {<br /> <span style="color: navy; font-weight: bold;">int </span>j; <span style="color: navy; font-weight: bold;">for </span>(j = <span style="color: blue;">0</span>; j &lt; pos; ++j) {<br /> <span style="color: navy; font-weight: bold;">if </span>(characters[i] == characters[j]) {<br /> <span style="color: navy; font-weight: bold;">break</span>;<br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">if </span>(j == pos) {<br /> characters[pos] = characters[i]; ++pos;<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> characters[pos] = <span style="color: blue;">0</span>; ++pos;<br /> }<br /> } <span style="color: navy; font-weight: bold;">return </span><span style="font-style: italic;">toUniqueString</span>(characters);<br /> } <br /><br /> <span style="color: grey; font-style: italic;">/* This solution assumes that given input String only contains </span><span style="color: grey; font-style: italic;"> <br />ASCII characters. You should ask this question to your Interviewer, </span><br /><span style="color: grey; font-style: italic;">if he says ASCII then this solution is Ok. This Algorithm</span><br /><span style="color: grey; font-style: italic;"> uses additional memory of constant size. */</span><br /><span style="color: grey; font-style: italic;"></span><br /><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public static </span>String removeDuplicatesFromString(String input) {<br /> <span style="color: navy; font-weight: bold;">if </span>(input == <span style="color: navy; font-weight: bold;">null </span>|| input.length()&lt; <span style="color: blue;">2</span>) {<br /> <span style="color: navy; font-weight: bold;">return </span>input;<br /> }<br /> <span style="color: navy; font-weight: bold;">boolean</span>[] ASCII = <span style="color: navy; font-weight: bold;">new boolean</span>[<span style="color: blue;">256</span>];<br /> <span style="color: navy; font-weight: bold;">char</span>[] characters = input.toCharArray();<br /> ASCII[input.charAt(<span style="color: blue;">0</span>)] = <span style="color: navy; font-weight: bold;">true</span>; <span style="color: navy; font-weight: bold;">int </span>dupIndex = <span style="color: blue;">1</span>;<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">int </span>i = <span style="color: blue;">1</span>; i&lt;input.length(); i++) {<br /> <span style="color: navy; font-weight: bold;">if </span>(!ASCII[input.charAt(i)]) {<br /> characters[dupIndex] = characters[i];<br /> ++dupIndex; ASCII[characters[i]] = <span style="color: navy; font-weight: bold;">true</span>;<br /> } <span style="color: navy; font-weight: bold;">else </span>{<br /> characters[dupIndex] = <span style="color: blue;">0</span>; ++dupIndex;<br /> }<br /> } <span style="color: navy; font-weight: bold;">return </span><span style="font-style: italic;">toUniqueString</span>(characters);<br /> }<br /><br /> <span style="color: grey; font-style: italic;">/* Utility method to convert Character array to String, <br />omitting </span><span style="color: grey; font-style: italic;">NUL character, ASCII value 0. */</span><span style="color: grey; font-style: italic;"><br /></span><span style="color: grey; font-style: italic;"> </span><span style="color: navy; font-weight: bold;">public static </span>String toUniqueString(<span style="color: navy; font-weight: bold;">char</span>[] letters) {<br /> StringBuilder sb = <span style="color: navy; font-weight: bold;">new </span>StringBuilder(letters.<span style="color: #660e7a; font-weight: bold;">length</span>);<br /> <span style="color: navy; font-weight: bold;">for </span>(<span style="color: navy; font-weight: bold;">char </span>c : letters) { <span style="color: navy; font-weight: bold;">if </span>(c != <span style="color: blue;">0</span>) { sb.append(c);<br /> }<br /> }<br /> <span style="color: navy; font-weight: bold;">return </span>sb.toString();<br /> }<br />}</span><span style="font-family: &quot;courier new&quot;; font-size: 9pt;"><br /></span></pre></div></pre></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-48641054529519413422017-05-09T20:08:00.000-07:002017-05-09T20:19:03.177-07:00Eclipse Tutorial, How to use eclipse java ?<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b style="letter-spacing: -1px;">What is a Eclipse ? -</b> <span style="letter-spacing: -1px;">&nbsp;Eclipse is a Java-Based open source&nbsp;integrated development environment (IDE) used in computer programming, and is the most widely&nbsp;used Java IDE. The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used as Java IDE.</span></span></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Download Eclipse -</b></span> <span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">You can download eclipse form below given url.</span></div><div style="text-align: left;"><span style="background-color: white; text-align: justify;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></span></div><div style="text-align: center;"><a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr" target="_blank">http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr</a></div><div style="text-align: left;"><span style="background-color: white; text-align: justify;"><br /></span></div><div style="text-align: justify;"><span style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Installation of Eclipse - </b>Download setup from above given url and double click on setup and select desired option accept terms and condition and install the eclipse.</span></span></div><div style="text-align: justify;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="background-color: white;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span></span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><a href="https://4.bp.blogspot.com/-3dq_Y74R79M/WPx5h9p2VHI/AAAAAAAAH9Q/JhzbZmtLyQMmFpsSEhyfrsv9uR12XRZJACLcB/s1600/eclipse.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://4.bp.blogspot.com/-3dq_Y74R79M/WPx5h9p2VHI/AAAAAAAAH9Q/JhzbZmtLyQMmFpsSEhyfrsv9uR12XRZJACLcB/s400/eclipse.PNG" width="386" /></a></span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><br /><div style="text-align: left;"><span style="background-color: white; text-align: justify;"><br /></span></div></div>&gt;</div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-8144437471300190613.post-52885266213797796402017-04-17T10:27:00.000-07:002017-05-09T00:40:21.828-07:00ArrayList and Vector in java collection or difference between arraylist and vector<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>ArrayList , Vector in Java- </b>We should choose list implemented class to store heterogeneous unique or duplicate elements in indexed order.&nbsp;To store elements in index order and to retrieve&nbsp;them randomly.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Specific Functionalities of&nbsp;these two classes-&nbsp;</b></span><br /><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">duplicate objects are allowed in indexed order.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">heterogeneous objects are allowed.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">insertion order is preserved.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">null insertion is possible, more than one null is allowed.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">initial capacity is 10, incremental capacity is double for Vector and ArrayList use below formula (currentCapacity*3/2+1).</span></li></ul><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>The main difference between these two classes is-</b></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Vector object is thread safe</b>&nbsp;it means synchronized object - so multiple threads cannot modify this object concurrently. it is best suitable in multi-threaded&nbsp;model application to get desired result. But in single thread model application it gives performance issue because for every operation this object should be locked and locked.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>ArrayList Object is not thread-safe </b>it means it is not synchronized object it is best suitable in&nbsp;</span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">multi-threaded</span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp;</span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp;model application, also in multi-threaded&nbsp;model application if you ensure there is not data corruption.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Array List Constructor-</b></span><br /><br /><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>ArrayList()- </b>&nbsp;</span><span style="background-color: white; font-family: &quot;verdana&quot; , &quot;geneva&quot; , &quot;tahoma&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-size: 15px; text-align: justify;">This constructor is used to create an empty list with an initial capacity sufficient to hold 10 elements.</span></li><li><b>ArrayList(Collelction&lt;?Extends E&gt; e)-</b>&nbsp;<span style="background-color: white; font-family: &quot;verdana&quot; , &quot;geneva&quot; , &quot;tahoma&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-size: 15px; text-align: justify;">This constructor is used to create a list containing the elements of the specified collection.</span></li><li><span style="background-color: white; text-align: justify;"><b style="font-family: Verdana, Geneva, Tahoma, Arial, Helvetica, sans-serif; font-size: 15px;">ArrayList(int initialCapacity)- </b><span style="font-family: &quot;verdana&quot; , &quot;geneva&quot; , &quot;tahoma&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif; font-size: 15px;">This</span><b style="font-family: Verdana, Geneva, Tahoma, Arial, Helvetica, sans-serif; font-size: 15px;">&nbsp;</b><span style="font-family: &quot;verdana&quot; , &quot;geneva&quot; , &quot;tahoma&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span style="font-size: 15px;">Constructor&nbsp;is used to create empty list with initial Capacity.</span></span></span></li></ul><br /><b style="font-family: arial, helvetica, sans-serif;">Method in Array List-</b><br /><br /><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>boolean add(E e)-</b>&nbsp;This method is used to add elements in add method.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>boolean add(int index, Element e)- </b>This method is used add element at specified index.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>boolean addAll(Collection&lt;? extends E&gt;c)- </b>This method is used to add element in specified collection.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>boolean addAll(int index, Collection&lt;? extends E&gt; c)- </b>This method insert all the element in the specified collection, at a specified position.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>void clear()- </b>This method will remove all the elements from the list.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>void clone()- </b>This method returns shallow copy of list element.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>boolean contains(Object o)- </b>This method return true if element is available int the list.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>void ensureCapactiy(int minCapacity)- </b>This method increase the capacity of Array List.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>E get(int index)- </b>This method returns element at specified index.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>boolean isEmpty()- </b>This method return null if list is empty.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>int lastIndexOf(Object o)- </b>This method return last occurrence&nbsp;of the specified element.in the list.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>E remove(int index)- </b>This method remove element at a specified index.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>int size()- </b>This method returns size of an array list.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>E set(int index, E element)- </b>This method replace elements at specified position in the list..</span></li></ul><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Array List Example-</b></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">import java.util.*;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class ListExample {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; public static void main(String... args) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;ArrayList al=new ArrayList();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;al.add(2);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;al.add(4);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;al.add(1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp;Iterator itr=al.iterator();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp;while(itr.hashNext()) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Elements are",+itr.next());</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp; &nbsp;}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp;}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}&nbsp;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><br /><div style="text-align: left;"><b style="font-family: arial, helvetica, sans-serif;">How add mehthod work in ArrayList-&nbsp;</b><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">So in the above example , we have created an ArrayList object arrobj . To add elements into the arrobj we called the add method on arrobj. After printing the arrobj , we get the desired result ,i.e , values are added to the arrobj.</span></div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">But the question is how add(Object) method adds the value in ArrayList. So lets find out :</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-79008571926997485692017-04-15T04:15:00.001-07:002017-05-09T00:40:21.842-07:00How to use InteliJ <div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Hi Guys in today blog i will teach you how to import project in InteliJ.</span><br /><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once you install InteliJ on your system click on InteliJ icon below given window will appear.</span></li></ul><div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-zL8Pa5IlC1Q/WPHxkLA2YBI/AAAAAAAAH5w/KP113pxxuFUrBJzGubrTOMWFcZ_7hkuYwCEw/s1600/101.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="272" src="https://1.bp.blogspot.com/-zL8Pa5IlC1Q/WPHxkLA2YBI/AAAAAAAAH5w/KP113pxxuFUrBJzGubrTOMWFcZ_7hkuYwCEw/s400/101.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: center;"></div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Click on Import Project and give path where you have created local repository using SourceTree.</span><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-XFYhC6lODP8/WPHxj9RrUZI/AAAAAAAAH6Y/_5Z4jlL1C50CsxmosD48mWs913TTxLIAwCEw/s1600/102.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://2.bp.blogspot.com/-XFYhC6lODP8/WPHxj9RrUZI/AAAAAAAAH6Y/_5Z4jlL1C50CsxmosD48mWs913TTxLIAwCEw/s400/102.png" width="313" /></a></div></li></ul><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In my case i have imported maven project you can select appropriate and click on next button see blow given screenshot. </span>&nbsp;</li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-IE89i0QELGk/WPHxj_veVLI/AAAAAAAAH6Y/YkiS2EXQg8EBx-X7uW7BTVQxZ0UsgDm4gCEw/s1600/103.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="392" src="https://4.bp.blogspot.com/-IE89i0QELGk/WPHxj_veVLI/AAAAAAAAH6Y/YkiS2EXQg8EBx-X7uW7BTVQxZ0UsgDm4gCEw/s400/103.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"></div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">One the next window click on Environment Setting and set a path for Maven Home Directory, Setting.xml file path and Local Directory Path then click on next button even in the next window click on next button.</span></li></ul><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-UiPRv5_V53s/WPHxkpFMDMI/AAAAAAAAH6Y/rahVItZD7LgQMygVsOhRdhYbrSh_77NAgCEw/s1600/105.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="392" src="https://2.bp.blogspot.com/-UiPRv5_V53s/WPHxkpFMDMI/AAAAAAAAH6Y/rahVItZD7LgQMygVsOhRdhYbrSh_77NAgCEw/s400/105.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"></div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In this step click on&nbsp;+ sign to add JDK path as given in the below screenshot project imported successfully.</span></li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-Z-PkzUIvXUQ/WPHxlrsGwAI/AAAAAAAAH6Y/7TROxaoZQhQeWxszr2Ye4uODLB2gk10eACEw/s1600/111.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="381" src="https://4.bp.blogspot.com/-Z-PkzUIvXUQ/WPHxlrsGwAI/AAAAAAAAH6Y/7TROxaoZQhQeWxszr2Ye4uODLB2gk10eACEw/s400/111.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"></div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once import completed in the menu bar Go to File &gt; Setting &gt; Version Control &gt; Git given git.exe path available on your system and click on Test once git.exe executed successfully you can use InteliJ.</span></li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-G_XGrFAFeDc/WPH-PtnTezI/AAAAAAAAH6s/b09hkobtAO4vbqlpFStQ0jHV9wSPAQ2qQCLcB/s1600/113.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="268" src="https://4.bp.blogspot.com/-G_XGrFAFeDc/WPH-PtnTezI/AAAAAAAAH6s/b09hkobtAO4vbqlpFStQ0jHV9wSPAQ2qQCLcB/s400/113.PNG" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: left;"></div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">If you have followed above given steps then you InteliJ will look like below given screenshot Terminal can be used to run the command in the Version Control you will get your local changes even you can pull the latest code from Repository &nbsp;To pull the latest changes Right Click &gt; Git &gt; Repository &gt; Pull.</span></li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-KR0iR1vM2w8/WPH_ySf_wdI/AAAAAAAAH64/C-QX1eKXEQUS8kBl_T0WRXpqMWrWz3--QCLcB/s1600/114.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="206" src="https://2.bp.blogspot.com/-KR0iR1vM2w8/WPH_ySf_wdI/AAAAAAAAH64/C-QX1eKXEQUS8kBl_T0WRXpqMWrWz3--QCLcB/s400/114.PNG" width="400" /></a></div>.<br /><br /><br /><div style="margin-left: 1em; margin-right: 1em; text-align: justify;"></div></div></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-22955479478858513232017-04-15T01:34:00.001-07:002017-05-09T00:40:21.814-07:00SourceTree Blog<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In Today blog i will teach you guys how to install and use SourceTree. Below given steps you have to follow to setup project structure.</span><br /><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once you double clicked on Source Tree Setup one prompt window will appear as given below in the screenshot accept the licence agreement and click on continue button.</span></li></ul></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-yc_2bQ4hJFU/WPHQpjnWWYI/AAAAAAAAH4c/rrxlBkCQsqktHyY_nv7_9x3WA3x0iyXZgCEw/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="232" src="https://4.bp.blogspot.com/-yc_2bQ4hJFU/WPHQpjnWWYI/AAAAAAAAH4c/rrxlBkCQsqktHyY_nv7_9x3WA3x0iyXZgCEw/s400/1.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><div><br /></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><ul><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In the next window it will ask for Atlassian account if you don't have account If you are new user click on Go to My Atlassian and create an account.</span><div class="separator" style="clear: both; text-align: center;"></div></li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-oqXpX_TBRyY/WPHSCuHk4lI/AAAAAAAAH5c/lCk5S_9fQvUoMfntg0MAE69rdu7NHeY9gCEw/s1600/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://3.bp.blogspot.com/-oqXpX_TBRyY/WPHSCuHk4lI/AAAAAAAAH5c/lCk5S_9fQvUoMfntg0MAE69rdu7NHeY9gCEw/s400/3.png" width="235" /></a></div><div><br /></div></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">If you have account click on 'Use an existing account' to login.</span><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/--tf5N3bJBTI/WPHQppShFeI/AAAAAAAAH4c/nEARvLZoLXIcyf6DaRdv3jE2iJ_4VkG5gCEw/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="232" src="https://1.bp.blogspot.com/--tf5N3bJBTI/WPHQppShFeI/AAAAAAAAH4c/nEARvLZoLXIcyf6DaRdv3jE2iJ_4VkG5gCEw/s400/2.png" width="400" /></a></div></li></ul><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once Registration complete/ Login successful Registration Completed window will appear then click on Continue button to completed installation of SourceTree.&nbsp;</span><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-yD2RPfdmCDU/WPHSCy1tLfI/AAAAAAAAH5c/uAuWMWj0_ssa_52a8ZvYesV4H9gr1M6UQCEw/s1600/4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="238" src="https://2.bp.blogspot.com/-yD2RPfdmCDU/WPHSCy1tLfI/AAAAAAAAH5c/uAuWMWj0_ssa_52a8ZvYesV4H9gr1M6UQCEw/s400/4.png" width="400" /></a></div>&nbsp;</li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once installation complete click on Skip Setup as given in the below screenshot.</span><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-cJDUEB1JRQk/WPHSDX7JxgI/AAAAAAAAH5c/WGLkNTmcoyAiky5CQ3KUrqIfNFaiICVyACEw/s1600/6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="232" src="https://4.bp.blogspot.com/-cJDUEB1JRQk/WPHSDX7JxgI/AAAAAAAAH5c/WGLkNTmcoyAiky5CQ3KUrqIfNFaiICVyACEw/s400/6.png" width="400" /></a></div></li></ul></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Below given window will appear.</span><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-XJT-OVKbu0c/WPHSDolLkbI/AAAAAAAAH5c/RwFnLjHvMXQIRjNDft_5_XVUtKyXthPOACEw/s1600/7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="157" src="https://1.bp.blogspot.com/-XJT-OVKbu0c/WPHSDolLkbI/AAAAAAAAH5c/RwFnLjHvMXQIRjNDft_5_XVUtKyXthPOACEw/s400/7.png" width="400" /></a></div></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Before adding project into SourceTree Go to Tools&gt; Options- select below given check box as given in the below given screenshot.</span><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-KptFYyjABz4/WPHSD7YXrGI/AAAAAAAAH5c/Fz4iggZLK9wq2CXACZhvU9ep8THx8WpWACEw/s1600/8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="255" src="https://4.bp.blogspot.com/-KptFYyjABz4/WPHSD7YXrGI/AAAAAAAAH5c/Fz4iggZLK9wq2CXACZhvU9ep8THx8WpWACEw/s400/8.png" width="400" /></a></div>&nbsp;</li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">To create a repository click on Clone/New button one prompt window will appear in the Source Path/ URL given your project url and in the Destination Path given you repository location then click on clone button.&nbsp;</span><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-rOzMgtYKzpo/WPHSD5qy0zI/AAAAAAAAH5c/5QctrXPbapAM0r-8yjqRqMPf-GwsDoJ8wCEw/s1600/9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="218" src="https://4.bp.blogspot.com/-rOzMgtYKzpo/WPHSD5qy0zI/AAAAAAAAH5c/5QctrXPbapAM0r-8yjqRqMPf-GwsDoJ8wCEw/s400/9.png" width="400" /></a></div></li></ul></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once all the files are pulled successfully below given screenshot will appear then click on close button.</span><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-UXQvanFIbkY/WPHSCAmoamI/AAAAAAAAH5c/gwaB7BdBe-8gqZml6VmNdJ-rjQGLIBstQCEw/s1600/12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="213" src="https://2.bp.blogspot.com/-UXQvanFIbkY/WPHSCAmoamI/AAAAAAAAH5c/gwaB7BdBe-8gqZml6VmNdJ-rjQGLIBstQCEw/s400/12.png" width="400" /></a></div>&nbsp;</li><li><div class="separator" style="clear: both; text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Once you clicked on close button your local repository created successfully and below given screenshot will appear.</span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-p9DfwXIrHe0/WPHSCLgEd8I/AAAAAAAAH5c/3Z6WvBSJW8kerDFxNkpwEPAF79UTq1IOgCEw/s1600/14.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="213" src="https://3.bp.blogspot.com/-p9DfwXIrHe0/WPHSCLgEd8I/AAAAAAAAH5c/3Z6WvBSJW8kerDFxNkpwEPAF79UTq1IOgCEw/s400/14.PNG" width="400" /></a></div></li></ul></div></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div><br /></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0India20.593684 78.962880000000041-8.6044825 37.654286000000042 49.791850499999995 120.27147400000004tag:blogger.com,1999:blog-8144437471300190613.post-80161978176056205512017-02-25T22:03:00.000-08:002017-05-09T00:40:21.850-07:00How to Override equals and hashcode Method in Java?<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Hello Everyone, In this tutorial we are going to learn easy way to override equals and hashCode method. Before implementation of equals and hashCode method let me tell you the difference between&nbsp;'==' operator and equals() method.</span><br /><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>'==' Operator -&nbsp;</b></span></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">It always compare object with refernce.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we can't compare incompatible objects, compiler throws compilation error..</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we can use it also for computing primitive values and two nul's directly.</span></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>equals() Method -</b></span></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">It compare object either with reference or with state based on its implementation.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In object class it compare object with refence.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In subclass it compare object with reference.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we can compare incompatible objects using equals it return "false".</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we can't use it for compari<div class="separator" style="clear: both; text-align: center;"></div>ng primitive values and two nulls directly it leads compilation error.</span></li></ul></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Now we will understand these two method via example for this i have created one <span style="color: blue;">M</span></span><span style="color: blue; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ain </span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">class and one custom object which we call it as </span><span style="color: blue; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Point </span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we will make Point class as Final to make a package private first we will understand why we have to override equals and hashCode method what are the disadvantage if we don't override equals and hashCode method.</span></div><div><div><br /><span style="color: #3d85c6;">//Main.java</span></div><div><span style="background-color: #444444; color: #3d85c6; font-family: Arial, Helvetica, sans-serif;"><br /></span></div><div><pre><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">package channelsUpstream.requests.npp;<br />import java.util.*;<br />/**<br /> * Created by namdeva on 22/04/2017.<br /> */<br />public class Main {<br /> public static void main(String... args) {<br /><br /> List&lt;Point&gt; p = new ArrayList&lt;Point&gt;();<br /><br /> p.add(new Point(1,1));<br /> p.add(new Point(2,2));<br /><br /> System.out.println("Size " + p.size());<br /> Point p1 = new Point(1,1);<br /> if (p.contains(p1)) {<br /> System.out.println("true");<br /> } else {<br /> System.out.println("false");<br /> }<br /> }<br />}</span></pre><pre><br /></pre><pre><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">//Point.java</span></pre></div><div><span style="background-color: white; color: #3d85c6; font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">package channelsUpstream.requests.npp;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">import org.apache.commons.lang.builder.EqualsBuilder;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">import org.apache.commons.lang.builder.HashCodeBuilder;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">/**</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp;* Created by namdeva on 22/04/2017.</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp;*/</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">final class Point {</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; private final double x;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; private final double y;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; //will make public constructor.</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; public Point(double x, double y) {</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">&nbsp; &nbsp;&nbsp;</span><span style="color: #3d85c6; font-family: Arial, Helvetica, sans-serif;">}</span><br /><span style="color: blue; font-family: Arial, Helvetica, sans-serif;"><br /></span></div><div><div><span style="font-family: Arial, Helvetica, sans-serif;">Output -</span><br /><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-jGgSOYputQg/WPstIxm1j6I/AAAAAAAAH8w/KqTFlS9ymrUjIInM5aBh_EJ7WdbjzjkkgCEw/s1600/HashCodeEqalsMethodImplementation.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="85" src="https://2.bp.blogspot.com/-jGgSOYputQg/WPstIxm1j6I/AAAAAAAAH8w/KqTFlS9ymrUjIInM5aBh_EJ7WdbjzjkkgCEw/s400/HashCodeEqalsMethodImplementation.PNG" width="400" /></a></div><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">//Main.java</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">package channelsUpstream.requests.npp;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">import java.util.*;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">/**</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp;* Created by namdeva on 22/04/2017.</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp;*/</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">public class Main {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; public static void main(String... args) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; List&lt;Point&gt; p = new ArrayList&lt;Point&gt;();</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; p.add(new Point(1,1));</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; p.add(new Point(2,2));</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Size " + p.size());</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; Point p1 = new Point(1,1);</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; if (p.contains(p1)) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("true");</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; } else {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("false");</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"></span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">}</span><br /><span style="font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">//Point.java</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">package channelsUpstream.requests.npp;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">import org.apache.commons.lang.builder.EqualsBuilder;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">import org.apache.commons.lang.builder.HashCodeBuilder;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">/**</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp;* Created by namdeva on 22/04/2017.</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp;*/</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">final class Point {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; private final double x;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; private final double y;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; //will make public constructor.</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; public Point(double x, double y) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; @Override</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; public int hashCode() {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; return new HashCodeBuilder().append(x).append(y).toHashCode();</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; @Override</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; public boolean equals(Object obj) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; //If object is null return false</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; if(obj==null) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; // If both object are same return true</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; if (obj==this) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; if (obj.getClass()!=this.getClass()) {</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; //Caste into point vaiable</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; Point p2=(Point)obj;</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; &nbsp; &nbsp; return new EqualsBuilder().append(x,p2.x).append(y,p2.y).isEquals();</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">&nbsp; &nbsp; }</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"></span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">}</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;">Output -</span><br /><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-eRHGOWM6LD4/WPszztVisxI/AAAAAAAAH88/F6Vs-vWwdvg1Soj88r5BkDgX4DjAp839QCLcB/s1600/HashCodeEqalsMethodImplementation1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="86" src="https://3.bp.blogspot.com/-eRHGOWM6LD4/WPszztVisxI/AAAAAAAAH88/F6Vs-vWwdvg1Soj88r5BkDgX4DjAp839QCLcB/s400/HashCodeEqalsMethodImplementation1.PNG" width="400" /></a></div><span style="color: #3d85c6; font-family: arial, helvetica, sans-serif;"><br /></span></div></div></div></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-8144437471300190613.post-45265665604599349602017-02-12T08:07:00.000-08:002017-05-15T21:19:41.978-07:00Java Collection Framework and Gernerics Part-I<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Java Collection- </b>Collection is a java object which is used to group homogeneous and heterogeneous, duplicate and unique objects without size limitation carrying multiple object at a time from one application to another application. java.util package contains several classes to group homogeneous and heterogeneous object. These classes are usually called collection framework classes.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Why collection classes are given when we have Object[] to group heterogeneous object ?- </b>All array object has two problems</span><br /><br /><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">It allows us to store only same type of elements.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">it is fixed in size.</span>&nbsp;</li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">The first problem can be solved using java.lang Object[] array we can collect all types of objects. But the second problem can't be solved automatically.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>why the name collection framework?- </b>Framework is a semi finished reusable&nbsp;application&nbsp;which provides some common low level services for solving reoccurring problems and that can be customized according to our requirement.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Example- </b>Computer is a framework, it can be used by all people according to their requirement.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Types of collection classes-</b>&nbsp; There are 2 types of collection classes.</span></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">To collect objects in array format.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">To collect object in (key,value) pair format.</span></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Legacy collection framework classes- </b>In java 1.0 version SUN introduced&nbsp;2 classes.</span></div></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Vector- </b>It stores object in array format.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Hastable- </b>It stores object in (key,value) pair format.</span></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Drawback of Vector and Hashtable&nbsp;classes- </b>These two classed were created as thread safe classes, means all the methods in these two classes are declared as synchronized. Hence for every method call on these two objects either for adding or removing elements. JVM locks and unlock these two objects. So in Single thread model application t leads to performance issue. In Single thread model application execution is sequential so there is no data corruption, hence no need locking. To solve this performance issue problem in Java 1.2 SUN introduced non-thread safe classes as a alternative to above two classes.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Collection Hierarchy- </b>Collection hierarchy classes are divided into three categories - Set, List and Queue.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Set </b>is collection which doesn't allow duplicate values.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>List </b>is a duplicate collection. List is super interface for all duplicate collection classes</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>SortedSet </b>is a sub interface of set that store elements in sorted order either in ascending or descending order.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>NavigableSet </b>is a sub interface of SortedSet. it is added in java 1.6 to add more navigation methods to sorted set.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Queue </b>is root interface of all types of queues. queues provide additional insertion, extraction and inspection operations.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Collection Interface classes hierarchy- </b>In the below hierarchy, Vector and Stack classes are available since Java 1.0, LinkedHashSet class is available since Java 1.4, Queue is available since Java 1.5, and NavigableSet is available sing Java1.6, and all other remaining classes and interface are available sing Java 1.2 version.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-bmz6QUuHugU/WKCEguU9OMI/AAAAAAAAH0o/tQ6T_VP0yhgeKCBIZFjajTPxgrQ-Z0-UACLcB/s1600/List.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="395" src="https://1.bp.blogspot.com/-bmz6QUuHugU/WKCEguU9OMI/AAAAAAAAH0o/tQ6T_VP0yhgeKCBIZFjajTPxgrQ-Z0-UACLcB/s640/List.PNG" width="640" /></a></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Map Hierarchy- </b>Map hierarchy classes are used to collect elements in (Key,Value) pair format. In a map, keys should be unique and value can be duplicated. Each (key,value) is called an entry. In map by default entries are not sorted.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Sorted Map </b>is the sub interface of Map. It sorts entries based on keys natural sorting order.</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-lwnNW25KWi0/WKB5DmVnuWI/AAAAAAAAH0c/P5FvYkPfpp4vzcuqKg_lTd6puZuTlukxgCEw/s1600/map1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="550" src="https://3.bp.blogspot.com/-lwnNW25KWi0/WKB5DmVnuWI/AAAAAAAAH0c/P5FvYkPfpp4vzcuqKg_lTd6puZuTlukxgCEw/s640/map1.PNG" width="640" /></a></div><div><br /></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-73892822700845051412017-02-11T11:45:00.003-08:002017-05-09T00:40:21.837-07:00Resolution of Singleton Design Pattern<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">This is the continuation of my previous blog, Reflection, Serialization/Deserialization, clone, multi-threaded access all of them really help to create new instance and violate basis singleton concept of having one and only one instance.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">in today blog we will fix all these issue.</span><br /><div style="text-align: left;"></div><ul style="text-align: left;"><li><b style="font-family: Arial, Helvetica, sans-serif;">Reflection - &nbsp;</b><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we have singleton definition in which we have private constructor and public static getInstance() method and then we have test case which create two instance then we create instance using reflection in the below program. Reflection work in singleton because we have set the constructor as accessible and and invoke it. So we will go into constructor to fix this issue we are going to check in constructor if instance is already created or not if it is not null then we will throw exception.</span></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Example-</b></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b><br /></b></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton {</span></div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><div>private static MySingleton instance=new MySingleton();</div><div>private MySingleton() {</div><div>if(instance!= null) {</div><div><span class="Apple-tab-span" style="white-space: pre;"> </span>throw new RuntimeException("Can't create object");</div><div>}</div><div>//proceed with object creation</div><div>System.out.println(“User can’t create object using new keyword”);</div><div>}</div><div>public static MySingleton getInstance() {</div><div>return instance;</div><div>}</div><div>}</div><div><br /></div><div>//How to use Singleton class</div><div><br /></div><div>Class TestClass {</div><div>public static void main(String[] args) throws Exception{</div><div>MySingleton ms=new MySingleton(); &nbsp; &nbsp;// Not allowed</div><div>MySingleton ms1=MySingleton.getInstance();</div><div>MySingleton ms2=MySingleton.getInstance();</div><div>print(“MySingleton”+ms1);</div><div>print(“MySingleton”+ms2);</div><div>//Reflection</div><div>Class cls=Class.forName(“singleton.MySingleton”); &nbsp; &nbsp; &nbsp;//This way i am going to load a class</div><div>//Using this cls i am going to get default constructor.</div><div>Constructor&lt;MySingleton&gt; ctr=cls.getDeclaredConstructor(); &nbsp; &nbsp; // I got the default constructor now it is private i can’t invoke it, so i am going to use setAccessible api. By using this i am changing constructor to public constructor.</div><div><br /></div><div>ctr.setAccessible(true);</div><div>MySingleton ms3=ctr.newInstance();</div><div>print(“MySingleton ms3”,ms3);</div><div>}</div><div>static void print(String name, MySingleton object) {</div><div>System.out.println(String.format(“Object : Hashcode”, name, object.hashcode()));</div><div>}</div><div>}</div><div><br /></div><div><br /></div><div><ul style="text-align: left;"><li><b>Serialization/Deserialization- &nbsp;</b>To fix this issue related to serialization/deserialzation in singleton class we have to implement read resolve method in singleton class when deserialization happens it is guarantee java will call read resolve method before deserializing the object.</li></ul><div><b>Example-</b></div></div><div><b><br /></b></div></span></div><div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton implements Serializable {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private static MySingleton instance=new MySingleton();</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private MySingleton() {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">System.out.println(“User can’t create object using new keyword”);</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static MySingleton getInstance() {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return instance;</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private Object readResolve() throws ObjectStreamException {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Inside Read Resolve method");</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>return instance;</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//How to use Singleton class</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class TestClass {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static void main(String[] args) {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms=new MySingleton(); // Not allowed</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms1=MySingleton.getInstance();</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms2=MySingleton.getInstance();</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms1);</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms2);</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//Serialization example</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream(“xyz.ser”));</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">oos.writeObject(ms1);</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ObjectInputStream ois=new ObjectInputStream(new FileInputStream(“xyz.ser”));</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms3=(MySingleton)ois.readObject();</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms3);</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">static void print(String name, MySingleton object) {</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">System.out.println(String.format(“Object : Hashcode”, name, object.hashcode()));</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Clone- </b>&nbsp;Singleton object can be duplicated using clone method &nbsp;to prevent that the easiest way to prevent is throw an exception from a clone method&nbsp;and not allow cloning, we have to throw CloneNotSupportedException from clone api.</span></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Example-</b></span></div></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span></div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><div>public Class MySingleton implements Cloneable {</div><div>private static MySingleton instance=new MySingleton();</div><div>private MySingleton() {</div><div>System.out.println(“User can’t create object using new keyword”);</div><div>}</div><div><br /></div><div>@Override</div><div>protected object clone() throws CloneNotSupportedException {</div><div>return super.clone();</div><div>}</div><div><br /></div><div>public static MySingleton getInstance() {</div><div>return instance;</div><div>}</div><div>}</div><div>//How to use Singleton class</div><div><br /></div><div>Class TestClass {</div><div>public static void main(String[] args) {</div><div>MySingleton ms=new MySingleton(); // Not allowed</div><div>MySingleton ms1=MySingleton.getInstance();</div><div>MySingleton ms2=MySingleton.getInstance();</div><div>print(“MySingleton”+ms1);</div><div>print(“MySingleton”+ms2);</div><div><br /></div><div>//Cloneable example</div><div><br /></div><div>MySingleton ms3=(MySingleton) ms2.clone();</div><div>print(“MySingleton”,ms3);</div><div>}</div><div>static void print(String name, MySingleton object) {</div><div>System.out.println(String.format(“Object : Hashcode”, name, object.hashcode()));</div><div>}</div><div>}</div></span></div><div><ul style="text-align: left;"><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Multi-threaded-&nbsp;</b>In our previous blog on singleton&nbsp;multi-threaded example multiple&nbsp;thread is able to create two instances. To fix this issue we have to implement double locking mechanism in this mechanism if one thread i accessing getInstance() method other thread is not able to access getInstance() method so we have used synchronized block. Suppose one more thing we have to notice one thread enters in getInstance() method and it saw instance is null then it tries to lock in singleton class by the time it got lock it is possible that some another thread might initialize your instance so it is very good approach to check instance is null or not even after getting a lock this is called double locking mechanism.</span></li></ul><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><b>Example-</b></span></div></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></div><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span></div><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><div>public Class MySingleton {</div><div>//private static MySingleton instance=new MySingleton(); &nbsp;//eagerly created instance.</div><div>private static MySingleton instance=null;</div><div>private MySingleton() {</div><div>System.out.println(“User can’t create object using new keyword”);</div><div>}</div><div>public static MySingleton getInstance() {</div><div><br /></div><div>//Double check locking mechanism</div><div>if(instance==null) {</div><div>synchronized(Singleton.class) {</div><div>if(instance==null) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>instance=new MySingleton();</div><div>}</div><div>}</div><div>return instance;</div><div>}</div><div>}</div><div><br /></div><div>//How to use Singleton class</div><div><br /></div><div>Class TestClass {</div><div>public static void main(String[] args) {</div><div>MySingleton ms=new MySingleton(); // Not allowed</div><div>MySingleton ms1=MySingleton.getInstance();</div><div>MySingleton ms2=MySingleton.getInstance();</div><div>print(“MySingleton”+ms1);</div><div>print(“MySingleton”+ms2);</div><div>MySingleton ms3=MySingleton.getInstance();</div><div>print(“MySingleton”,ms3);</div><div>}</div><div>static void print(String name, MySingleton object) {</div><div>System.out.println(String.format(“Object : Hashcode”, name, object.hashcode()));</div><div>}</div><div>}</div></span></div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-8144437471300190613.post-48282717956192691122017-02-02T10:41:00.000-08:002017-05-09T00:40:21.822-07:00Understanding of Sigleton Design Pattern<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Singleton Design Pattern ensure that class has only one instance and provide global access to it that means across your application you are going to have only one instance of the class and whenever you need it you are not going to create it using new Keyword.</span><br /><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span></strong><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Implementation of Singleton Class –</span></strong><br /><ul><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Make Sure there is only one instance.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Make consutructor as private and let the class manage its instance.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Provide global point of access.</span></li></ul><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Example –</span></strong><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private static MySingleton instance=new MySingleton();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private MySingleton() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(“User</span> can’t create object using new keyword”);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static MySingleton getInstance() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return instance;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//How to use Singleton class</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class TestClass {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static void main(String[] args) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms=new MySingleton(); // Not allowed</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms1=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms2=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms2);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">static void print(String name, MySingleton object) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(String.format(“Object</span> : Hashcode”, name, object.hashcode()));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">if i will run this TestClass then constructor will be called because class is initialized during class loading then i got two instances ms1 and ms2 and printed them and we will get same Hascode but this singleton class have some issue which we are going to discuss.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In this singleton class we are violating the requirement of singleton class using the following point.</span><br /><ol><li><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Reflection</span></strong></li><li><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Seriallization/deserialization</span></strong></li><li><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">clone</span></strong></li><li><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">multi-threaded access</span></strong></li></ol><ul><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Reflection</strong>– Reflection allows you to construct singleton a object without using new instance or any other api.</span></li></ul><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Example</strong> <strong>–</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private static MySingleton instance=new MySingleton();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private MySingleton() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(“User</span> can’t create object using new keyword”);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static MySingleton getInstance() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return instance;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//How to use Singleton class</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class TestClass {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static void main(String[] args) throws Exception{</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms=new MySingleton(); // Not allowed</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms1=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms2=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms2);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//Reflection</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class cls=Class.forName(“singleton.MySingleton”); //This way i am going to load a class</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//Using this cls i am going to get default constructor.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Constructor&lt;MySingleton&gt; ctr=cls.getDeclaredConstructor(); // I got the default constructor now it is private i can’t invoke it, so i am going to use setAccessible api. By using this i am changing constructor to public constructor.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ctr.setAccessible(true);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms3=ctr.newInstance();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton ms3”,ms3);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">static void print(String name, MySingleton object) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(String.format(“Object</span> : Hashcode”, name, object.hashcode()));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">if we run TestClass hascode for ms3 will be different that means we have successfully violated the singleton requirement.</span><br /><ul><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Serialization/deserialization –&nbsp;</strong>Serialization is a process of writing object data into a stream and late you can read it back and construct your object back by deserialization. Now Let’s see the example.</span></li></ul><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Example –</span></strong><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton implements Serializable {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private static MySingleton instance=new MySingleton();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private MySingleton() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(“User</span> can’t create object using new keyword”);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static MySingleton getInstance() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return instance;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//How to use Singleton class</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class TestClass {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static void main(String[] args) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms=new MySingleton(); // Not allowed</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms1=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms2=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms2);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//Serialization example</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream(“<span class="skimlinks-unlinked">xyz.ser</span>”));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">oos.writeObject(ms1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">ObjectInputStream ois=new ObjectInputStream(new FileInputStream(“<span class="skimlinks-unlinked">xyz.ser</span>”));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms3=(MySingleton)ois.readObject();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms3);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">static void print(String name, MySingleton object) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(String.format(“Object</span> : Hashcode”, name, object.hashcode()));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">In this TestClass i am serializing ms1 object and then i have deserialized it and i am trying to print ms3 object hashcode and the hascode is different.</span><br /><span style="font-family: &quot;arial&quot;;"><br /></span><ul><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Clone –&nbsp;</strong>Cloning is the another way of violating the singleton definition. For this i am going to implement Cloneable interface. whenever i am implementing cloneable interface i am able to override clone() method which by default calls <span class="skimlinks-unlinked">super.clone</span>() api. Now i am trying to clone ms2 object (refer the TestClass). if i will print ms3 the hashcode will be different which violate singleton rule.</span></li></ul><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Example –</span></strong><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton implements Cloneable {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private static MySingleton instance=new MySingleton();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private MySingleton() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(“User</span> can’t create object using new keyword”);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">@Override</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">protected object clone() throws CloneNotSupportedException {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return <span class="skimlinks-unlinked">super.clone</span>();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static MySingleton getInstance() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return instance;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//How to use Singleton class</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class TestClass {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static void main(String[] args) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms=new MySingleton(); // Not allowed</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms1=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms2=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms2);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//Cloneable example</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms3=(MySingleton) ms2.clone();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”,ms3);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">static void print(String name, MySingleton object) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(String.format(“Object</span> : Hashcode”, name, object.hashcode()));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><ul><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Multi-threaded access –&nbsp;</strong>This is another way of violate the singleton rule. This is most important scenario because when multiple thread are trying to create your singleton. So in our example we have eagerly created the instance here but this is always not a very good practice sometimes you don’t need instance until the getInstance() is called you don’t really need to construct a MySingelton object. In this case you would delay the creation of MySingleton until it is mandatory. Other advantage to creating an instance of MySingleton is suppose at the time of class loading instance creation is failed so we don’t have chance again to recreate that instance until you reload the class. That is why we have declared static instance variable as null and creating instance lazily in the getInstance() method. So in the getInstance() method we will check if instance is null we are going to create instance of our singleton class.</span></li></ul><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Example –</span></strong><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">package singleton;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public Class MySingleton {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//private static MySingleton instance=new MySingleton(); &nbsp;//eagerly created instance.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private static MySingleton instance=null;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">private MySingleton() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(“User</span> can’t create object using new keyword”);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static MySingleton getInstance() {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">if(instance==null) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">instance=new MySingleton();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">return instance;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">//How to use Singleton class</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class TestClass {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">public static void main(String[] args) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms=new MySingleton(); // Not allowed</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms1=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms2=<strong>MySingleton.getInstance();</strong></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms1);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”+ms2);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">MySingleton ms3=MySingleton.getInstance();</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">print(“MySingleton”,ms3);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">static void print(String name, MySingleton object) {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><span class="skimlinks-unlinked">System.out.println(String.format(“Object</span> : Hashcode”, name, object.hashcode()));</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-11931450166221573112017-02-01T09:52:00.003-08:002017-05-09T00:40:21.777-07:00What is Java.<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Java programming language was developed by Sun Microsystems which was initiated by James Gosling and released in 1995. Originally known as <b>oak</b>, &nbsp;The goal of the Java programming language was to make it so that a developer who programmed on one computer could run his/her code on another machine.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Java is an Object-Oriented, Platform Independent,&nbsp;Simple, Secure, Portable, Multithreaded and Dynamic&nbsp;&nbsp;programming language.</span><br /><ul><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Object-Oriented</strong> – In Java everything is object.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Platerform Independent</strong> – Java is plateform independent or we can machine independent, Once the byte is generated, it will be executeable in any machine.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Simple</strong> – Java is design to be easy to learn. Once you understand the oop’s concept you can start programming in java.</span></li><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Secure</strong> -With Java’s secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.</span></li><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Portable</strong> – Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.</span></li><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Multithreaded</strong> – With Java’s multithreaded feature it is possible to write programs that can perform many tasks simultaneously.</span></li><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><strong>Dynamic</strong> -Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.</span></li></ul></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-18866714093528088592017-01-31T20:54:00.001-08:002017-05-09T00:40:21.781-07:00About System.out.println();<div dir="ltr" style="text-align: left;" trbidi="on"><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class System {</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static printStream out;</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ……….</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">System.out.println(“Hello”);</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Here System is a class present is java.lang package, out is a static variable present in System class of type PrintStream and println is a method present in PrintStream class.</span></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-91830699573361316292017-01-31T20:48:00.001-08:002017-05-09T00:40:21.858-07:00About public static void main(String [] args)<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Let’s take a simple example first.</span><br /><ul><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Class Test {</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">}</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span></ul><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Above code compile fine even-though we are not writing main method, if i run this code immediately at run time we will get exception RE: NoSuchMethodError :main, weather class contains main method or not or main method declared properly or not these things are not checked at compile time at run time JVM is responsible to check these things.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">At run time JVM always search for main() method and the prototype of main method which is required by the jvm is- public static void main(String[] args).</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">you have question in your mind that why JVM always search for this main method only because inside a JVM prototype of main method is define the same way, definitely you can change the name of main() method to abcmain() but customized name of JVM main() is required.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Now next question in your mind is why main() method is public ?- so the answer is- who is going to call main method – definitely JVM. To call by JVM from anywhere main method should be public.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Next is why the main method is static ? – main() method is class level method it is no where related to object , without existing object also JVM has to call this method &amp; main() method no where related to any object.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Next is why the main method return type is void ? – main() method won’t return anything to JVM that is why return type is void.</span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">String[] args- these are the command line arguments.</span><br /><span style="font-family: &quot;arial&quot;;"><br /></span><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Let’s perform some operation on main() method-</span></strong><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">Suppose if you have removed static keyword in the main() method or you have changed return type of main() method and main() method name is changed then compile time you won’t get any error but at run time you will get exception RE: NoSuchMethodError : main.</span><br /><br /><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><br /><strong><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">But some changes are acceptable in the main() method-</span></strong><br /><ul><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">The order of modifier is not impotent.</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">We can declare String[] in any acceptable form like. main(String &nbsp;[]args), main(String args[]).</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">main(String[] args) here args is the name of array, instead of ‘args’ we can take any valid java identifier.</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">we can replace string[] with var arg parameter main(String… args).</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span></ul><div style="text-align: center;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">main(String[] args)=main(String.. args)</span></div><ul><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">you can declare main() method with final modifier.</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">you can declare main() method with synchronized keyword.</span></li><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;"></span><li style="text-align: left;"><span style="font-family: &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;">you can declare main() method with strictfp modifier.</span></li></ul></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0tag:blogger.com,1999:blog-8144437471300190613.post-19799455572084804032017-01-30T03:20:00.002-08:002017-05-09T00:40:21.811-07:00Java fundamental concept<div dir="ltr" style="text-align: left;" trbidi="on"><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">Java supports the following fundamental concepts −</div><ul class="list" style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li>Classes</li><li>Object</li><li>Inheritance</li><li>Polymorphism</li><li>Encapsulation</li><li>Abstraction</li></ul><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Classes</strong>&nbsp;- It is bluepring or template of an object.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Ex</strong>&nbsp;-</div><ol style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li><span class="kwd">public</span>&nbsp;<span class="kwd">class</span>&nbsp;Car</li><li><span class="pun">{</span></li><li><span class="typ">String</span><span class="pln">&nbsp;Gear</span><span class="pun">;</span></li><li><span class="typ">String</span><span class="pln">&nbsp;color</span><span class="pun">;</span></li><li>static String Company="Tata";</li><li><span class="kwd">void</span><span class="pln">&nbsp;forward</span><span class="pun">()</span></li><li><span class="pun">{</span></li><li>int test;</li><li><span class="pun">}</span></li><li><span class="kwd">void</span><span class="pln">&nbsp;reverse</span><span class="pun">()</span></li><li><span class="pun">{</span></li><li></li><li><span class="pun">}</span></li><li><span class="kwd">void</span><span class="pln">&nbsp;neutral&nbsp;</span><span class="pun">()</span></li><li><span class="pun">{</span></li><li></li><li><span class="pun">}</span></li><li><span class="pun">}</span></li></ol><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">A class can contain any of the following variable types.</div><ul style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li><b>Local variables</b>&nbsp;− Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Here test variable inside forward method is local variable.</li><li><b>Instance variables</b>&nbsp;− Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Here Gear, Color are instance variable.</li><li><b>Class variables</b>&nbsp;− Class variables are variables declared within a class, outside any method, with the static keyword. Here Company is the Class variable.</li></ul><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Object</strong>&nbsp;- It is encapsulated form of static and non-static method and variable.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Inheritance</strong>&nbsp;- Inheritace is the process of acessing the properties of parent class in our subclass is known as Inheritance.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">To implement inheritance we have to use extends and implements keyword.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Ex -</strong></div><ul style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li>class Super</li><li>{<br />......</li><li>}</li><li>class Sub extends Super</li><li>{<br />......</li><li>}</li></ul><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Polymorphism-&nbsp;</strong>More than one method with same name but different signature defined in the same class or with same signature defined in the different class is called as polymorphism.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">Following concept demonstrate different types of polymorphism.</div><ul style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li>Method Overloading</li><li>Method Overriding</li></ul><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Method Overloading-&nbsp;</strong>More than one method with same name and with same signature defined in the same class is called as Method Overloading.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Ex-</strong></div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">class A&nbsp;{<br />void demo (int a)<br />{<br />System.out.println ("a: " + a);<br />}<br />void demo (int a, int b)<br />{<br />System.out.println ("a and b: " + a + "," + b);<br />}<br />double demo(double a) {<br />System.out.println("double a: " + a);<br />return a*a;<br />}<br />}<br />class MethodOverloading<br />{<br />public static void main (String args [])<br />{<br />A&nbsp;Obj = new A();<br />double result;<br />Obj .demo(10);<br />Obj .demo(10, 20);<br />result = Obj .demo(5.5);<br />System.out.println("O/P : " + result);<br />}<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">Here the method demo() is overloaded 3 times: first having 1 int parameter, second one has 2 int parameters and third one is having double arg. The methods are invoked or called with the same type and number of parameters used.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Rules of Method Overloading-&nbsp;</strong></div><ul style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li>Overloading can take place in the same class or in its sub-class.</li><li>Constructor in Java can be overloaded</li><li>Overloaded methods must have a different argument list.</li><li>The parameters may differ in their type or number, or in both.</li><li>They may have the same or different return types.</li><li>It is also known as compile time polymorphism.</li></ul><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Why to use Method Overloading-</strong></div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">In the above example you have same function demo but you want to perform some operation using different data so we have created a same function demo but with different parameter.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Method Overriding-</strong></div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">More than one method with same name and same signature but defined in the different class is called as Method Overriding.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Rules of Method Overriding-</strong></div><ul style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;"><li>applies only to inherited methods</li><li>object type (NOT reference variable type) determines which overridden method will be used at runtime</li><li>Overriding method can have different return type (refer this)</li><li>Overriding method must not have more restrictive access modifier</li><li>Abstract methods must be overridden</li><li>Static and final methods cannot be overridden</li><li>Constructors cannot be overridden</li><li>It is also known as Runtime polymorphism.</li></ul><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public class Base<br />{<br />public void methodToOverride() &nbsp; &nbsp; &nbsp;//Base class method<br />{<br />System.out.println ("I'm the method of BaseClass");<br />}<br />}<br />public class Derived extends Base<br />{<br />public void methodToOverride() &nbsp; &nbsp; //Derived Class method<br />{<br />System.out.println ("I'm the method of DerivedClass");<br />}<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public class TestMethod<br />{<br />public static void main (String args []) {<br />// Base class reference and object<br />Base obj1 = new Base();<br />// Base class reference but Derived Class object<br />Base obj2 = new Derived();<br />// Calls the method from Base class class<br />obj1.methodToOverride();<br />//Calls the method from Derived class class<br />obj2.methodToOverride();<br />}<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Why to user Method Overriding-</strong></div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">Overriding is useful when you want to change the functionality of some method based on a class.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Encapsulation-&nbsp;</strong>Encapsulation is the process of hiding the implementation details from the outside world and accessing through publicly exposed method that means setter or getter method.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Ex-</strong></div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public class EnDemo{<br />private int sno;<br />private String empName;<br />private int empAge;</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">//Getter and Setter methods</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public int getEmpSNO(){<br />return sno;<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public String getEmpName(){<br />return empName;<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public int getEmpAge(){<br />return empAge;<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public void setEmpAge(int newValue){<br />empAge = newValue;<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public void setEmpName(String newValue){<br />empName = newValue;<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">public void setEmpSNO(int newValue){<br />sno = newValue;<br />}<br />}<br />public class EncapsTest{<br />public static void main(String args[]){<br />EnDemo obj = new EnDemo();<br />obj.setEmpName("Mario");<br />obj.setEmpAge(32);<br />obj.setEmpSNO(112233);<br />System.out.println("Employee Name: " + obj.getEmpName());<br />System.out.println("Employee SNO: " + obj.getEmpSNO());<br />System.out.println("Employee Age: " + obj.getEmpAge());<br />}<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">In above example all the three data members (or data fields) are private which cannot be accessed directly. These fields can be accessed via public methods only. Fields empName, sno and empAge are made hidden data fields using encapsulation technique of OOPs.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Abstraction-&nbsp;</strong>Abstraction is the process of hiding implementation details and providing functionality to the user, in other word we can say show functionality and hide complexity.Abstraction can be implemented using abstract class and interface.</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;"><strong>Ex-</strong></div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">abstract class Base<br />{<br />int x,y;<br />void show()<br />{<br />System.out.println("x");<br />System.out.println("y");<br />}<br />abstract void display();<br />}</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">class Derived extends Base<br />{<br />void set (int x,int y)<br />{<br />this.x=x;<br />this.y=y;<br />System.out.println("x "+x);<br />System.out.println("y "+y);</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px; margin-bottom: 24px;">}<br />void display()<br />{<br />System.out.println("revoke");</div><div style="color: #3d596d; font-family: Merriweather, Georgia, &quot;Times New Roman&quot;, Times, serif; font-size: 15px;">}<br />public static void main(String args[])<br />{<br />Derived d1= new Derived();<br />d1.set(10,20);<br />d1.show();<br />d1.display();<br />}<br />}</div></div>ajaynmdhttps://plus.google.com/116486022935751414001noreply@blogger.com0