<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3365499786104773673</id><updated>2018-10-29T14:54:06.649+05:30</updated><category term="Interview"/><category term="Java Interview Questions"/><category term="Core Java"/><category term="Java String"/><category term="Arrays"/><category term="Coding Questions"/><category term="Core Java Projects"/><category term="Exception Handling in Java"/><category term="Exception Handling in Java programs"/><category term="Exceptions in Java"/><category term="Exceptions in Java Tutorial"/><category term="Java"/><category term="Java Exception Handling Tutorial"/><category term="Binary Numbers"/><category term="Cab Invoice Generator Java Project"/><category term="Circular Shifting in Arrays"/><category term="Coding"/><category term="Coding Competition"/><category term="Command Line Arguements"/><category term="Comparison:C++ and Java"/><category term="Connectivity"/><category term="Constructors in Java"/><category term="Core Java Desktop Application Project"/><category term="Database"/><category term="Difference between throw and throws"/><category term="Download Java Projects"/><category term="Exception Handling Tutorial Java"/><category term="Find Even and Odd without modulus"/><category term="First java Program"/><category term="How to"/><category term="How to install java"/><category term="How to set environment variables for java"/><category term="Installing Java"/><category term="Interger.parseInt"/><category term="Invoice Generator Java Project"/><category term="JDBC"/><category term="Java Environment Variables"/><category term="Java Myths and Facts"/><category term="Java Program Structure"/><category term="Java Project for beginners"/><category term="Java SE Projects"/><category term="Left Circular Shifting in Arrays"/><category term="Need for setting environment variables in java"/><category term="Nested try blocks"/><category term="Occurrence of pattern in String"/><category term="Oracle"/><category term="Patterns"/><category term="Phone Number Format"/><category term="Project Reports"/><category term="Rent-a-Cab Invoice Generator"/><category term="Resultset"/><category term="Right Circular Shifting in Arrays"/><category term="Running and compiling the java code"/><category term="Setting the Environment Variables"/><category term="Source File Layout"/><category term="Statement"/><category term="String Formatting"/><category term="String Reverse"/><category term="String to Number"/><category term="access protection in packages"/><category term="advantages of using java"/><category term="anagrams"/><category term="attributes and methods"/><category term="core java projects with source code download"/><category term="creating and using the package"/><category term="declaring classes"/><category term="introduction to java"/><category term="java-a revolutionary language"/><category term="naming conventions in java"/><category term="package"/><category term="scope of java"/><category term="top 10 errors java programmers make"/><category term="top ten errors"/><category term="use of command line arguements in java"/><category term="using command line arguements"/><title type='text'>Code in Java</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.codeinjava.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Harish Sharma</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>25</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-773392571377747504</id><published>2015-07-19T17:59:00.002+05:30</published><updated>2015-07-19T21:08:24.856+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="anagrams"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><title type='text'>Java Program to Find Whether the given words are Anagram or Not</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Anagrams as you might already know, are the words or phrase formed by rearranging the letters of another word. For example: angel and glean or stain and satin.&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Well, my description of what the anagrams are ends here as this is not an English grammar tutorial site. What I am more interested in writing out a piece of code to which we can pass the words and can get know whether the words were anagram or not. Well, this is what the solo purpose of this tutorial at least. May be you&#39;d asked such a question somewhere someday, may be in some interview or some coding test, so I just thought to post this here.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So, how do we proceed? Java is such a powerful programming language that using the provided library methods, you might be able to do this in no more than couple of lines of code. Great if you can! But having faced enough number of interviews to assure you of that, the interviewer in most cases, are not that interested in knowing how good you&#39;re at memorizing the method and class names but how quickly your brain can find out a solution to the given problem and your approach. So, the chances are that the &lt;a href=&quot;http://www.codeinjava.com/search/label/Java%20Interview%20Questions&quot; target=&quot;_blank&quot;&gt;Java programming interview questions&lt;/a&gt; that you&#39;d be asked would expect you to solve the problem without relying much upon the library methods.&lt;/div&gt;&lt;div&gt;Well, we&#39;ll do it both ways and we&#39;ll just take two words as the input to our method. You can easily extend the code to include more. (&lt;i&gt;var-args&lt;/i&gt; might be of some help!)&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Using the library methods&lt;/h3&gt;&lt;div&gt;This just makes it ridiculously easy! We will be just reading the &amp;nbsp;given strings, sorting them using &lt;i&gt;Arrays.sort()&lt;/i&gt; and then comparing the sorted strings. If the sorted strings are equal then the words were anagrams. Simple! Because the anagrams as already stated are the words formed by rearranging the words in hand.&amp;nbsp;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Without using the library methods&lt;/h3&gt;&lt;div&gt;Without using the library methods also does not make it any less simpler. We can just first read the two strings, check for their same lengths, take one string character by character and search the other for each characters. And if all characters are found from one string are found in another then the two strings are anagrams because they were of same length.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So, enough of the text, now the program!&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Java program to check whether the two strings are anagram or not&lt;/h3&gt;&lt;div&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;import java.util.Arrays;&lt;br /&gt;import java.util.Scanner;&lt;br /&gt;&lt;br /&gt;public class AnagramDemo {&lt;br /&gt;&lt;br /&gt; // method to check for the anagrams using built-in methods&lt;br /&gt; public static boolean isAnagram(String str1, String str2) {&lt;br /&gt;&lt;br /&gt;  char[] charArr1 = str1.toCharArray();&lt;br /&gt;  char[] charArr2 = str2.toCharArray();&lt;br /&gt;&lt;br /&gt;  // performing sorting of string&lt;br /&gt;  Arrays.sort(charArr1);&lt;br /&gt;  Arrays.sort(charArr2);&lt;br /&gt;&lt;br /&gt;  return Arrays.equals(charArr1, charArr2);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; // method to check for the anagrams without using library methods&lt;br /&gt; public static boolean isAnagramManual(String str1, String str2) {&lt;br /&gt;  // if length itself not equal, return false.&lt;br /&gt;  if (str1.length() != str2.length())&lt;br /&gt;   return false;&lt;br /&gt;  // else do the brainstorming!&lt;br /&gt;  boolean anagram = true;&lt;br /&gt;&lt;br /&gt;  for (int i = 0; i &amp;lt; str1.length(); i++) {&lt;br /&gt;   if (str2.indexOf(str1.charAt(i)) == -1)&lt;br /&gt;    anagram = false;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return anagram;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;  System.out.println(&quot;First string&quot;);&lt;br /&gt;  Scanner sc = new Scanner(System.in);&lt;br /&gt;  String str1 = sc.nextLine();&lt;br /&gt;  System.out.println(&quot;Second string&quot;);&lt;br /&gt;  String str2 = sc.nextLine();&lt;br /&gt;  System.out.println(&quot;Using library methods:-&quot;);&lt;br /&gt;  if (isAnagram(str1, str2))&lt;br /&gt;   System.out.println(str1 + &quot; and &quot; + str2 + &quot; are anagrams!&quot;);&lt;br /&gt;  else&lt;br /&gt;   System.out.println(str1 + &quot; and &quot; + str2 + &quot; are NOT anagrams!&quot;);&lt;br /&gt;&lt;br /&gt;  System.out.println(&quot;WITHOUT USING LIBRARY METHODS&quot;);&lt;br /&gt;  if (isAnagramManual(str1, str2))&lt;br /&gt;   System.out.println(&quot;ANAGRAMS&quot;);&lt;br /&gt;  else&lt;br /&gt;   System.out.println(&quot;NOT ANAGRAMS&quot;);&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;I hope that you find it useful. In case you find any mistakes or the better way of doing it, please write in comments.&lt;br /&gt;In case you have any other Java related question or problem that you want me to write code for, please let me know in comments or &lt;a href=&quot;mailto:iamharish15@codeinjava.com&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;mail me&lt;/a&gt;.&amp;nbsp;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/773392571377747504/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=773392571377747504&amp;isPopup=true' title='34 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/773392571377747504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/773392571377747504'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/07/java-program-to-find-whether-given.html' title='Java Program to Find Whether the given words are Anagram or Not'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>34</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-6817549835447748658</id><published>2015-06-27T19:10:00.002+05:30</published><updated>2015-07-19T21:35:52.231+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Binary Numbers"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Find Even and Odd without modulus"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><title type='text'>Program to check if Least Significant Bit for a number is set or not</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;In Binary number representation of a number, the rightmost bit (or digit) is called the Least Significant Bit (LSB) and the leftmost bit is called the Most Significant Bit (MSB). As we know while converting a binary representation of a number back to its decimal system representation, we calculate the decimal representation by multiplying the LSB by 2&lt;sup&gt;0&lt;/sup&gt; and then increasing the power of 2 from 0 by 1 on each bit as we go from LSB to MSB.&lt;br /&gt;For example: Suppose we have a binary number: 11001&lt;br /&gt;It&#39;s decimal equivalent will be: 1*2&lt;sup&gt;4&lt;/sup&gt;&amp;nbsp;+ 1*2&lt;sup&gt;3&lt;/sup&gt;&amp;nbsp;+ 0*2&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;+ 0*2&lt;sup&gt;1&lt;/sup&gt;&amp;nbsp;+ 1*2&lt;sup&gt;0&lt;/sup&gt; = 25&lt;br /&gt;Let us take another example:&lt;br /&gt;Binary number: 11100&lt;br /&gt;It&#39;s decimal equivalent will be: 1*2&lt;sup&gt;4&lt;/sup&gt;&amp;nbsp;+ 1*2&lt;sup&gt;3&lt;/sup&gt;&amp;nbsp;+ 1*2&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;+ 0*2&lt;sup&gt;1&lt;/sup&gt;&amp;nbsp;+ 0*2&lt;sup&gt;0&lt;/sup&gt;&amp;nbsp;= 28&lt;br /&gt;&lt;br /&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Thing to note from the above discussion&lt;/h3&gt;&lt;div&gt;Sometimes in the interviews you do not get the questions directly and to even understand the question properly, you need to have a careful look at the question statement.&amp;nbsp;&lt;/div&gt;&lt;div&gt;Suppose, like in this case, you are asked to write a program to check if the Least Significant Bit of a number is set or not (And you can not make use of the % and / operators.&amp;nbsp;&lt;/div&gt;&lt;div&gt;Here in this question, if you did not get what the interviewer is expecting from you, you might end up wasting a lot of time calculating the binary equivalent of the number and then manually checking the value of its LSB. But thankfully, this is not what this questions expects you to do. Let us go back to the first section of the post where we discussed in brief about the binary to decimal conversions. The one and only thing I wanted you to notice from the above discussion is that, &lt;i&gt;&lt;b&gt;for even numbers, the LSB is not set and for odd, the LSB is always set.&lt;/b&gt;&lt;/i&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;Now understanding this simple logic can save you a lot of efforts in your interviews and coding competitions. Now you would have understood why the examiner has denied you the use of &#39;%&#39; and &#39;/&#39; operators. So, you are basically assigned a challenge of &lt;i&gt;&lt;b&gt;finding whether a given number is odd or even without using the &#39;%&#39; and &#39;/&#39; operators&lt;/b&gt;&lt;/i&gt;.&amp;nbsp;&lt;/div&gt;&lt;div&gt;There might be many approaches to do that, I will just implement only one in the below Java program method. The method will simply take the input number and return true or false based on whether the LSB is set or not.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;public boolean isLSBSet(int number) {&lt;br /&gt;&lt;br /&gt;  String num = Integer.toString( number );&lt;br /&gt;  int lastDigit = num.length() - 1;&lt;br /&gt;  //Only for odd numbers, the Least Significant Bit will be set.&lt;br /&gt;  if (num.charAt(lastDigit) == &#39;1&#39; || num.charAt(lastDigit) == &#39;3&#39;&lt;br /&gt;    || num.charAt(lastDigit) == &#39;5&#39; || num.charAt(lastDigit) == &#39;7&#39;&lt;br /&gt;    || num.charAt(lastDigit) == &#39;9&#39;)&lt;br /&gt;   return true;&lt;br /&gt;  else&lt;br /&gt;   return false;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;strong&gt;We thank DJ Spiess for his tip! The whole above code now can be reduced to one line.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;return (number ^ (number - 1)) == 1;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;So, Kudos! We now know &lt;em&gt;How to check if number is even or odd without using the modulus &#39;%&#39; or &#39;/&#39; operator.&lt;/em&gt; Keep sharing such tips to the community and help us all learn Java in a simple and fun way! :)&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/6817549835447748658/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=6817549835447748658&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6817549835447748658'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6817549835447748658'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/program-to-check-lsb-set-or-not.html' title='Program to check if Least Significant Bit for a number is set or not'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-827317957731384403</id><published>2015-06-27T18:21:00.003+05:30</published><updated>2015-07-19T21:36:30.485+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Java String"/><category scheme="http://www.blogger.com/atom/ns#" term="Occurrence of pattern in String"/><category scheme="http://www.blogger.com/atom/ns#" term="Patterns"/><title type='text'>Java Program to find occurrence of a pattern in String with (and without) String methods</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Finding patterns in the String : The problem description&lt;/h2&gt;&lt;div&gt;Today, I will be writing a program to print the occurrence of a String pattern in the given input String in two ways: one using the String substring() method (which will make the task very easy) and second without using the substring() method (similar but need to do the substring calculation and comparison manually).&lt;/div&gt;&lt;div&gt;The scenario is we have an input String of characters and we want to find the occurrences of some pattern (which will of course be the substring of the input string).&amp;nbsp;&lt;/div&gt;&lt;div&gt;For example:-&amp;nbsp;&lt;/div&gt;&lt;div&gt;Input String: &quot;CODEINJAVATUTORIALSFORJAVABEGINNERS&quot;&lt;/div&gt;&lt;div&gt;Patter String: &quot;JAVA&quot;&lt;/div&gt;&lt;div&gt;Occurrence: 2&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Logic for finding the pattern occurrences in the String&lt;/h2&gt;&lt;div&gt;We will go for the simple and straightforward logic. And that is, we will be finding all the sub-strings of length equal to the length of the pattern we are looking for. And then from those list of sub-strings, we will pick sub-strings one by one and compare it against the patter string. Whenever the match happens, we will increment the occurrence count. So, the main task here seems to be just finding all the sub-strings of length L equal to the length of pattern string. The rest part is just comparison with the pattern and counting the occurrences.&amp;nbsp;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Using String substring() method&lt;/h3&gt;&lt;div&gt;Using the String utility method &lt;i&gt;substring()&lt;/i&gt; for finding the sub-strings makes this task extremely easy and straightforward. Below is the method that takes the input string and pattern string and return the number of occurrences of pattern in input string.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;public int countOccurenceOfPattern(String input, String pattern) {&lt;br /&gt;  int inputLength = input.length();&lt;br /&gt;  int patternLength = pattern.length();&lt;br /&gt;  int occurrence = 0;&lt;br /&gt;&lt;br /&gt;  // Using String methods. In this case we&#39;ll need only substring()&lt;br /&gt;  for (int i = 0; i &lt; (inputLength - patternLength); i++) {&lt;br /&gt;   if ((input.substring(i, i + patternLength)).equals(pattern))&lt;br /&gt;    occurrence++;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return occurrence;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Without using the substring() method&lt;/h3&gt;&lt;div&gt;Well, here is when we will have to do a bit of logic building and do the things manually. Well this doesn&#39;t complicate the code much and is still very straightforward. We will be creating the substrings of length L equal to length of pattern manually iterating through the two loops and comparing the each substring with the pattern string. Below is the previous method re-written to achieve the same objective without using the substring() method.&lt;/div&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;public int countOccurenceOfPattern(String input, String pattern) {&lt;br /&gt;  int inputLength = input.length();&lt;br /&gt;  int patternLength = pattern.length();&lt;br /&gt;  int occurrence = 0;&lt;br /&gt;&lt;br /&gt;  // Without using substring()&lt;br /&gt;  // In that we will be required to do a bit of logic building ourself.&lt;br /&gt;  String temp = &quot;&quot;;&lt;br /&gt;&lt;br /&gt;  for (int i = 0; i &lt; (inputLength - patternLength); i++) {&lt;br /&gt;   temp = &quot;&quot;;&lt;br /&gt;   for (int j = i; j &lt; (i + patternLength); j++) {&lt;br /&gt;    temp = temp + Character.toString(input.charAt(j));&lt;br /&gt;   }&lt;br /&gt;   if (temp.equals(pattern))&lt;br /&gt;    occurrence++;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return occurrence;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/827317957731384403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=827317957731384403&amp;isPopup=true' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/827317957731384403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/827317957731384403'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/java-program-occurrence-of-pattern-in-string.html' title='Java Program to find occurrence of a pattern in String with (and without) String methods'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-8242234448239746931</id><published>2015-06-24T21:20:00.001+05:30</published><updated>2015-07-19T21:36:51.934+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="Circular Shifting in Arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Left Circular Shifting in Arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="Right Circular Shifting in Arrays"/><title type='text'>Circular Shifting of elements in Arrays - Java program to shift array elements circularly</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;What do we mean by shifting the elements of an array&lt;/h3&gt;&lt;div&gt;Many a times in our programs, we need to shift the elements of our array either to the left or to the right. For example, while dynamically adding any new element in between the existing elements of an array, we will be right shifting the elements after the inserted position. Similarly, we will do a left shift of the elements when we remove an element.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Circular Shifting in Arrays &amp;nbsp;- Right Circular Shift and Left Circular Shift&lt;/h3&gt;&lt;div&gt;As we are now familiar with what we mean by shifting the array elements, the term circular adds a little more complexity to the otherwise a simple process of shifting elements of arrays to the left or right.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Left Circular Shifting of Array Elements: &lt;/b&gt;In left circular shifting of array elements by N positions, we will take the first N elements starting from the left most side of the array and start placing them from the rear of the array in the same order as we took out them from the front of array.&amp;nbsp;&lt;/div&gt;&lt;div&gt;for example:&amp;nbsp;&lt;/div&gt;&lt;div&gt;Suppose we have an input array like this:&lt;/div&gt;&lt;div&gt;10, 20, 30, 40, 50&lt;/div&gt;&lt;div&gt;After left circular shift by 2 positions, the array would become:-&lt;/div&gt;&lt;div&gt;30, 40, 50, 10, 20&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Right Circular Shifting of Array Elements:&lt;/b&gt;&amp;nbsp;The &lt;i&gt;Right circular shifting of arrays&lt;/i&gt; is exactly opposite to that of the Left circular shifting. In this we will be taking out the N elements from the rear of the array and will be inserting them to the front of the array. The below example would help us to understand this much easily.&lt;/div&gt;&lt;div&gt;for example:-&lt;/div&gt;&lt;div&gt;We have an input array: 10, 20, 30, 40, 50&lt;/div&gt;&lt;div&gt;After right circular shift by 2 positions: 40, 50, 10, 20, 30&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Java program to implement left circular shifting and right circular shifting of array elements&lt;/h3&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;public class CodeInJava{&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  CodeInJava obj = new CodeInJava();&lt;br /&gt;  //input array&lt;br /&gt;  int[] inputArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};&lt;br /&gt;  &lt;br /&gt;  obj.printArray( inputArray );&lt;br /&gt;  &lt;br /&gt;  //circular left shifted array by 2 positions. &lt;br /&gt;  int[] leftShiftedArray = obj.circularShiftSingle( inputArray, 2, false );&lt;br /&gt;  &lt;br /&gt;  obj.printArray(leftShiftedArray);&lt;br /&gt;  &lt;br /&gt;  //circular right shifted array by 2 positions&lt;br /&gt;  int[] rightShiftedArray = obj.circularShiftSingle( inputArray, 2, true );&lt;br /&gt;  &lt;br /&gt;  obj.printArray( rightShiftedArray );&lt;br /&gt;  &lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public int[] circularShiftSingle(int[] array, int shift, boolean right) {&lt;br /&gt;  int[] array2 = new int[shift];&lt;br /&gt;  if(right) {&lt;br /&gt;   for (int i = 0;i &amp;lt; shift;i++)&lt;br /&gt;    array2[i] = array[array.length - shift + i];&lt;br /&gt;   System.arraycopy(array, 0, array, shift, array.length - shift);&lt;br /&gt;   for (int i = 0;i &amp;lt; shift;i++) {&lt;br /&gt;    array[i] = array2[i];&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  else {&lt;br /&gt;   for (int i = 0;i &amp;lt; shift;i++)&lt;br /&gt;    array2[i] = array[i];&lt;br /&gt;   System.arraycopy(array, shift, array, 0, array.length - shift);&lt;br /&gt;   for (int i = array.length - shift; i &amp;lt; array.length; i++) {&lt;br /&gt;    array[i] = array2[shift + i - array.length];&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return array;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; //utility method just to display the elements of an array&lt;br /&gt; private void printArray(int[] array) {&lt;br /&gt;  for (int element:array)&lt;br /&gt;   System.out.print(element + &quot;, &quot;);&lt;br /&gt;  System.out.println();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output:&lt;/h4&gt;&lt;div&gt;&lt;div&gt;10, 20, 30, 40, 50, 60, 70, 80, 90, 100,&amp;nbsp;&lt;/div&gt;&lt;div&gt;30, 40, 50, 60, 70, 80, 90, 100, 10, 20,&amp;nbsp;&lt;/div&gt;&lt;div&gt;10, 20, 30, 40, 50, 60, 70, 80, 90, 100,&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;The first line shows the input as it is.&lt;/div&gt;&lt;div&gt;The second line shows the input after circular left shifting by 2 positions.&lt;/div&gt;&lt;div&gt;The third line shows the array in 2nd line after circular right shifting by 2 positions.&lt;/div&gt;&lt;div&gt;So, we shifted 2 positions left and then we shift 2 positions right on the same input array and we will get the same input array back in line 3rd.&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/8242234448239746931/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=8242234448239746931&amp;isPopup=true' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/8242234448239746931'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/8242234448239746931'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/circular-shifting-of-elements-in-arrays-java.html' title='Circular Shifting of elements in Arrays - Java program to shift array elements circularly'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-6395278406201310162</id><published>2015-06-24T19:41:00.001+05:30</published><updated>2015-06-24T19:41:17.429+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Java String"/><title type='text'>Java Program to print only the unique elements from the String array without using Collections</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;In Java, you&#39;ll rarely be writing a program without using the arrays. As the arrays put no limitations on what type of elements you can put in them or do not provide any means to check for the duplicates, there might be some scenarios wherein you might need only the distinct elements from the array instead of all its elements.&lt;br /&gt;Suppose we have an array of Strings and we need only the distinct strings out from that array. Obviously, the same task can be done conveniently using Collections API but there might be cases where you&#39;ll be just better off not requiring to put any overhead of using the Collections classes such as Sets in this case. Or if this does not convince you, knowing how to do this without using the Collections will still be worth learning if you&#39;re going to appear in some interviews or for some coding competitions. As I have repeatedly mentioned in my posts that &lt;a href=&quot;http://www.codeinjava.com/search/label/Java%20String&quot; target=&quot;_blank&quot;&gt;Java programming questions on Strings&lt;/a&gt; are among the hot favorite for interviewers, so I&#39;ll be posting many more such &lt;i&gt;String based java programming questions&lt;/i&gt; in my future posts.&lt;br /&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Java Program to find only distinct elements of String Array without using Collections&lt;/h3&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class=&quot;brush: java&quot;&gt;&lt;br /&gt;public class CodeInJava {&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  //Input array&lt;br /&gt;  String str[] = {&quot;Java&quot;,&quot;Struts2&quot;,&quot;Java&quot;,&quot;Spring3&quot;,&quot;C++&quot;,&quot;Python&quot;,&quot;Java&quot;};&lt;br /&gt;  &lt;br /&gt;  //Creating new array to store only the unique elements&lt;br /&gt;  String[] uniqueArray = new String[str.length];&lt;br /&gt;  &lt;br /&gt;  //copying the first element directly to uniqueArray&lt;br /&gt;  uniqueArray[0] = str[0];&lt;br /&gt;  &lt;br /&gt;  //Counter to keep track of the index to put next value at in uniqueArray&lt;br /&gt;  int counter = 1;&lt;br /&gt;  &lt;br /&gt;  boolean flag = false;&lt;br /&gt;  for(int i = 1;i &lt; str.length;i++) {&lt;br /&gt;   flag = false;&lt;br /&gt;   for (int j = 0;j &lt; i;j++) {&lt;br /&gt;    if(uniqueArray[j] == null)&lt;br /&gt;     j=i;&lt;br /&gt;    //comparing in case-insensitive manner.&lt;br /&gt;    //In case you need to find unique in case sensitive manner, use equals() only&lt;br /&gt;    else if( str[i].equalsIgnoreCase( uniqueArray[j] ) )&lt;br /&gt;     flag = true;&lt;br /&gt;   }&lt;br /&gt;   if( !flag ) {&lt;br /&gt;    uniqueArray[counter] = str[i];&lt;br /&gt;    counter++;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  //Printing the unique elements. Counter stores the number of unique elements in the original array.&lt;br /&gt;  for (int i = 0; i &lt; counter; i++) &lt;br /&gt;   System.out.println(uniqueArray[i]);&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/6395278406201310162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=6395278406201310162&amp;isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6395278406201310162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6395278406201310162'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/java-program-to-print-only-unique.html' title='Java Program to print only the unique elements from the String array without using Collections'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-2737702801000973913</id><published>2015-06-23T12:12:00.002+05:30</published><updated>2015-06-23T12:18:18.528+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Interger.parseInt"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Java String"/><category scheme="http://www.blogger.com/atom/ns#" term="String to Number"/><title type='text'>Java program to convert String into Integer without Integer.parseInt()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Although Java SDK provides you with convenient methods to convert the compatible data from one data types to another, but sometimes in &lt;i&gt;interviews&lt;/i&gt; and other coding competitions you are also supposed to know how to do those conversions without those built-in methods provided by Java SDK itself. In one such case, one of the most common thing in your java code is to convert the string representation of any number back to native int or Integer data type in Java. Although there is a simple method &lt;b&gt;&lt;i&gt;Integer.parseInt(String num)&lt;/i&gt;&lt;/b&gt; that you can use to get the string num in &lt;i&gt;int&lt;/i&gt; format, but in case you need to do that stuff manually, this post will show you how to do that.&lt;br /&gt;&lt;br /&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Logic building and approach&lt;/h3&gt;&lt;div&gt;We will be first reading the string backwards character by character looking for the digit characters only. Till we keep getting the digit characters, we will use those to formulate our number by using the digits and increment the factor by 10 in each step.&lt;/div&gt;&lt;div&gt;We are simply ignoring the non-digit characters here or otherwise in we want, we can convert each character to its number representation, but I don&#39;t think that&#39;s what we need. And finally, we will also take care of negative numbers as well. For that we will check for the &quot;-&quot; in the very first place in the String input, if it is &quot;-&quot; then we will make the number negative.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Java Program to Manually convert string to int without Integer.parseInt() method&lt;/h3&gt;&lt;div&gt;Below is the simple Java method that takes a String str as input and return the int representation of its digits only.&amp;nbsp;&lt;/div&gt;&lt;div&gt;For example:-&lt;/div&gt;&lt;div&gt;&quot;1263*67%&quot; will return 126367&lt;/div&gt;&lt;div&gt;&quot;a76dsb6l7&quot; will return 76617&lt;/div&gt;&lt;div&gt;&quot;-78fjkd7439&quot; will return -78439&lt;/div&gt;&lt;div&gt;&quot;289289&quot; will return 289289&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class=&quot;brush: java&quot;&gt;public int stringToInt(String str) {&lt;br /&gt;  int number = 0, factor = 1; &lt;br /&gt;  boolean negative = false;&lt;br /&gt;  for (int i = str.length() - 1;i &amp;gt;= 0;i--) {&lt;br /&gt;   //check if the very first character has &#39;-&#39;, then the result must be negative&lt;br /&gt;   if((i == 0) &amp;amp;&amp;amp; (str.charAt(i) == &#39;-&#39;)) {&lt;br /&gt;    negative = true;&lt;br /&gt;    continue;&lt;br /&gt;   }&lt;br /&gt;   //if the string has any non-number characters, continue with the next character ignoring the current one&lt;br /&gt;   else if(&quot;0123456789&quot;.indexOf( str.charAt(i) ) == -1) {&lt;br /&gt;    continue;&lt;br /&gt;   }&lt;br /&gt;   number+= (str.charAt(i) - &#39;0&#39;) * factor;&lt;br /&gt;   factor*= 10;&lt;br /&gt;  }&lt;br /&gt;  if(negative)&lt;br /&gt;   return -number;&lt;br /&gt;  return number;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;There might be many other methods to achieve this as well. For instance, we could have used the static methods of &lt;i&gt;Character&lt;/i&gt; class such as &lt;i&gt;isDigit()&lt;/i&gt; to check for whether the characters are digits or not. In fact, this whole program could have been done using the static methods of Character class. You may want to give it a try!&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/2737702801000973913/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=2737702801000973913&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2737702801000973913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2737702801000973913'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/java-program-to-convert-string-into.html' title='Java program to convert String into Integer without Integer.parseInt()'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-1381900082270692966</id><published>2015-06-20T21:04:00.000+05:30</published><updated>2015-06-20T21:04:16.636+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Coding Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Java String"/><category scheme="http://www.blogger.com/atom/ns#" term="String Reverse"/><title type='text'>Java program to reverse a string word by word</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Program to reverse a String word by word in Java&lt;/h2&gt;&lt;div&gt;String based programming questions are among the most asked questions in the Java interviews for the freshers as well as the experienced Java developers and Software engineers. And reversing a string word by word or characters by characters is very likely to be asked in any Java technical interview. In this post, I will be writing a program for the former i.e. for &lt;i&gt;reversing a Java String word by word&lt;/i&gt;.&amp;nbsp;&lt;/div&gt;&lt;div&gt;For example:-&amp;nbsp;&lt;/div&gt;&lt;div&gt;Input String: Code In Java&lt;/div&gt;&lt;div&gt;Output String: Java In Code&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;There might be many approaches for solving this and you may want to choose based on your liking and its simplicity.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Below is a &lt;i&gt;Java program to reverse a String word by word&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class=&quot;brush: java&quot;&gt;public class ReverseString {&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  //Input String&lt;br /&gt;  String str = &quot;Code in Java - Programming in Java Tutorials for beginners&quot;;&lt;br /&gt;  &lt;br /&gt;  //Approach 1 : Using String Array&lt;br /&gt;  String[] reverseArray = str.split(&quot; &quot;);&lt;br /&gt;  &lt;br /&gt;  //String to store the reversed string&lt;br /&gt;  String reverseString1 = &quot;&quot;;&lt;br /&gt;  &lt;br /&gt;  //reading each word from the reverseArray and creating the reverseString1&lt;br /&gt;  for(int i = reverseArray.length-1;i &gt;= 0;i--)&lt;br /&gt;  {&lt;br /&gt;   reverseString1+= reverseArray[i] + &quot; &quot;;   &lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  System.out.println( reverseString1 );&lt;br /&gt;  &lt;br /&gt;  //Approach 2: Using StringBuilder&lt;br /&gt;  StringBuilder revString = new StringBuilder();&lt;br /&gt;  &lt;br /&gt;  //String to store the reversed string&lt;br /&gt;  String reverseString2 = &quot;&quot;;&lt;br /&gt;  &lt;br /&gt;  for (int i = 0; i&lt; reverseArray.length;i++) {&lt;br /&gt;   revString.append( reverseArray[ reverseArray.length - 1 - i ]).append(&#39; &#39;);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  //creating String from the StringBuilder object&lt;br /&gt;  reverseString2 = revString.toString();&lt;br /&gt;  &lt;br /&gt;  System.out.println( reverseString2 );&lt;br /&gt;  &lt;br /&gt;  //There might be many other approaches to this as well. Please share the same in comments.&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/1381900082270692966/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=1381900082270692966&amp;isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1381900082270692966'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1381900082270692966'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/java-program-to-reverse-string-word-by.html' title='Java program to reverse a string word by word'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-1388499379096796045</id><published>2015-06-14T23:58:00.003+05:30</published><updated>2015-06-26T23:16:45.335+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Coding"/><category scheme="http://www.blogger.com/atom/ns#" term="Coding Competition"/><category scheme="http://www.blogger.com/atom/ns#" term="Coding Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Phone Number Format"/><category scheme="http://www.blogger.com/atom/ns#" term="String Formatting"/><title type='text'>Java Program to Format the String to Phone Number Format with dashes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Java Program to format the given input string to phone number format with dashes&lt;/h2&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Below is a coding challenge question I recently attempted in some competition. Suppose we are given a string that might contain some undesired characters apart from the digits. We are supposed to convert it into a phone number format with dashes with grouping of 3 digits. In case, only 4 digits are left in the end after grouping other numbers in a group of 3, then the numbers should be grouped in 2. In case 5 digits are left in the end, the digits should be grouped in 3 and 2.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;For example: &quot;89437438843921&quot; should be converted into 894-374-388-439-21&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;and &quot;7836473097338&quot; should be converted into 783-647-309-73-38&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;and &quot;483974937434&quot; should be converted into 483-974-937-434&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;And in case there are some alphabets and special characters, we should ignore them and remove from the output string.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;/ul&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Java Program to convert a given String to Phone number format with dashes:-&lt;/h4&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class=&quot;brush: java&quot;&gt;public class PhoneNumberFormat {&lt;br /&gt; &lt;br /&gt; public String formatToPhoneNumberFormat(String S) {&lt;br /&gt;  StringBuilder filterOutJunk = new StringBuilder();&lt;br /&gt;  int digitCount = 0;&lt;br /&gt;  for (int i = 0;i &amp;lt; S.length();i++) {&lt;br /&gt;   if(&quot;0123456789&quot;.indexOf(S.charAt(i))==-1)&lt;br /&gt;    continue;&lt;br /&gt;   digitCount++;&lt;br /&gt;   filterOutJunk.append(S.charAt(i));&lt;br /&gt;  }&lt;br /&gt;  //if the input contains less than 2 digits, return null&lt;br /&gt;  if(digitCount &amp;lt; 2)&lt;br /&gt;   return null;&lt;br /&gt;  //now the filterOutJunk contains only the clean string containing digits only&lt;br /&gt;  filterOutJunk = new StringBuilder(filterOutJunk.toString());&lt;br /&gt;  //start inserting the dashes after 3rd digit&lt;br /&gt;  int insertDashes = 3;&lt;br /&gt;  while (insertDashes &amp;lt; filterOutJunk.length())&lt;br /&gt;  {&lt;br /&gt;   //insert dashes after 3 digits&lt;br /&gt;      filterOutJunk.insert(insertDashes, &quot;-&quot;);&lt;br /&gt;      //but if we are left with only four digits in the end&lt;br /&gt;      //enter dashes after 2 digits (2 + 1 = 3 for the fact that dash also takes one place)&lt;br /&gt;      if(insertDashes == (filterOutJunk.length() - 5))&lt;br /&gt;       insertDashes = insertDashes + 3;&lt;br /&gt;      else&lt;br /&gt;       insertDashes = insertDashes + 4;&lt;br /&gt;  }&lt;br /&gt;  return filterOutJunk.toString();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  PhoneNumberFormat t = new PhoneNumberFormat();&lt;br /&gt;  //Hard coded input for testing purpose. Take it from user.&lt;br /&gt;  //Check for maximum input length of 100.&lt;br /&gt;  String input = &quot;47983&quot;; &lt;br /&gt;  String formatted = t.formatToPhoneNumberFormat(input);&lt;br /&gt;  System.out.println(formatted);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;Please try out this on your own first and then refer this in case you get stuck somewhere. This will help you learn. And in case you find out this code not working properly for any given input string, please do let me know in comments.&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/1388499379096796045/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=1388499379096796045&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1388499379096796045'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1388499379096796045'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2015/06/java-program-to-format-string-to-phone.html' title='Java Program to Format the String to Phone Number Format with dashes'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-1303148164883469794</id><published>2012-12-19T01:38:00.001+05:30</published><updated>2015-06-15T00:04:43.563+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Difference between throw and throws"/><category scheme="http://www.blogger.com/atom/ns#" term="Exception Handling in Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Exception Handling in Java programs"/><category scheme="http://www.blogger.com/atom/ns#" term="Exceptions in Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Exceptions in Java Tutorial"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Exception Handling Tutorial"/><category scheme="http://www.blogger.com/atom/ns#" term="Nested try blocks"/><title type='text'>Java Exception Handling Tutorial-2</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2&gt;Advanced Exception Handling in Java Tutorial with Example Programs&lt;/h2&gt;This post is in continuation of the previous &lt;a href=&quot;http://codeinjavase.blogspot.com/2012/12/java-exception-handling-tutorial.html&quot; target=&quot;_blank&quot;&gt;Java Exception Handling Tutorial&lt;/a&gt;. After reading the previous post, the basic concept of using &lt;em&gt;try&lt;/em&gt; and &lt;em&gt;catch&lt;/em&gt; block should be clear. The other thing that is to be noted in context of using &lt;em&gt;try&lt;/em&gt; and &lt;em&gt;catch&lt;/em&gt; block is that &lt;em&gt;try&lt;/em&gt; block can not exist alone, it must be followed by at least one &lt;em&gt;catch&lt;/em&gt; block or &lt;em&gt;finally&lt;/em&gt; block. The next step that you should learn about is the nesting of &lt;em&gt;try&lt;/em&gt; blocks.&lt;br /&gt;&lt;h3&gt;Nesting Try Blocks&lt;/h3&gt;The &lt;em&gt;try&lt;/em&gt; statement can be nested. That is, a &lt;em&gt;try&lt;/em&gt; statement can be inside the block of another try. Each time a &lt;em&gt;try&lt;/em&gt; statement is entered, the context of that exception is pushed on the stack. If an inner &lt;em&gt;try&lt;/em&gt; statement does not have a &lt;em&gt;catch&lt;/em&gt; handler for a particular exception, the stack is unwound and the next try statement&#39;s catch handlers are inspected for a match. This continues until one of the &lt;em&gt;catch&lt;/em&gt; statements succeeds, or until all of the nested &lt;em&gt;try&lt;/em&gt; statements are exhausted. If no &lt;em&gt;catch&lt;/em&gt; statement matches, then the Java run-time system will handle the exception.&lt;br /&gt;Below example program demonstates how the nesting of &lt;em&gt;try&lt;/em&gt; blocks work in Java exception handling.&lt;br /&gt;&lt;h4&gt;Program to demonstate nesting of try blocks in Java Exception Handling&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/h4&gt;&lt;pre class=&quot;brush: java&quot;&gt;import java.util.*;&lt;br /&gt;class DemoExceptionHandling2{&lt;br /&gt; public static void main(String [] args){&lt;br /&gt;  Scanner s = new Scanner(System.in);&lt;br /&gt;  int a = 1;&lt;br /&gt;  int b = 1;&lt;br /&gt;  int n = 0;&lt;br /&gt;  int c[] = {1};&lt;br /&gt;  //enter value of B as zero to cause a divide by zero exception in Outer try block&lt;br /&gt;  System.out.println(&quot;Enter value of B:&quot;);&lt;br /&gt;  b=s.nextInt();&lt;br /&gt;  //Outer Try Block&lt;br /&gt;  try{&lt;br /&gt;   System.out.println(&quot;Outer Try Block Entered!&quot;);&lt;br /&gt;   System.out.println(&quot;A/B=&quot;+a/b);  //divide by zero exception occurs here if b=0&lt;br /&gt;   System.out.println(&quot;No Exception has occured in Outer try block!!&quot;);&lt;br /&gt;   //Inner Try Block&lt;br /&gt;   try{&lt;br /&gt;    System.out.println(&quot;Nested Try Block Entered!&quot;);&lt;br /&gt;    System.out.println(&quot;Enter 0 to run normal, 1 or greater to cause ArrayIndexOutOfBoundsException:&quot;);&lt;br /&gt;    n = s.nextInt();&lt;br /&gt;    System.out.println(&quot;C= &quot;+c[n]);&lt;br /&gt;    System.out.println(&quot;Enter value of B again:&quot;); //enter zero to cause divide by zero Arithmetic Exception&lt;br /&gt;    b = s.nextInt();&lt;br /&gt;    System.out.println(&quot;a/b =&quot;+a/b);&lt;br /&gt;    System.out.println(&quot;No exception in inner try block has occured!!&quot;);&lt;br /&gt;   }&lt;br /&gt;   //inner catch block will only catch Array Index Out of Bounds Exceptions&lt;br /&gt;   catch(ArrayIndexOutOfBoundsException e){&lt;br /&gt;    System.out.println(&quot;Exception caught in Inner catch block : &quot;+e);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;  //This outer catch block will catch Arithmetic Exceptions&lt;br /&gt;  catch(ArithmeticException ae){&lt;br /&gt;   System.out.println(&quot;Exception Caught in Outer Catch Block 1: &quot;+ae);&lt;br /&gt;  }&lt;br /&gt;  //This outer catch block will catch all the uncaught Exceptions&lt;br /&gt;  catch(Exception eo){&lt;br /&gt;   System.out.println(&quot;Exception Caught in Outer Block 2: &quot;+eo);&lt;br /&gt;  }&lt;br /&gt;  //finally block will execute every time&lt;br /&gt;  finally{&lt;br /&gt;   System.out.println(&quot;The code inside the finally block is executing...&quot;);&lt;br /&gt;  }&lt;br /&gt;  System.out.println(&quot;Line after try-catch-finally Block!&quot;);&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;The above program simply demonstrates the concept of nesting &lt;em&gt;try&lt;/em&gt; blocks in Java. I encourage you to try it yourself by adjusting the different values and notice how and which exception is being caught where. But anyway, I would like to brief the results of the above program. When you make b=0, the exception in outer &lt;em&gt;try&lt;/em&gt; block occurs and then the whole &lt;em&gt;try&lt;/em&gt; block including the nested inner block is skipped and the exception is caught by first &lt;em&gt;catch&lt;/em&gt; block (which is coded to catch &lt;em&gt;ArithmeticException&lt;/em&gt; such as divide by zero) and then the &lt;em&gt;finally&lt;/em&gt; block is executed followed by any code afterwards. But when you make b=1, outer &lt;em&gt;try&lt;/em&gt; block works fine and inner &lt;em&gt;try&lt;/em&gt; block is reached. If you make n&amp;gt;1, then it causes &lt;em&gt;ArrayIndexOutOfBoundsException&lt;/em&gt; to occur, which is caught by the inner &lt;em&gt;catch&lt;/em&gt; block. But if &lt;em&gt;ArrayIndexOutOfBoundsException&lt;/em&gt; is not caused but a Divide by Zero exception in inner &lt;em&gt;try&lt;/em&gt; block, then there is no corresponding &lt;em&gt;catch&lt;/em&gt; block to catch that &lt;em&gt;ArithmeticException&lt;/em&gt; and in that case that exception is caught in the outer &lt;em&gt;catch&lt;/em&gt; block.&lt;br /&gt;&lt;h3&gt;Difference between throw and throws&lt;/h3&gt;&lt;strong&gt;throw&lt;/strong&gt;: &lt;em&gt;throw&lt;/em&gt; keyword is used to explicitly throw any exception in your java program, till now all the exceptions that were caught by the &lt;em&gt;catch&lt;/em&gt; block, were thrown by the Java run-time system. The general form of using &lt;em&gt;throw&lt;/em&gt; keyword is:&lt;br /&gt;throw ThrowableInstance;&lt;br /&gt;Here the ThrowableInstance is an object of &lt;em&gt;Throwable&lt;/em&gt; class or any of its subclasses. Let me remind you that &lt;em&gt;Throwable&lt;/em&gt; is the base class of &lt;em&gt;Exception&lt;/em&gt; class, which in turn is the base class of all the exceptions. One can use the new keyword to create the object of &lt;em&gt;Throwable&lt;/em&gt; class. &lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;throw new IllegalAccessException;&lt;/pre&gt;or &lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;throw new IllegalAccessException(&quot;demo exception&quot;);&lt;/pre&gt;&lt;br /&gt;The above two options of throwing occurs due to the presence of at least two constructors of &lt;em&gt;Throwable&lt;/em&gt; and all of its sub-classes. One without the parameter and one with string parameter describing that exception.&lt;br /&gt;&lt;strong&gt;throws&lt;/strong&gt;: &lt;em&gt;throws&lt;/em&gt; is used with methods to signal that the method might cause some exception (s) that it is not equipped to handle, so that the calling method can do the exception handling arrangements while calling that function. If a method is capable of causing an exception that it does not handle, then it must specify the type of exceptions it can cause using &lt;em&gt;throws&lt;/em&gt; keyword followed by the comma separated list of all the exception classes that it is suspected to cause. Any method that calls such method with &lt;em&gt;throws&lt;/em&gt; declaration must provide means of handling all those exceptions by either enclosing the function call in &lt;em&gt;try-catch-finally&lt;/em&gt; block or rethrowing those exceptions to its calling methods, if any.&lt;br /&gt;The general form of using &lt;em&gt;throws&lt;/em&gt; is like:&lt;br /&gt;type method-name(parameter-list) throws exception-list&lt;br /&gt;for example: &lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;double anyMethod(double x, double y) throws IOException, FileNotFoundException, IllegalAccessException{//method-body}&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Defining and Using your own Exceptions&lt;/h3&gt;Java also enables you to create your own exceptions to handle situations specific to your applications. Although most of the errors are handled by Java&#39;s built-in exceptions, but being able to create your own exceptions might come handly in some particular situations depending on your application. Creating your own Exceptions is very easy, you just need to define a subclass of Exception class and specify the constructors and the other methods you want to add to specify your particular exception scenario. This new built class does not inherit any functions from Exception class as Exception class does not even define any methods of its own; it inherits from Throwable class.&lt;br /&gt;Below is the simple example to demonstate how to define your own exceptions in java.&lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;class MyException extends Exception{&lt;br /&gt; private int i;&lt;br /&gt; MyException(int x){&lt;br /&gt;  i=x;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;public class MyCustomException{&lt;br /&gt; static void displayNumber(int x) throws MyException{&lt;br /&gt;  System.out.println(&quot;called displayNumber(&quot;+x+&quot;)&quot;);&lt;br /&gt;  if(x&amp;gt;15)&lt;br /&gt;  {&lt;br /&gt;   throw new MyException(x);&lt;br /&gt;  }&lt;br /&gt;  System.out.println(&quot;MyException did not occur!&quot;);&lt;br /&gt; }&lt;br /&gt;  public static void main(String args[]){&lt;br /&gt;   try{&lt;br /&gt;    displayNumber(5);&lt;br /&gt;    displayNumber(25);&lt;br /&gt;   }catch(MyException me){&lt;br /&gt;    System.out.println(&quot;MyException Caught: &quot;+me);&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;Exceptions handling in java is a great tool which you can handle to ensure the smooth and uninterrupted execution of your program. The above tutorials are still the basic level &lt;a href=&quot;http://codeinjava.com/p/java-se-tutorials.html&quot; target=&quot;_blank&quot;&gt;tutorials&lt;/a&gt; meant for &lt;a href=&quot;http://codeinjavase.blogspot.com/&quot; target=&quot;_blank&quot;&gt;beginners in Java&lt;/a&gt; only. Once you get over with these, you can try more advanced use of the Exceptions Handling in java. &lt;br /&gt;If you have any questions regarding this and the&lt;a href=&quot;http://codeinjavase.blogspot.com/2012/12/java-exception-handling-tutorial.html&quot; target=&quot;_blank&quot;&gt; previous tutorials&lt;/a&gt;, you can write in the comments below.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/1303148164883469794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=1303148164883469794&amp;isPopup=true' title='48 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1303148164883469794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1303148164883469794'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2012/12/java-exception-handling-tutorial-2.html' title='Java Exception Handling Tutorial-2'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>48</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-2475424498403532188</id><published>2012-12-03T01:00:00.004+05:30</published><updated>2012-12-19T02:00:38.987+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Exception Handling in Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Exception Handling in Java programs"/><category scheme="http://www.blogger.com/atom/ns#" term="Exception Handling Tutorial Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Exceptions in Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Exceptions in Java Tutorial"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Exception Handling Tutorial"/><title type='text'>Java Exception Handling Tutorial</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2&gt;Exception Handling in Java Tutorial with Example Programs&lt;/h2&gt;&lt;h3&gt;Exceptions in Java - What are Java Exceptions?&lt;/h3&gt;A Java Exception is an abnormal condition that arises during  the execution of a program and also called a run-time error. An exception in  Java signals the unusual or catastrophic situations that can arise.&lt;br /&gt;Examples of Java Exceptions are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Incorrect user input such as division by zero&lt;/li&gt;&lt;li&gt;File that needs to be opened not found&lt;/li&gt;&lt;li&gt;Network connection problem&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Types of Java Exceptions&lt;/h3&gt;Like anything else in this world, Exceptions can also be  categorized! Based on whether or not you are required to handle the exception,  there are two types of exceptions: Checked and Unchecked Exceptions.&lt;br /&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: right; margin-left: 1em; text-align: right;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-IJLWhnswgjw/ULuqVNgfY9I/AAAAAAAAAWE/GodUxb0l9rg/s1600/exceptions-heirarchy.jpg&quot; imageanchor=&quot;1&quot; rel=&quot;nofollow&quot; style=&quot;clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Java Exception Class Heirarcy&quot; border=&quot;0&quot; height=&quot;197&quot; src=&quot;http://4.bp.blogspot.com/-IJLWhnswgjw/ULuqVNgfY9I/AAAAAAAAAWE/GodUxb0l9rg/s320/exceptions-heirarchy.jpg&quot; title=&quot;Java Exception Class Heirarchy&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Exception Class Heirarchy&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;strong&gt;Checked Exceptions&lt;/strong&gt;:  These are the type of exceptions for which the compiler checks to ensure that  your code is prepared for handling such exceptions. These are the exceptions  that you can expect to occur frequently and you must handle them in your code.  The conditions that generate such exceptions are generally outside the control  of your program and they can occur in a correct program. But you can anticipate  them and thus you must write code to deal with them. Programmatically, checked  exceptions are the instances of the Exception class or one of its subclasses,  excluding &lt;i&gt;RuntimeException&lt;/i&gt; subtree. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Unchecked Exceptions&lt;/strong&gt;:  The compiler does not check for such type of exceptions. Unchecked Exceptions  comprise of &amp;nbsp;run time exceptions (of type  &lt;i&gt;RuntimeException&lt;/i&gt; or its subclasses) and errors (of type &lt;i&gt;Error&lt;/i&gt; or its subclasses).  Runtime Exceptions occur due to program bugs and include exceptions such as  division by zero and invalid array indexing. You can not recover from an  unchecked exception and you are not required to handle such type of exceptions  either, but still you can do so if you want. &lt;i&gt;ArithmeticException&lt;/i&gt; class is an  example of unchecked exception. Errors are the exceptions that are not expected  to be caught under normal circumstances by your program. They are used by the  Java run time environment to indicate errors related to run time environment  itself. Stack Overflow is an example of error.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;h3&gt;Exception Handling in Java&lt;/h3&gt;We will come across five keywords while learning Exception  Handling in java: &lt;em&gt;try, catch, finally, throw&lt;/em&gt; and &lt;em&gt;throws&lt;/em&gt;. To handle the java  exceptions, try and catch are the two major keywords that you will be using.  Simply enclose the code that is expected to produce some kind of exception by  try block and catch that exception in the catch block immediately following the  try block. Whenever an exception occurs, an object is created and thrown  containing the information about that exception. To catch this thrown block,  you must specify the catch block(s). The catch block receives this thrown  object as its parameter and then you can specify the code to handle this  exception. &lt;br /&gt;&lt;strong&gt;Java Program Example to illustrate the Exception Handling in java.&lt;/strong&gt;&lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;  class DemoExceptionHandling{&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;  int a=5;&lt;br /&gt;  int b=0;&lt;br /&gt;  System.out.println(&quot;We are about to enter in a try block!...&quot;);&lt;br /&gt;  try {&lt;br /&gt;   System.out.println(&quot;a/b=&quot;+a/b);&lt;br /&gt;   System.out.println(&quot;This will not be printed.&quot;);&lt;br /&gt;   } catch (ArithmeticException ae){&lt;br /&gt;    System.out.println(&quot;Exception Caught: &quot;+ae.getMessage());&lt;br /&gt;    }&lt;br /&gt;  System.out.println(&quot;Statement after catch block.&quot;);&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;In the above example ArithmeticException thrown by the try  block is caught by the catch block which is coded to receive ArithmeticException  object. Now what if, if the catch block would contain some other parameter type  than the thrown exception? In that case, the exception thrown will not be  caught due to the absence of appropriate catch block and the program will  terminate without executing any other line from the method. You may create as  many catch blocks as you want, each for receiving the particular types of  exception. Alternatively, you can create a catch block to receive all types of  exceptions by configuring it to receive the object of class Exception itself,  which is the super class of all the exceptions.&lt;br /&gt;One major benefit of having an error signalled by an  exception is that it separates the code that deals with errors from the code  that is executed when things are moving along smoothly. Another positive aspect  of exceptions is that they provide a way of enforcing a response to particular  errors.  &lt;br /&gt;&lt;h3&gt;Use of finally Block in Java Exception Handling&lt;/h3&gt;In case the exception could not be caught, the program will  terminate and no further lines of code will be executed after the line where  the exception occurred. But in some situation you may desire to execute some  lines of code irrespective of the fact whether or not the catch block  corresponding to the try block has been caught. In that you should use finally  block. The finally block is executed almost always even if the exception is not  caught.&lt;br /&gt;&lt;strong&gt;Example Java Program  to illustrate the use of finally block&lt;/strong&gt;&lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;class DemoExceptionHandling{&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;  int a=5;&lt;br /&gt;  int b=0;&lt;br /&gt;  System.out.println(&quot;We are about to enter in a try block!...&quot;);&lt;br /&gt;  try {&lt;br /&gt;   System.out.println(&quot;a/b=&quot;+a/b);&lt;br /&gt;   System.out.println(&quot;This will not be printed.&quot;);&lt;br /&gt;   } catch (ArrayIndexOutOfBoundException oe){&lt;br /&gt;    System.out.println(&quot;Exception Caught: &quot;+oe.getMessage());&lt;br /&gt;    }&lt;br /&gt;    finally{&lt;br /&gt;     System.out.println(&quot;Finally block will execute always!&quot;);&lt;br /&gt;    }&lt;br /&gt;  System.out.println(&quot;Statement after catch and finally block.&quot;);&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;In the above example, the finally block will be executed  always, whether the exception is caught or not. If the exception is caught by  the appropriate catch block, then the execution of the program will continue  after the finally block also. But if the exception in try block has not been  caught by the appropriate catch block, then the control will be transferred to  the finally block and after that the program will terminate.  &lt;br /&gt;&lt;h3&gt;What happens when exception handler is not provided?&lt;/h3&gt;You must be thinking what would happen, if exception handling  is avoided in a java program.&amp;nbsp; When you  do not provide provisions of exception handling, java does it for you by  default. Whenever an exception occurs, the java run-time detects it and creates  an exception object and throws it. Now since there are no exception handlers  created, the thrown exception object will be caught by the default handler  provided by the java run time. The default handler displays a string describing  the exception, prints a stack trace from the point at which the exception occurred,  and terminates the program. So, in order to avoid this immature termination of  program, exception handling is used.&lt;br /&gt;Exceptions should not be overused at all. The reason for  this is that dealing with exceptions involves quite a lot of processing  overhead, so if your program is handling exceptions a lot of the time it will  be a lot slower than it needs to be.    For  Example, a user entering incorrect input to your program for instance is a  normal event and should be handled without recourse to exceptions. &lt;br /&gt;This post is just for the basic introduction of the Exceptions in Java and Handling Exceptions in Java. To know little more about Exception Handling in Java such as nested try blocks, difference between throw and throws and creating your own exception classes, Read &lt;a href=&quot;http://codeinjavase.blogspot.com/2012/12/java-exception-handling-tutorial-2.html&quot; target=&quot;_blank&quot;&gt;Java Exception Handling Tutorial-2&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/2475424498403532188/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=2475424498403532188&amp;isPopup=true' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2475424498403532188'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2475424498403532188'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2012/12/java-exception-handling-tutorial.html' title='Java Exception Handling Tutorial'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-IJLWhnswgjw/ULuqVNgfY9I/AAAAAAAAAWE/GodUxb0l9rg/s72-c/exceptions-heirarchy.jpg" height="72" width="72"/><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-2254650706362794939</id><published>2012-10-02T15:52:00.001+05:30</published><updated>2012-10-02T16:00:53.570+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Cab Invoice Generator Java Project"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java Desktop Application Project"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="core java projects with source code download"/><category scheme="http://www.blogger.com/atom/ns#" term="Invoice Generator Java Project"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Project for beginners"/><category scheme="http://www.blogger.com/atom/ns#" term="Java SE Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="Rent-a-Cab Invoice Generator"/><title type='text'>Core Java Project Desktop Application : Cab Invoice Generator</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Invoice Generator Core Java Desktop Application Using Swing&lt;/span&gt;&lt;/h2&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Description&lt;/h3&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Invoice Generator is a Core Java Project for generating the invoices for a Rent-a-Cab company. It takes the details of the invoices via a form and then calculates the various subtotals, service taxes, rates applicable etc on the basis of choices entered and then generates the invoice for the user.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;It also allows you to Add, Edit or Remove the cabs, define and modify the various rent packages for customers, billing schemes etc. The company owner can change each and every detail about how the invoice will be calculated and generated.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;I have used the MS-Access database for simplicity as this project is intentionally for the beginners in java programming. If you want to run the project, you must have to create the DSN with the name &quot;&lt;b&gt;invoice&lt;/b&gt;&quot; and point it to the database that is provided with this project. Alternatively you can create the DSN with any name but in that case you will have to change the DSN name in each occurence of DSN in the project.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;I have left out the various checks for type of data that can be entered in the fields and other validation checks in almost every forms. So you can download the project source code and do it yourself.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;I will also additional functionalities like PRINT the invoice to this project in a week or two as and when I will get time. But for now it is a good beginner level &lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-projects.html&quot; target=&quot;_blank&quot;&gt;Core Java Project&lt;/a&gt; for you to look at and understand the basic concepts and try some modification of your own. Like I have built it for a Rent-a-Cab company but you can modify it to accomodate the needs of some other firm.&lt;/div&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;I could not proof-check every form and details of the project due to time constrainsts as I built this project for a friend of mine as he needed the same for submitting in some institute. So, I want you to take a look at it and ask your queries and suggestions about what could I have done better in this (except the validation checks, that I have left out intentionally due to lack of time).&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Snapshots&lt;/h3&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-WZuu0obzync/UGq3vCM4A7I/AAAAAAAAAVE/XGp5m49XPTI/s1600/Invoice1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-2e50CcXYbZE/UGq3whubmdI/AAAAAAAAAVM/E0c3hdSjgAQ/s1600/InvoiceForm.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Invoice Generator main form page, core java desktop application&quot; border=&quot;0&quot; height=&quot;301&quot; src=&quot;http://4.bp.blogspot.com/-2e50CcXYbZE/UGq3whubmdI/AAAAAAAAAVM/E0c3hdSjgAQ/s320/InvoiceForm.png&quot; title=&quot;Core Java Project : Invoice Generator for Rent-a-Cab Company&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Main Form Page&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://4.bp.blogspot.com/-meKcWMUd36I/UGq3yNrh9VI/AAAAAAAAAVQ/ydWDqjxxeH0/s1600/SettingsCabs.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Cab Settings for Invoice Generator Core Java Project&quot; border=&quot;0&quot; height=&quot;255&quot; src=&quot;http://4.bp.blogspot.com/-meKcWMUd36I/UGq3yNrh9VI/AAAAAAAAAVQ/ydWDqjxxeH0/s320/SettingsCabs.png&quot; title=&quot;Cab Settings for Invoice Generator Core Java Project&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Settings for Cabs&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-97WNZYOAe0Q/UGq3zHASP2I/AAAAAAAAAVc/fAjoK79NnKQ/s1600/SettingsPackages.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Settings for Invoice Generator Core Java Project&quot; border=&quot;0&quot; height=&quot;254&quot; src=&quot;http://1.bp.blogspot.com/-97WNZYOAe0Q/UGq3zHASP2I/AAAAAAAAAVc/fAjoK79NnKQ/s320/SettingsPackages.png&quot; title=&quot;Settings for Invoice Generator Core Java Project&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Settings Page for Rent Packages&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-WZuu0obzync/UGq3vCM4A7I/AAAAAAAAAVE/XGp5m49XPTI/s1600/InvoiceGenerated.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;invoice core java desktop application&quot; border=&quot;0&quot; height=&quot;240&quot; src=&quot;http://1.bp.blogspot.com/-WZuu0obzync/UGq3vCM4A7I/AAAAAAAAAVE/XGp5m49XPTI/s320/Invoice1.png&quot; title=&quot;Invoice Generated Core Java Project&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Generated Invoice&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Download Project With Source Code and Database:&lt;/h4&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;a href=&quot;https://www.box.com/s/4rwm77v2t2awj5a30ktb&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Download Cab Invoice Generator Jar File &lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;a href=&quot;https://www.box.com/s/knazxlaqxgae4hjdfun8&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Download Associated Database File&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;a href=&quot;https://www.box.com/s/7w0ox9x9u2efjvc76pju&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Download All-in-One Zip File with Source Code and Database&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;More &lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-projects.html&quot;&gt;Core Java Projects&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/2254650706362794939/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=2254650706362794939&amp;isPopup=true' title='42 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2254650706362794939'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2254650706362794939'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2012/10/invoicegeneratorcorejavaproject.html' title='Core Java Project Desktop Application : Cab Invoice Generator'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-2e50CcXYbZE/UGq3whubmdI/AAAAAAAAAVM/E0c3hdSjgAQ/s72-c/InvoiceForm.png" height="72" width="72"/><thr:total>42</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-3149931990920394355</id><published>2012-06-23T00:11:00.001+05:30</published><updated>2012-12-01T09:54:29.421+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Connectivity"/><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><category scheme="http://www.blogger.com/atom/ns#" term="Oracle"/><category scheme="http://www.blogger.com/atom/ns#" term="Resultset"/><category scheme="http://www.blogger.com/atom/ns#" term="Statement"/><title type='text'>JDBC : Connection to databases</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2&gt;  What is JDBC?&lt;/h2&gt;&lt;div&gt;&lt;div&gt;JDBC is an API that encapsulates the low−level calls needed for database access and interaction into one common interface.&amp;nbsp;Both the Java Development Kit (JDK) and Java Runtime Environment (JRE) contain the API as part of the standard distribution. The API’s interfaces and classes reside in the java.sql and javax.sql packages. The standard components are packaged in java.sql while the enterprise elements are in javax.sql.&lt;br /&gt;&lt;br /&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt; Steps for building a simple JDBC application&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;1. Import Packages&lt;/div&gt;&lt;div&gt;2. Register JDBC Driver&lt;/div&gt;&lt;div&gt;3. Open a Connection&lt;/div&gt;&lt;div&gt;4. Execute a Query&lt;/div&gt;&lt;div&gt;5. Extract data from Result Set&lt;/div&gt;&lt;div&gt;6. Clean-Up Environment&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt; A Simple Program to demonstrate the JDBC application working:&lt;/h4&gt;&lt;div&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;This is a simple program that retrieves &amp;nbsp;and displays the values of particular columns from the Employees table stored in the database.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;brush: java&quot;&gt;&lt;br /&gt;//STEP 1. Import packages&lt;br /&gt;&lt;br /&gt;import java.sql.DriverManager;&lt;br /&gt;import java.sql.Connection;&lt;br /&gt;import java.sql.Statement;&lt;br /&gt;import java.sql.ResultSet;&lt;br /&gt;import java.sql.Date;&lt;br /&gt;import java.sql.SQLException;&lt;br /&gt;&lt;br /&gt;public class FirstQuery {&lt;br /&gt;   public static void main(String[] args) {&lt;br /&gt;   Connection conn = null;     //Define Connection variable&lt;br /&gt;   try{                        //Begin standard error handling&lt;br /&gt;&lt;br /&gt;//STEP 2: Register JDBC driver&lt;br /&gt;&lt;br /&gt;     String driver = &quot;oracle.jdbc.driver.OracleDriver&quot;; //for oracle database&lt;br /&gt;     Class.forName(driver);&lt;br /&gt;//STEP 3: Open a connection&lt;br /&gt;     System.out.println(&quot;Connecting to database...&quot;);&lt;br /&gt;     String jdbcUrl = &quot;jdbc:oracle:thin:@localhost:1521:XE&quot;;&lt;br /&gt;/* this url will be different for the different databases */&lt;br /&gt;     String user = &quot;username&quot;; &lt;br /&gt;     String password = &quot;password&quot;;&lt;br /&gt;     conn = DriverManager.getConnection(jdbcUrl,user,password);&lt;br /&gt;&lt;br /&gt;//STEP 4: Execute a query&lt;br /&gt;&lt;br /&gt;     Statement stmt = conn.createStatement();&lt;br /&gt;     String sql;&lt;br /&gt;     sql = &quot;SELECT SSN, Name, Salary, Hiredate FROM Employees&quot;;&lt;br /&gt;     ResultSet rs = stmt.executeQuery(sql);&lt;br /&gt;&lt;br /&gt;//STEP 5: Extract data from result set&lt;br /&gt;&lt;br /&gt;     while(rs.next()){&lt;br /&gt;//Retrieve by column name&lt;br /&gt;&lt;br /&gt;     int ssn= rs.getInt(&quot;ssn&quot;);&lt;br /&gt;     String name = rs.getString(&quot;name&quot;);&lt;br /&gt;//Retrieve by column index as an example&lt;br /&gt;&lt;br /&gt;     double salary = rs.getDouble(3);&lt;br /&gt;     Date date = rs.getDate(4);&lt;br /&gt;&lt;br /&gt;//Display values&lt;br /&gt;&lt;br /&gt;     System.out.print(&quot;SSN: &quot; + ssn);&lt;br /&gt;     System.out.print(&quot;, Name: &quot; + name);&lt;br /&gt;     System.out.print(&quot;, Salary: $&quot; + salary);&lt;br /&gt;     System.out.println(&quot;, HireDate: &quot; + date);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;//STEP 6: Clean−up environment&lt;br /&gt;&lt;br /&gt;  rs.close();&lt;br /&gt;  stmt.close();&lt;br /&gt;  conn.close();&lt;br /&gt; }catch(SQLException se){&lt;br /&gt;&lt;br /&gt;//Handle errors for JDBC&lt;br /&gt;&lt;br /&gt; se.printStackTrace();&lt;br /&gt; }catch(Exception e){&lt;br /&gt;&lt;br /&gt;//Handle errors for Class.forName&lt;br /&gt;&lt;br /&gt; e.printStackTrace();&lt;br /&gt; }finally{&lt;br /&gt;&lt;br /&gt;//finally block used to close resources&lt;br /&gt;&lt;br /&gt; try{&lt;br /&gt;       if(conn!=null)&lt;br /&gt;           conn.close();&lt;br /&gt;&lt;br /&gt;   }catch(SQLException se){&lt;br /&gt;       se.printStackTrace();&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt; System.out.println(&quot;Finished!&quot;);&lt;br /&gt;}      //end of main&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt; The Expected Output after you have created the databse with some values of these columns:&lt;/h4&gt;&lt;div&gt;&lt;div&gt;Connecting to database...&lt;/div&gt;&lt;div&gt;SSN: 111111111, Name: Harish, Salary: $5000.55, HireDate: 2010−09−16&lt;/div&gt;&lt;div&gt;SSN: 419876541, Name: Vikas, Salary: $1500.75, HireDate: 2011−03−05&lt;/div&gt;&lt;div&gt;SSN: 312654987, Name: Aman, Salary: $2000.95, HireDate: 2012−01−11&lt;/div&gt;&lt;div&gt;SSN: 123456789, Name: Sumit, Salary: $3080.05, HireDate: 2009−09−07&lt;/div&gt;&lt;div&gt;SSN: 987654321, Name: John, Salary: $4351.27, HireDate: 2011−12−31&lt;/div&gt;&lt;div&gt;Finished!&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The values that you will store in the databse will be shown.&lt;br /&gt;&lt;br /&gt;This was just a simple demonstration program to just give some idea about the JDBC connectivity.&lt;br /&gt;In the later posts, the more detailed and programs for entering the data in the databse, updating and creating the database using either Statement or preparedStatement objects.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/3149931990920394355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=3149931990920394355&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/3149931990920394355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/3149931990920394355'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2012/06/jdbc-connection-to-databases.html' title='JDBC : Connection to databases'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-66398968329956952</id><published>2011-08-19T02:20:00.001+05:30</published><updated>2012-12-06T23:43:59.863+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="Download Java Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="Project Reports"/><title type='text'>New Core Java Projects Added</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;I have collected some of the best &lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-projects.html#.Tk16PV0erTo&quot;&gt;Core Java Projects&lt;/a&gt; for you to work upon.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;There are six Core Java Projects synopsis cum project report that will help you develop the project. All the projects can be developed in Java SE. &amp;nbsp;You may use any IDE of your choice but I would prefer Netbeans 7 as I find it more comfortable and easy to use even though it eats up more memory!&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;I have studied all the project reports in detail but I haven&#39;t developed them all. I have only collected them from the different sources and have put them together for your convenience.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;If you have any difficulties in developing on the following given projects, you may &lt;a href=&quot;http://codeinjavase.blogspot.com/p/contact-us.html&quot;&gt;contact me&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Your feedback is important so do join &lt;a href=&quot;http://codeinjavase.blogspot.com/&quot;&gt;this blog&lt;/a&gt; and forget not to write in your ideas, suggestions and problems as comments.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-projects.html#.Tk16PV0erTo&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Core Java Projects (Synopsis)&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;a href=&quot;http://codeinjavase.blogspot.com/2012/10/invoicegeneratorcorejavaproject.html&quot;&gt;Cab Invoice Generator Core Java Desktop Application with source code&lt;/a&gt; &lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://www.box.net/shared/dho04fk9vty0cumux02m&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Migration Tool-kit&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://www.box.net/shared/pytz4kf7089da7i08eoa&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Hardware Sales and Servicing Information System&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;a href=&quot;http://www.box.net/shared/fe1ull96lb85la78xkpm&quot;&gt;Face Identification Software&lt;/a&gt; ***&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://www.box.net/shared/uo3g3nvynj17qx82nh8p&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Digital Signature System&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://www.box.net/shared/yiepvkahtu50ym4g87xf&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Power Distribution System&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;a href=&quot;http://www.box.net/shared/cq8yfvf8yh1kbo9h1gnx&quot;&gt;Finger Print Recognition System&lt;/a&gt; ***&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;(&lt;b&gt;***&lt;/b&gt;Difficulty Level is High and You will also be required additional Hardware Resources to build these projects)&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/66398968329956952/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=66398968329956952&amp;isPopup=true' title='25 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/66398968329956952'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/66398968329956952'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/08/new-core-java-projects-added.html' title='New Core Java Projects Added'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>25</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-4723270868807248672</id><published>2011-07-27T16:05:00.001+05:30</published><updated>2012-12-01T18:38:58.448+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="access protection in packages"/><category scheme="http://www.blogger.com/atom/ns#" term="creating and using the package"/><category scheme="http://www.blogger.com/atom/ns#" term="package"/><title type='text'>Packages in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b style=&quot;color: #134f5c;&quot;&gt;Java Packages&lt;/b&gt;&lt;/h2&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;color: #134f5c;&quot;&gt;What is a Java Package&lt;/span&gt;&lt;/h4&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;A package is a collection of classes and the related functions within the classes.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;Packages are container for classes that are used to keep the class name space compartmentalized. For example, you can create a class named “Abc” in your package “mypackage” without being concerned to it being colliding with other class also named as “Abc”.&amp;nbsp; And then you can write your own methods in it for the later use by you or anyone that uses that package.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;Packages are stored in a hierarchical way. That means you can create a package inside a package. This is like a box contained inside a box.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;Now how to create a package? Well this is simple, you just need to use the keyword ‘package’ followed by the name of the package.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;This is the general form of the package statement:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;package mypackage;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;Here &lt;i&gt;package&lt;/i&gt; is the keyword for creating the package called &lt;i&gt;mypackage.&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;color: #1d1d1e;&quot;&gt;Java uses file system directories to store packages. For example, the &lt;b&gt;.class &lt;/b&gt;files for any&lt;/span&gt;&lt;span style=&quot;color: #1d1d1e;&quot;&gt;&amp;nbsp;classes you declare to be part of &lt;/span&gt;&lt;b style=&quot;color: #1d1d1e;&quot;&gt;mypackage &lt;/b&gt;&lt;span style=&quot;color: #1d1d1e;&quot;&gt;must be stored in a directory called &lt;/span&gt;&lt;b style=&quot;color: #1d1d1e;&quot;&gt;mypackage&lt;/b&gt;&lt;span style=&quot;color: #1d1d1e;&quot;&gt;. Remember that case is significant, and the directory name must match the package name exactly.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-family: inherit;&quot;&gt;You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #1d1d1e; font-family: inherit;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-family: inherit;&quot;&gt;package &lt;i&gt;pkg1&lt;/i&gt;[.&lt;i&gt;pkg2&lt;/i&gt;[.&lt;i&gt;pkg3&lt;/i&gt;]];&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-family: inherit;&quot;&gt;A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;i&gt;&lt;span style=&quot;color: #1d1d1e; font-family: inherit;&quot;&gt;package java.awt.image&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-family: inherit;&quot;&gt;needs to be stored in &lt;b&gt;java/awt/image&lt;/b&gt;, &lt;b&gt;java\awt\image&lt;/b&gt;, or &lt;b&gt;java:awt:image &lt;/b&gt;on your UNIX, Windows, or Macintosh file system, respectively. Be sure to choose your package names carefully. You cannot rename a package without renaming the directory in which the classes are stored.&lt;i&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #1d1d1e;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #134f5c; font-family: inherit;&quot;&gt;How to Use the package&lt;/span&gt;&lt;/h4&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;Now after you have created the package, you would want to use it in your program. For that you must import the package in your program.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;Suppose if you created the above package mypackage, then you can import it by using import statement.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;import mypackage.*;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: inherit; line-height: 115%;&quot;&gt;The above import statement will import all the classes inside the mypackage package. If you want to import only specific class, you can import it by specifying the class name instead of *.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;import mypackage.MyClass;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;EXAMPLE PROGRAM&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;&lt;/div&gt;&lt;pre class=&quot;brush: java&quot; style=&quot;text-align: left;&quot;&gt;//creating the package&lt;br /&gt;//save each class of the package in a separate file named as the name of the //class e.g. save this file as “Calculation.java”&lt;br /&gt;package mypackage;&lt;br /&gt;public class Calculation&lt;br /&gt;{&lt;br /&gt;            public int Add(int x, int y)&lt;br /&gt;            {&lt;br /&gt;                        return(x+y);&lt;br /&gt;            }&lt;br /&gt;            public float Add(float x, float y)&lt;br /&gt;            {&lt;br /&gt;                        return(x+y);&lt;br /&gt;            }&lt;br /&gt;            public int div(int x, int y)&lt;br /&gt;            {&lt;br /&gt;                        return(x/y);&lt;br /&gt;            }&lt;br /&gt;            public float div(float x, float y)&lt;br /&gt;            {&lt;br /&gt;                        return(x/y);&lt;br /&gt;            }&lt;br /&gt;}&lt;br /&gt;Now Create another class in which you will be using this package.&lt;br /&gt;//using my package&lt;br /&gt;// save the file as “UsingPackage.java”&lt;br /&gt;import mypackage.Calculation;&lt;br /&gt;import java.util.*;&lt;br /&gt;class UsingPackage&lt;br /&gt;{&lt;br /&gt;            public static void main(String args[])&lt;br /&gt;            {&lt;br /&gt;                        Scanner s = new Scanner(System.in);&lt;br /&gt;                        Calculation c = new Calculation();&lt;br /&gt;                        System.out.println(&quot;Enter two Numbers:&quot;);&lt;br /&gt;                        int x = s.nextInt();&lt;br /&gt;                        int y = s.nextInt();&lt;br /&gt;                        System.out.println(c.Add(x,y));&lt;br /&gt;                        System.out.println(&quot;Enter two Numbers:&quot;);&lt;br /&gt;                        float a = s.nextFloat();&lt;br /&gt;                        float b = s.nextFloat();&lt;br /&gt;                        System.out.println(c.Add(a,b));&lt;br /&gt;            }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;text-align: left;&quot;&gt;&lt;h4&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: inherit; font-weight: normal;&quot;&gt;ACCESS PROTECTION&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;The following table applies only to the members of the class. A class can only have two access modifiers : default and public.&lt;/span&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableLightShadingAccent1&quot; style=&quot;border-collapse: collapse; border: none; mso-border-bottom-alt: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-alt: solid #4F81BD 1.0pt; mso-border-top-themecolor: accent1; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;   &lt;td style=&quot;border-bottom: solid #4F81BD 1.0pt; border-left: none; border-right: none; border-top: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 189.7pt;&quot; valign=&quot;top&quot; width=&quot;253&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 5;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border-bottom: solid #4F81BD 1.0pt; border-left: none; border-right: none; border-top: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.85pt;&quot; valign=&quot;top&quot; width=&quot;94&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 1;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Private&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border-bottom: solid #4F81BD 1.0pt; border-left: none; border-right: none; border-top: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.9pt;&quot; valign=&quot;top&quot; width=&quot;95&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 1;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Default&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border-bottom: solid #4F81BD 1.0pt; border-left: none; border-right: none; border-top: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 77.95pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 1;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Protected &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border-bottom: solid #4F81BD 1.0pt; border-left: none; border-right: none; border-top: solid #4F81BD 1.0pt; mso-border-bottom-themecolor: accent1; mso-border-top-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 52.7pt;&quot; valign=&quot;top&quot; width=&quot;70&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 1;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Public&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 189.7pt;&quot; valign=&quot;top&quot; width=&quot;253&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 68;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Same Class&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.85pt;&quot; valign=&quot;top&quot; width=&quot;94&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.9pt;&quot; valign=&quot;top&quot; width=&quot;95&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 77.95pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 52.7pt;&quot; valign=&quot;top&quot; width=&quot;70&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 189.7pt;&quot; valign=&quot;top&quot; width=&quot;253&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 4;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Same Package   Subclass&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.85pt;&quot; valign=&quot;top&quot; width=&quot;94&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.9pt;&quot; valign=&quot;top&quot; width=&quot;95&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 77.95pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 52.7pt;&quot; valign=&quot;top&quot; width=&quot;70&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 189.7pt;&quot; valign=&quot;top&quot; width=&quot;253&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 68;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Same package   non-subclass&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.85pt;&quot; valign=&quot;top&quot; width=&quot;94&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.9pt;&quot; valign=&quot;top&quot; width=&quot;95&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 77.95pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; padding: 0cm 5.4pt 0cm 5.4pt; width: 52.7pt;&quot; valign=&quot;top&quot; width=&quot;70&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 189.7pt;&quot; valign=&quot;top&quot; width=&quot;253&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 4;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Different   Package Subclass&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.85pt;&quot; valign=&quot;top&quot; width=&quot;94&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.9pt;&quot; valign=&quot;top&quot; width=&quot;95&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 77.95pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;border: none; padding: 0cm 5.4pt 0cm 5.4pt; width: 52.7pt;&quot; valign=&quot;top&quot; width=&quot;70&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;   &lt;td style=&quot;background: #D3DFEE; border-bottom: solid #4F81BD 1.0pt; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; mso-border-bottom-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 189.7pt;&quot; valign=&quot;top&quot; width=&quot;253&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 68;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;Different   package non-Subclass&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border-bottom: solid #4F81BD 1.0pt; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; mso-border-bottom-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.85pt;&quot; valign=&quot;top&quot; width=&quot;94&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border-bottom: solid #4F81BD 1.0pt; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; mso-border-bottom-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 70.9pt;&quot; valign=&quot;top&quot; width=&quot;95&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border-bottom: solid #4F81BD 1.0pt; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; mso-border-bottom-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 77.95pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;No&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;   &lt;td style=&quot;background: #D3DFEE; border-bottom: solid #4F81BD 1.0pt; border: none; mso-background-themecolor: accent1; mso-background-themetint: 63; mso-border-bottom-themecolor: accent1; padding: 0cm 5.4pt 0cm 5.4pt; width: 52.7pt;&quot; valign=&quot;top&quot; width=&quot;70&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-yfti-cnfc: 64;&quot;&gt;&lt;span style=&quot;color: #365f91; font-size: 16pt;&quot;&gt;&lt;b&gt;Yes&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;We can alter the access protection of the members and the classes in the package according to the need.&lt;/span&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/4723270868807248672/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=4723270868807248672&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/4723270868807248672'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/4723270868807248672'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/07/packages-in-java.html' title='Packages in Java'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-5592639489243580743</id><published>2011-07-14T23:57:00.001+05:30</published><updated>2012-09-21T15:04:32.920+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Constructors in Java"/><title type='text'>Constructors in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-size: large;&quot;&gt;&lt;b&gt;Constructors&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;A &lt;i&gt;constructor &lt;/i&gt;initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the &lt;b&gt;new &lt;/b&gt;operator completes. Constructors look a little strange because they have no return type, not even &lt;b&gt;void&lt;/b&gt;. This is because the implicit return type of a class’ constructor is the class type itself. It is the constructor’s job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;EXAMPLE:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;/* Here, Box uses a constructor to initialize the dimensions of a box.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;*/&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;class Box {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double width;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double height;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif; font-size: 19px;&quot;&gt;double depth;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// This is the constructor for Box.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;Box() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;System.out.println(&quot;Constructing Box&quot;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;width = 10;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;height = 10;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;depth = 10;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// compute and return volume&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double volume() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;return width * height * depth;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;class BoxDemo6 {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;public static void main(String args[]) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// declare, allocate, and initialize Box objects&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;Box mybox1 = new Box();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;Box mybox2 = new Box();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double vol;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// get volume of first box&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;vol = mybox1.volume();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;System.out.println(&quot;Volume is &quot; + vol);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// get volume of second box&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;vol = mybox2.volume();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;System.out.println(&quot;Volume is &quot; + vol);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #1d1d1e;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;OUTPUT:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;u&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;Constructing Box&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;Constructing Box&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;Volume is 1000.0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;Volume is 1000.0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #134f5c; font-size: large;&quot;&gt;Parameterized Constructors&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;While the &lt;b&gt;Box( ) &lt;/b&gt;constructor in the preceding example does initialize a &lt;b&gt;Box &lt;/b&gt;object, it is not very useful—all boxes have the same dimensions. What is needed is a way to construct &lt;b&gt;Box &lt;/b&gt;objects of various dimensions. The easy solution is to add parameters to the constructor. As you can probably guess, this makes them much more useful. For example, the following version of &lt;b&gt;Box &lt;/b&gt;defines a parameterized constructor which sets the dimensions of a box as specified by those parameters. Pay special attention to how &lt;b&gt;Box &lt;/b&gt;objects are created.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;EXAMPLE&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;:&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;/* Here, Box uses a parameterized constructor to initialize the dimensions of a box.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;*/&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;class Box {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double width;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double height;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double depth;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;THE JAVA LANGUAGE&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// This is the constructor for Box.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;Box(double w, double h, double d) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;width = w;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;height = h;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;depth = d;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// compute and return volume&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double volume() {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;return width * height * depth;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;class BoxDemo7 {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;public static void main(String args[]) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// declare, allocate, and initialize Box objects&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;Box mybox1 = new Box(10, 20, 15);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;Box mybox2 = new Box(3, 6, 9);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;double vol;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// get volume of first box&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;vol = mybox1.volume();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;System.out.println(&quot;Volume is &quot; + vol);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;// get volume of second box&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;vol = mybox2.volume();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;System.out.println(&quot;Volume is &quot; + vol);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #1d1d1e;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;The output from this program is shown here:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;Volume is 3000.0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #1d1d1e; font-size: 14pt;&quot;&gt;Volume is 162.0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/5592639489243580743/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=5592639489243580743&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/5592639489243580743'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/5592639489243580743'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/07/constructors-in-java.html' title='Constructors in Java'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-6168755762051381967</id><published>2011-07-14T23:52:00.001+05:30</published><updated>2012-09-29T17:23:13.660+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Command Line Arguements"/><category scheme="http://www.blogger.com/atom/ns#" term="use of command line arguements in java"/><category scheme="http://www.blogger.com/atom/ns#" term="using command line arguements"/><title type='text'>Using the Command Line Arguements</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Command Line Arguements&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Sometimes, there might arise a need for a program to run it in accordance with the specific input provided at the time of running the program. Command line arguements serve this purpose in java by allowing the user to input some values at the time of running the java program.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;As the name implies, this feature is for only the programs which run on/from the command line interface. The input provided at the command line along with the instruction of running the java application, is stored in a String array and can be used in the program to make changes in the way program will behave.&lt;/span&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Syntax:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;They are directly passed to the main() of the java program.&lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt; &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-size: small;&quot;&gt;&lt;i&gt;public static void main(String args[])&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;the args[] is an array of strings, and it is called as command line arguements because the value of args[] can only be given at the run time from the command line. But wonder how to use it in your program?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;This is simple, take a loot at the demo code that makes use of the command line arguements.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-size: small;&quot;&gt;//Demo Code for using the Command Line Arguements in your program&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-size: small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;class CommandLineArgs&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public static void &amp;nbsp;main(String args[])&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;int sum = 0;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(int i=0;i&amp;lt;args.length;i++)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;sum = sum + Integer.parseInt(args[i]);&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;Sum is &quot;+sum);&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large; text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;You might want to read or download the chapter wise &lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-tutorials.html&quot; target=&quot;_blank&quot;&gt;Java SE tutorials&lt;/a&gt;.&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: x-large;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/6168755762051381967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=6168755762051381967&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6168755762051381967'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6168755762051381967'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/07/using-command-line-arguements.html' title='Using the Command Line Arguements'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-2340847801223809376</id><published>2011-07-14T23:33:00.001+05:30</published><updated>2011-07-29T00:49:34.583+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="attributes and methods"/><category scheme="http://www.blogger.com/atom/ns#" term="declaring classes"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Program Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Source File Layout"/><title type='text'>Structure of Java Program</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #134f5c; font-size: large;&quot;&gt;&lt;b&gt;Source File Layout/ Java Program Structure&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;A Java Technology source file takes the following form:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;[&amp;lt;package_declaration&amp;gt;]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&amp;lt;import_declaration&amp;gt;*&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&amp;lt;class_declaration&amp;gt;+&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;NOTE: &lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;- The plus (+) indicates one or more. To be meaningful, a source file must contain at least one class definition.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;1.&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;The order of the items is important. That is, any import statements must precede all class declarations and, if you use a package declaration, it must precede both the classes and imports.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;2.&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;The name of the source file must be the same as the name of the public class declaration in that file. A source file can include more than one class declaration, but only one class can be declared public.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -18.0pt;&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;3.&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;If a source file contains no public class declarations, then the name of the source file is not restricted. However, it is good practice to have one source file for every class declaration and the name of the file is identical to the name of the class.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-size: large;&quot;&gt;&lt;b&gt;Declaring Classes in Java &lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;The Java Technology class declaration takes the following form:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;modifier&amp;gt;* &lt;b&gt;class&lt;/b&gt; &amp;lt;class_name&amp;gt; &lt;b&gt;{&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;attribute_declaration&amp;gt;*&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;constructor_declaration&amp;gt;*&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;method_declaration&amp;gt;*&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3d85c6;&quot;&gt;}&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;Example&lt;/span&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;public class Vehicle&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int x,y;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //declaring methods&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setValues(int a, int b)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //declaring methods&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = a;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y = b;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-size: large;&quot;&gt;Declaring Attributes&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;The declaration of an object attribute takes the following form:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&amp;lt;modifier&amp;gt;* &amp;lt;type&amp;gt; &amp;lt;name&amp;gt; [= &amp;lt;initial_value&amp;gt;];&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;Example:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;public class Foo&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int x;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private float y = 1000.0F;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private String name = “Harish Sharma”;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;border-bottom: solid #4F81BD 1.0pt; border: none; mso-border-bottom-themecolor: accent1; mso-element: para-border-div; padding: 0cm 0cm 4.0pt 0cm;&quot;&gt;&lt;div class=&quot;MsoTitle&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-size: large;&quot;&gt;&lt;b&gt;Declaring Methods&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;The method declaration in Java is similar to that used in C and C++.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;The declaration takes the following basic form:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&amp;lt;modifier&amp;gt;* &amp;lt;return_type&amp;gt; &amp;lt;name&amp;gt; (&amp;lt;arguement&amp;gt;*)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;statement&lt;b&gt;&amp;gt;*;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e;&quot;&gt;}&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;Example:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;public class Dog&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int weight;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int getWeight()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return weight;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setWeight(int newWeight)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If(newWeight&amp;gt;0)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Weight = newWeight;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-size: large;&quot;&gt;&lt;b&gt;Accessing Object Members&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-size: large;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;Example&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;public class AccessObjects&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public static void main(String arg[])&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Dog d = new Dog();&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;Dog d&#39;s weight is&quot;+ d.getWeight());&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;d.setWeight(45);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;Dog d&#39;s weight is&quot;+ d.getWeight());&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;d.setWeight(-45);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;Dog d&#39;s weight is&quot;+ d.getWeight());&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #45818e; font-family: Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/2340847801223809376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=2340847801223809376&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2340847801223809376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/2340847801223809376'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/07/structure-of-java-program.html' title='Structure of Java Program'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-7592117938461001576</id><published>2011-07-09T14:07:00.001+05:30</published><updated>2011-07-29T00:50:17.039+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="First java Program"/><category scheme="http://www.blogger.com/atom/ns#" term="naming conventions in java"/><category scheme="http://www.blogger.com/atom/ns#" term="Running and compiling the java code"/><title type='text'>First Java Program</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Getting Started with Java: Here is a simple Java program that just displays a custom message on screen say “Hello this is my first Java Program”.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;I first write the program and then discuss the various parts of it.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;b style=&quot;color: #38761d;&quot;&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;//first java program&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;class FirstJava&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: #38761d;&quot;&gt; &lt;/b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;public static void main(String args[])&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; System.out.println(&quot;This is my first java program&quot;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Compile the code:&lt;/span&gt;&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;first save the program with the name &quot;FirstJava.java&quot;.If you wrote the code using notepad then save the filename is double quotes otherwise the program will be saved as FirstJava.java.txt which will be a txt file not a .java file.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Then go to command prompt by typing cmd in the Run dialog box or in the search bar.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Now at the command prompt:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Change the directory using cd command to the directory where you saved the file FirstJava.java&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;then type javac FirstJava.java&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;if the code contains errors the list of errors will be displayed, otherwise the command prompt will appear.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;It means that the code has been compiled and it contains no errors.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;You can also check that a file FirstJava.class has been created in the directory where you saved your program. This is because when you compile the code the Java compiler converts the source code into intermediate &quot;Byte Code&quot; which is saved in a .class file.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;And this Byte code can be run on any platform thus facilitating the &lt;b&gt;Platform In-dependency&lt;/b&gt; feature of Java.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;b style=&quot;color: #38761d;&quot;&gt;Running the code:&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;To Run the code:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;type java FirstJava at the command from the same directory where you have saved the file.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;The output will appear as:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; This is my first java program&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;Naming Conventions to Follow in Java&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;/span&gt;&lt;b&gt;For Naming Methods:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Follow &lt;b&gt;Camel Rule : &lt;/b&gt;The rule is that function name must begin with a small letter and if the function name consists of two or more words, then the first letter of every other word must begin with a Capital letter.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Examples of some method names:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;display()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;displayAge()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;showMyName()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;createVariableLengthArray()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;For Naming Classes:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Follow Pascal Rule : The rule is that a class name must begin with a capital letter and if the name of the class contains two or more than two words then first letter of every word must be capital.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Examples of some class names&lt;b&gt;:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Demo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;MyDate&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Addition&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;For Naming Variables:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;Variable naming follows the same rule as methods naming.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;line-height: 115%;&quot;&gt;But name for a constant variable must be in all capitals.&lt;b&gt;&amp;nbsp;&lt;/b&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size: 14pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/7592117938461001576/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=7592117938461001576&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/7592117938461001576'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/7592117938461001576'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/07/first-java-program.html' title='First Java Program'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-6055214224885859967</id><published>2011-07-09T13:26:00.001+05:30</published><updated>2012-11-02T01:16:40.148+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="How to"/><category scheme="http://www.blogger.com/atom/ns#" term="How to install java"/><category scheme="http://www.blogger.com/atom/ns#" term="How to set environment variables for java"/><category scheme="http://www.blogger.com/atom/ns#" term="Installing Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Environment Variables"/><category scheme="http://www.blogger.com/atom/ns#" term="Need for setting environment variables in java"/><category scheme="http://www.blogger.com/atom/ns#" term="Setting the Environment Variables"/><title type='text'>Installing the JDK and Setting the Environment Variables</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Want to &lt;span style=&quot;font-size: large;&quot;&gt;get started with Java and set u&lt;span style=&quot;font-size: large;&quot;&gt;p your computer &lt;span style=&quot;font-size: large;&quot;&gt;for Java, then this &lt;span style=&quot;font-size: large;&quot;&gt;post is for you only. &lt;span style=&quot;font-size: large;&quot;&gt;Every Java programmer I know starts off &lt;span style=&quot;font-size: large;&quot;&gt;with writing &lt;span style=&quot;font-size: large;&quot;&gt;the Java programs in any text editor &lt;span style=&quot;font-size: large;&quot;&gt;such &lt;span style=&quot;font-size: large;&quot;&gt;Notepad and compile and run it usin&lt;span style=&quot;font-size: large;&quot;&gt;g &lt;span style=&quot;font-size: large;&quot;&gt;Ja&lt;span style=&quot;font-size: large;&quot;&gt;va tools on command line. But to make sure al&lt;span style=&quot;font-size: large;&quot;&gt;l goes well, you must need to in&lt;span style=&quot;font-size: large;&quot;&gt;stall &lt;span style=&quot;font-size: large;&quot;&gt;Java properly and Set u&lt;span style=&quot;font-size: large;&quot;&gt;p the &lt;span style=&quot;font-size: large;&quot;&gt;environment variables&lt;span style=&quot;font-size: large;&quot;&gt; for Java.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Installing the Java 2 SDK (or JDK)&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;If you don’t have the Java 2 SDK(Software Development Kit) or simply JDK installed on your system, Follow these steps to install the JDK on your system:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;1. &lt;a href=&quot;http://www.oracle.com/technetwork/java/javase/downloads/index.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Download the latest Java SDK&lt;/a&gt; version from the Oracle official site&lt;span style=&quot;font-size: large;&quot;&gt;.&lt;/span&gt;&lt;a href=&quot;http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u26-download-400750.html&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;2. Now browse the jdk setup file on your computer  and double-click on the setup file to open it. Now follow the on screen instructions to install the JDK on your system. JRE is also installed along with the JDK.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;3. You can check if the JDK has been correctly installed or not by looking in the C:\Program Files\Java\. You must see the jdk1.6.0_26 folder and jre1.6.0_26 folder in it. 1.6.0_26 is the version of the jdk and it can be different depending upon which version did you download from the site.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;4. Now you have successfully installed the JDK, the next step is to set the Environment Variables for your Java (&lt;span style=&quot;font-size: large;&quot;&gt;JDK&lt;/span&gt;).&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Setting up the Environment Variables&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;1. Right click on My Computer and click on “Properties”. Then in &lt;/span&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span style=&quot;font-size: large;&quot;&gt;the Computer Properties window, on the left panel click on the “Advanced System Settings”, this will open the new “System Properties window.”&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;2. Go to Advanced tab and right down the window, click on the “Environment Variables”.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;3. Now in the Environment Variables box, either you can set the variables for local user or for System variables. Local variables will only work for the current user and the System Variables will work for all the users.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;4. Now for creating the variable for System Variables so that all the user can use the jdk, Find the “Path” variable in the list and click on Edit button.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;5. Now place the &lt;b&gt;;&lt;/b&gt; followed by the path to the bin folder inside your jdk. For example&amp;nbsp; &quot;&lt;i&gt;;C:\Program Files\Java\jdk1.6.0_07\bin &quot;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;6. For local variable, you must create the PATH variable by pressing “New” button and setting its value as &quot; &lt;i&gt;;C:\Program Files\Java\jdk1.6.0_07\bin;&lt;/i&gt; &quot;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;7. Now in the local variables, create a New variable “classpath” and set its value to “&lt;b&gt;.&lt;/b&gt;” i.e. a single dot.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;8. Now you are done and all ready to use the jdk for creating the java applications.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Need for Setting Environment Variables&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div style=&quot;color: #38761d;&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Need for setting PATH variable:&lt;/b&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Setting the PATH variable enables the user to use the java tools given in the bin folder from any directory on your system.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;If you don’t set this variable, you can only access those tools from the bin directory only.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;color: #38761d;&quot;&gt;&lt;i&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Need for Setting the &lt;span style=&quot;font-size: large;&quot;&gt;CLASSPATH&lt;/span&gt; variable:&lt;/b&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Setting the &lt;span style=&quot;font-size: large;&quot;&gt;CLASSPATH&lt;/span&gt; variable enables your class definition to be found across all the directories.&lt;span style=&quot;font-size: large;&quot;&gt; &lt;/span&gt;If you don’t set this variable, the program source will compile fine but will show the exception at run time that class definition not found.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;So by setting the classpath variable to “&lt;b&gt;.&lt;/b&gt;” &lt;span style=&quot;font-size: large;&quot;&gt;e&lt;/span&gt;nsures that current working directory is included in the list to found the class definition.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Now&lt;span style=&quot;font-size: large;&quot;&gt;, as you have &lt;span style=&quot;font-size: large;&quot;&gt;set u&lt;span style=&quot;font-size: large;&quot;&gt;p &lt;span style=&quot;font-size: large;&quot;&gt;the environment variables&lt;span style=&quot;font-size: large;&quot;&gt;, you are all set to &lt;a href=&quot;http://codeinjavase.blogspot.com/2011/07/first-java-program.html&quot;&gt;write your first java program&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;After that I recommend you to &lt;span style=&quot;font-size: large;&quot;&gt;go through the &lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-tutorials.html&quot; target=&quot;_blank&quot;&gt;Java Tutorials PDF&lt;/a&gt; books one by one and improve your ja&lt;span style=&quot;font-size: large;&quot;&gt;va programming skills.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Once you &lt;span style=&quot;font-size: large;&quot;&gt;start gaining &lt;span style=&quot;font-size: large;&quot;&gt;confidence&lt;span style=&quot;font-size: large;&quot;&gt;, &lt;span style=&quot;font-size: large;&quot;&gt;you can test your skills by preparing &lt;a href=&quot;http://codeinjavase.blogspot.com/p/java-se-projects.html&quot;&gt;Core Java Projects&lt;/a&gt; listed&lt;span style=&quot;font-size: large;&quot;&gt; here with the project synopses.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/6055214224885859967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=6055214224885859967&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6055214224885859967'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6055214224885859967'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/07/installing-jdk-and-setting-environment.html' title='Installing the JDK and Setting the Environment Variables'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-688069540398016847</id><published>2011-05-03T15:32:00.002+05:30</published><updated>2011-07-29T00:51:37.115+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java Myths and Facts"/><title type='text'>Java Myths- Fact versus Fiction</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h3 align=&quot;center&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Inside Java :&lt;br /&gt;Java myths - fact versus fiction&lt;/span&gt;&lt;/h3&gt;There are so many myths about         Java. This month, I&#39;ll look at some of the more persistent ones, and try         to dispel any doubt.&lt;br /&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fiction :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style=&quot;color: navy;&quot;&gt;Applets can read your hard-drive, and delete         files&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fact :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;Any attempt by an applet to access local files will throw a         SecurityException. If uncaught, the applet will crash, but no file         access will occur.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;The only exception to this rule is for digitally signed         applets, which may be granted additional privileges. Your browser will         display a dialog box, asking you if you want to accept the identity of         the applet author. Choose no if unsure, and you&#39;ll always be safe.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fiction :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style=&quot;color: navy;&quot;&gt;Java requires a web browser. Java only runs in         a web browser.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fact :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;Java code comes in many forms. The most familiar to users is the         applet, which runs inside a web browser. However, this is only the tip         of the iceberg.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Java applications can be run just like normal programs.         By installing a JVM from vendors like Sun or Microsoft, you gain the         ability to run Java programs. It&#39;s just like normal programs, such as         Microsoft Word.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Java servlets run inside a web server. Servlets are a         replacement for CGI scripts, and Active Server Pages (ASP). Servlets are         actually really fast, and can be more efficient than CGI scripts.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fiction :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style=&quot;color: navy;&quot;&gt;Java is a hundred times slower than C, so you         shouldn&#39;t ever use it for &quot;serious&quot; programming.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Fact :&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;The original Java Virtual Machines were slow, so slow that a         comparable C application was about twenty times faster. The gap between         C/C++ and Java is growing smaller though, thanks to better designed JVMs,         and Just-In-Time (JIT) compilers that convert bytecode to native machine         code at runtime.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Much of the cause for speed concerns is actually over         applet loading - large applets take a long time to load initially.         Thankfully, Java applications and servlets are not subject to such slow         loading times.&amp;nbsp;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fiction :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style=&quot;color: navy;&quot;&gt;There&#39;s no point learning any language other         than Java - it will dominate the software industry and put C++         programmers out of work.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fact :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;There are many languages suited to very narrow and specific tasks         (such as artificial intelligence) that Java will never overcome. While         it is true that Java has had phenomenal growth, and significantly eroded         the role of C++, it still has a long way to go. Java excels in         portability and Internet support - but C++ still offers better         performance and the ability to interact with the operating system.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fiction :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style=&quot;color: navy;&quot;&gt;Applets are inherently unreliable. The         differences between Netscape and Internet Explorer make it impossible to         write stable applets.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fact :&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;The problems with the behavior of applets running under browsers is         due to the different JVMs - between browsers and browser versions. These         differences can be frustrating for developers and users. There are often         workarounds though, and applets should always be tested with a variety         of browsers.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;The optimal solution is to use a single JVM, which will         be used across all browsers. Sound like fantasy? Sun Microsystems, the         creators of Java, have come up with the Java Plug-in. The Java Plug-in         bypasses the browser&#39;s JVM, and uses its own. This means that users with         the plug-in installed can always rely on uniform performance, regardless         of which browser they use. For more information, see the  Java Plug-in         page.&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/688069540398016847/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=688069540398016847&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/688069540398016847'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/688069540398016847'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/05/java-myths-fact-versus-fiction.html' title='Java Myths- Fact versus Fiction'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-7641272670842099323</id><published>2011-04-30T22:48:00.002+05:30</published><updated>2011-07-29T00:52:19.470+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="top 10 errors java programmers make"/><category scheme="http://www.blogger.com/atom/ns#" term="top ten errors"/><title type='text'>Top 10 Errors Java Programmers Make</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h1 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Top Ten Errors Java Programmers Make&lt;/h1&gt;&lt;h3 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;(How to spot them. How to fix/prevent them.)&lt;/h3&gt;&lt;div align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Whether you program regularly in Java, and know it like the back of         your hand, or whether you&#39;re new to the language or a casual programmer,         you&#39;ll make mistakes. It&#39;s natural, it&#39;s human, and guess what? You&#39;ll         more than likely make the same mistakes that others do, over and over         again. Here&#39;s my top ten list of errors that we all seem to make at one         time or another,&amp;nbsp; how to spot them, and how to fix them.&lt;/div&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;10. Accessing non-static member variables from static methods (such         as main)&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Many programmers, particularly when first introduced to Java, have         problems with accessing member variables from their &lt;i&gt;main&lt;/i&gt;         method. The method signature for main is marked static - meaning that we         don&#39;t need to create an instance of the class to invoke the main method.         For example, a Java Virtual Machine (JVM) could call the class         MyApplication like this :-&lt;/div&gt;&lt;blockquote style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;MyApplication.main ( command_line_args );&lt;/blockquote&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;This means, however, that there isn&#39;t an instance of MyApplication -         it doesn&#39;t have any member variables to access! Take for example the         following application, which will generate a compiler error message.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public class StaticDemo&lt;br /&gt;{&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;        public String my_member_variable = &quot;somedata&quot;;&lt;/pre&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public static void main (String args[])&lt;br /&gt;        {&lt;br /&gt;  // Access a non-static member from static method&lt;br /&gt;                System.out.println (&quot;This generates a compiler error&quot; +&lt;br /&gt;   my_member_variable );&lt;br /&gt;        }&lt;br /&gt;}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;If you want to access its member variables from a non-static method         (like &lt;i&gt;main&lt;/i&gt;), you must create an instance of the object. Here&#39;s         a simple example of how to correctly write code to access non-static         member variables, by first creating an instance of the object.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public class NonStaticDemo&lt;br /&gt;{&lt;br /&gt;        public String my_member_variable = &quot;somedata&quot;;&lt;br /&gt;&lt;br /&gt;        public static void main (String args[])&lt;br /&gt;        {&lt;br /&gt;                NonStaticDemo demo = new NonStaticDemo();&lt;br /&gt;&lt;br /&gt;  // Access member variable of demo&lt;br /&gt;                System.out.println (&quot;This WON&#39;T generate an error&quot; +&lt;br /&gt;                        demo.my_member_variable );&lt;br /&gt;        }&lt;br /&gt;}&lt;/pre&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;9. Mistyping the name of a method when overriding&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Overriding allows programmers to replace a method&#39;s implementation         with new code. Overriding is a handy feature, and most OO programmers         make heavy use of it. If you use the AWT 1.1 event handling model,         you&#39;ll often override listener implementations to provide custom         functionality. One easy trap to fall into with overriding, is to mistype         the method name. If you mistype the name, you&#39;re no longer overriding a         method - you&#39;re creating an entirely new method, but with the same         parameter and return type.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public class MyWindowListener extends WindowAdapter {&lt;br /&gt; // This should be WindowClose&lt;b&gt;d&lt;/b&gt;&lt;br /&gt; public void WindowClose(WindowEvent e) {&lt;br /&gt;  // Exit when user closes window&lt;br /&gt;  System.exit(0);&lt;br /&gt; }&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Compilers won&#39;t pick up on this one, and the problem can be quite         frustrating to detect. In the past, I&#39;ve looked at a method, believed         that it was being called, and taken ages to spot the problem. The         symptom of this error will be that your code isn&#39;t being called, or you         think the method has skipped over its code. The only way to ever be         certain is to add a println statement, to record a message in a log         file, or to use good trace debugger (like Visual J++ or Borland         JBuilder) and step through line by line. If your method still isn&#39;t         being called, then it&#39;s likely you&#39;ve mistyped the name.&lt;/div&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;8. Comparison assignment (&amp;nbsp; = rather than == )&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;This is an easy error to make. If you&#39;re used other languages before,         such as Pascal, you&#39;ll realize just how poor a choice this was by the         language&#39;s designers. In Pascal, for example, we use the := operator for         assignment, and leave = for comparison. This looks like a throwback to         C/C++, from which Java draws its roots.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Fortunately, even if you don&#39;t spot this one by looking at code on         the screen, your compiler will. Most commonly, it will report an error         message like this : &quot;Can&#39;t convert xxx to boolean&quot;, where xxx         is a Java type that you&#39;re assigning instead of comparing.&lt;/div&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;7. Comparing two objects ( == instead of .equals)&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;When we use the == operator, we are actually comparing two object         references, to see if they point to the same object. We cannot compare,         for example, two strings for equality, using the == operator. We must         instead use the .equals method, which is a method inherited by all         classes from java.lang.Object.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Here&#39;s the correct way to compare two strings.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;String abc = &quot;abc&quot;; String def = &quot;def&quot;;&lt;br /&gt;&lt;br /&gt;// Bad way&lt;br /&gt;if ( (abc + def) == &quot;abcdef&quot; )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ......&lt;br /&gt;}&lt;/pre&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;// Good way&lt;br /&gt;if ( (abc + def).equals(&quot;abcdef&quot;) )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; .....&lt;br /&gt;}&lt;/pre&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;6. Confusion over passing by value, and passing by reference&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;This can be a frustrating problem to diagnose, because when you look         at the code, you might be sure that its passing by reference, but find         that its actually being passed by value. Java uses &lt;b&gt;both&lt;/b&gt;,         so you need to understand when you&#39;re passing by value, and when you&#39;re         passing by reference.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;When you pass a primitive data type, such as a char, int, float, or         double, to a function then you are &lt;b&gt;passing by value&lt;/b&gt;.         That means that a copy of the data type is duplicated, and passed to the         function. If the function chooses to modify that value, it will be         modifying the copy only. Once the function finishes, and control is         returned to the returning function, the &quot;real&quot; variable will         be untouched, and no changes will have been saved. If you need to modify         a primitive data type, make it a return value for a function, or wrap it         inside an object.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;When you pass a Java object, such as an array, a vector, or a string,         to a function then you are &lt;b&gt;passing by reference&lt;/b&gt;. Yes -         a String is actually an object, not a primitive data type.&amp;nbsp; So that         means that if you pass an object to a function, you are passing a         reference to it, not a duplicate. Any changes you make to the object&#39;s         member variables will be permanent - which can be either good or bad,         depending on whether this was what you intended.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;On a side note, since String contains no methods to modify its         contents, you might as well be passing by value.&lt;/div&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;5. Writing blank exception handlers&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;I know it&#39;s very tempting to write blank exception handlers, and to         just ignore errors. But if you run into problems, and haven&#39;t written         any error messages, it becomes almost impossible to find out the cause         of the error. Even the simplest exception handler can be of benefit. For         example, put a try { .. } catch Exception around your code, to catch ANY         type of exception, and print out the message. You don&#39;t need to write a         custom handler for every exception (though this is still good         programming practice). Don&#39;t ever leave it blank, or you won&#39;t know         what&#39;s happening.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;For example&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;br /&gt; // Your code goes here..&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt; System.out.println (&quot;Err - &quot; + e );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/pre&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;4. Forgetting that Java is zero-indexed&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;If you&#39;ve come from a C/C++ background, you may not find this quite         as much a problem as those who have used other languages. In Java,         arrays are zero-indexed, meaning that the first element&#39;s index is         actually 0. Confused? Let&#39;s look at a quick example.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;// Create an array of three strings&lt;br /&gt;String[] strArray = new String[3];&lt;br /&gt;&lt;br /&gt;// First element&#39;s index is actually 0&lt;br /&gt;strArray[0] = &quot;First string&quot;;&lt;br /&gt;&lt;br /&gt;// Second element&#39;s index is actually 1&lt;br /&gt;strArray[1] = &quot;Second string&quot;;&lt;br /&gt;&lt;br /&gt;// Final element&#39;s index is actually 2&lt;br /&gt;strArray[2] = &quot;Third and final string&quot;;&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;In this example, we have an array of three strings, but to access         elements of the array we actually subtract one. Now, if we were to try         and access strArray[3], we&#39;d be accessing the fourth element. This will         case an ArrayOutOfBoundsException to be thrown - the most obvious sign         of forgetting the zero-indexing rule.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Other areas where zero-indexing can get you into trouble is with         strings. Suppose you wanted to get a character at a particular offset         within a string. Using the String.charAt(int) function you can look this         information up - but under Java, the String class is also zero-indexed.         That means than the first character is at offset 0, and second at offset         1. You can run into some very frustrating problems unless you are aware         of this - particularly if you write applications with heavy string         processing. You can be working on the wrong character, and also throw         exceptions at run-time. Just like the ArrayOutOfBoundsException, there         is a string equivalent. Accessing beyond the bounds of a String will         cause a StringIndexOutOfBoundsException to be thrown, as demonstrated by         this example.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public class StrDemo&lt;br /&gt;{&lt;br /&gt; public static void main (String args[])&lt;br /&gt; {&lt;br /&gt;        String abc = &quot;abc&quot;;&lt;br /&gt;&lt;br /&gt;        System.out.println (&quot;Char at offset 0 : &quot; + abc.charAt(0) );&lt;br /&gt;        System.out.println (&quot;Char at offset 1 : &quot; + abc.charAt(1) );&lt;br /&gt;        System.out.println (&quot;Char at offset 2 : &quot; + abc.charAt(2) );&lt;br /&gt;&lt;br /&gt; // This line should throw a StringIndexOutOfBoundsException&lt;br /&gt;        System.out.println (&quot;Char at offset 3 : &quot; + abc.charAt(3) );&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Note too, that zero-indexing doesn&#39;t just apply to arrays, or to         Strings. Other parts of Java are also indexed, but not always         consistently. The java.util.Date, and java.util.Calendar classes start         their months with 0, but days start normally with 1. This problem is         demonstrated by the following application.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;import java.util.Date;&lt;br /&gt;import java.util.Calendar;&lt;br /&gt;&lt;br /&gt;public class ZeroIndexedDate&lt;br /&gt;{&lt;br /&gt;        public static void main (String args[])&lt;br /&gt;        {&lt;br /&gt;                // Get today&#39;s date&lt;br /&gt;                Date today = new Date();&lt;br /&gt; &lt;br /&gt;  // Print return value of getMonth&lt;br /&gt;  System.out.println (&quot;Date.getMonth() returns : &quot; +&lt;br /&gt;    today.getMonth());&lt;br /&gt;&lt;br /&gt;  // Get today&#39;s date using a Calendar&lt;br /&gt;  Calendar rightNow = Calendar.getInstance();&lt;br /&gt;&lt;br /&gt;  // Print return value of get ( Calendar.MONTH )&lt;br /&gt;  System.out.println (&quot;Calendar.get (month) returns : &quot; +&lt;br /&gt;   rightNow.get ( Calendar.MONTH ));&lt;/pre&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Zero-indexing is only a problem if you don&#39;t realize that its         occurring. If you think you&#39;re running into a problem, always consult         your API documentation.&lt;/div&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;3. Preventing concurrent access to shared variables by threads&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;When writing multi-threaded applications, many programmers (myself         included) often cut corners, and leave their applications and applets         vulnerable to thread conflicts. When two or more threads access the same         data concurrently, there exists the possibility (and Murphy&#39;s law         holding, the probability) that two threads will access or modify the         same data at the same time. Don&#39;t be fooled into thinking that such         problems won&#39;t occur on single-threaded processors. While accessing some         data (performing a read), your thread may be suspended, and another         thread scheduled. It writes its data, which is then overwritten when the         first thread makes its changes.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Such problems are not just limited to multi-threaded applications or         applets. If you write Java APIs, or JavaBeans, then your code may not be         thread-safe. Even if you never write a single application that uses         threads, people that use your code WILL. For the sanity of others, if         not yourself, you should always take precautions to prevent concurrent         access to shared data.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;How can this problem be solved? The simplest method is to make your         variables private (but you do that already,&amp;nbsp; right?) and to use         synchronized accessor methods. Accessor methods allow access to private         member variables, but in a controlled manner. Take the following         accessor methods, which provide a safe way to change the value of a         counter.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public class MyCounter&lt;br /&gt;{&lt;br /&gt; private int count = 0; // count starts at zero&lt;br /&gt;&lt;br /&gt; public synchronized void setCount(int amount)&lt;br /&gt; { &lt;br /&gt;  count = amount;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public synchronized int getCount()&lt;br /&gt; {&lt;br /&gt;  return count;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;2. Capitalization errors&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;This is one of the most frequent errors that we all make. It&#39;s so         simple to do, and sometimes one can look at an uncapitalized variable or         method and still not spot the problem. I myself have often been puzzled         by these errors, because I recognize that the method or variable does         exist, but don&#39;t spot the lack of capitalization.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;While there&#39;s no silver bullet for detecting this error, you can         easily train yourself to make less of them. There&#39;s a very simple trick         you can learn :-         &lt;/div&gt;&lt;ul style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;li&gt;all methods and member variables in the Java API begin with             lowercase letters&lt;/li&gt;&lt;li&gt;all methods and member variables use capitalization where a new             word begins e.g - getDoubleValue()&lt;/li&gt;&lt;/ul&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;If you use this pattern for all of your member variables and classes,         and then make a conscious effort to get it right, you can gradually         reduce the number of mistakes you&#39;ll make. It may take a while, but it         can save some serious head scratching in the future.&lt;/div&gt;&lt;h2 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;(drum roll)&lt;/h2&gt;&lt;h2 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;And the number one error that Java programmers make         !!!!!&lt;/h2&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;1. Null pointers!&lt;/h1&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Null pointers are one of the most common errors that Java programmers         make. Compilers can&#39;t check this one for you - it will only surface at         runtime, and if you don&#39;t discover it, your users certainly will.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;When an attempt to access an object is made, and the reference to         that object is null, a NullPointerException will be thrown. The cause of         null pointers can be varied, but generally it means that either you         haven&#39;t initialized an object, or you haven&#39;t checked the return value         of a function.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Many functions return null to indicate an error condition - but         unless you check your return values, you&#39;ll never know what&#39;s happening.         Since the cause is an error condition, normal testing may not pick it up         - which means that your users will end up discovering the problem for         you. If the API function indicates that null may be returned, be sure to         check this before using the object reference!&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Another cause is where your initialization has been sloppy, or where         it is conditional. For example, examine the following code, and see if         you can spot the problem.&lt;/div&gt;&lt;pre style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt; // Accept up to 3 parameters&lt;br /&gt; String[] list = new String[3];&lt;br /&gt;&lt;br /&gt; int index = 0;&lt;br /&gt;&lt;br /&gt; while ( (index &amp;lt; args.length) &amp;amp;&amp;amp; ( index &amp;lt; 3 ) )&lt;br /&gt; {&lt;br /&gt;  list[index++] = args[index];&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; // Check all the parameters &lt;br /&gt; for (int i = 0; i &amp;lt; list.length; i++)&lt;br /&gt; {&lt;br /&gt;  if (list[i].equals &quot;-help&quot;)&lt;br /&gt;  {&lt;br /&gt;   // .........&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  if (list[i].equals &quot;-cp&quot;)&lt;br /&gt;  {&lt;br /&gt;   // .........&lt;br /&gt;  }&lt;br /&gt;  // else .....&lt;br /&gt; } &lt;br /&gt;}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;This code (while a contrived example), shows a common mistake. Under         some circumstances, where the user enters three or more parameters, the         code will run fine. If no parameters are entered, you&#39;ll get a         NullPointerException at runtime. Sometimes your variables (the array of         strings) will be initialized, and other times they won&#39;t. One easy         solution is to check BEFORE you attempt to access a variable in an array         that it is not equal to null.&lt;/div&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&lt;/h3&gt;&lt;h3 style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Summary&lt;/span&gt;&lt;/h3&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;These errors represent but some of the many that we all make. Though         it is impossible to completely eliminate errors from the coding process,         with care and practice you can avoid repeating the same ones. Rest         assured, however, that all Java programmers encounter the same sorts of         problems. It&#39;s comforting to know, that while you work late into the         night tracking down an error, someone, somewhere, sometime, will make         the same mistake!&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/7641272670842099323/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=7641272670842099323&amp;isPopup=true' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/7641272670842099323'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/7641272670842099323'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/04/top-10-errors-java-programmers-make.html' title='Top 10 Errors Java Programmers Make'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-6298238698984965482</id><published>2011-04-30T22:41:00.003+05:30</published><updated>2011-07-29T00:53:03.184+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Comparison:C++ and Java"/><title type='text'>Comparison: C++ and Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Comparing C++ and Java&lt;/span&gt;&lt;/h2&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;center&gt;           &lt;table bgcolor=&quot;#FFEDEC&quot; border=&quot;0&quot;&gt;&lt;tbody&gt;&lt;tr&gt;               &lt;td width=&quot;100%&quot;&gt;&lt;div align=&quot;left&quot;&gt;Many developers already have                 experience with an object-oriented programming language like                 C++. As you make the transition to Java, you will encounter many                 differences, despite some strong similarities. In this excerpt from &quot;&lt;i&gt;Thinking in Java&lt;/i&gt;&quot;, Bruce                 Eckel highlights the important differences that C++ programmers                 should be aware of.&lt;/div&gt;&lt;/td&gt;             &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/center&gt;         &lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;span style=&quot;font-size: small;&quot;&gt;As a C++ programmer, you already have the basic  idea of object-oriented programming, and the syntax of Java no doubt  looks familiar to you. This makes sense since Java was derived from C++.         However, there are a surprising number of differences between  C++ and Java.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;These differences are intended to be significant  improvements, and if you understand the differences you&#39;ll see why Java  is such a beneficial programming language. This         article takes you through the important features that  distinguish Java from C++.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace; font-size: small;&quot;&gt;          &lt;/span&gt;&lt;br /&gt;&lt;ol style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The biggest potential stumbling block is speed: interpreted Java               runs in the range of 20 times slower than C. Nothing prevents the               Java language from being compiled and there are just-in-time               compilers appearing at this writing that offer significant               speed-ups. It is not inconceivable that full native compilers will               appear for the more popular platforms, but without those there are               classes of problems that will be insoluble with Java because of               the speed issue.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has both kinds of comments like C++ does.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Everything must be in a class. There are no global functions or               global data. If you want the equivalent of globals, make &lt;b&gt;static&lt;/b&gt;               methods and &lt;b&gt;static&lt;/b&gt; data within a class. There are no               structs or enumerations or unions, only classes.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;All method definitions are defined in the body of the class.               Thus, in C++ it would look like all the functions are inlined, but               they’re not (inlines are noted later).&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Class definitions are roughly the same form in Java as in C++,               but there’s no closing semicolon. There are no class               declarations of the form &lt;b&gt;class foo,&lt;/b&gt; only class definitions. &lt;/span&gt;&lt;pre&gt;&lt;span style=&quot;font-size: small;&quot;&gt;class aType {&lt;br /&gt;    void aMethod( ) { /* method body */ }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There’s no scope resolution operator &lt;b&gt;::&lt;/b&gt; in Java. Java             uses the dot for everything, but can get away with it since you can             define elements only within a class. Even the method definitions             must always occur within a class, so there is no need for scope             resolution there either. One place where you’ll notice the             difference is in the calling of &lt;b&gt;static&lt;/b&gt; methods: you say &lt;b&gt;ClassName.methodName(&amp;nbsp;);&lt;/b&gt;.             In addition, &lt;b&gt;package&lt;/b&gt; names are established using the dot, and             to perform a kind of C++ &lt;b&gt;#include&lt;/b&gt; you use the &lt;b&gt;import&lt;/b&gt;             keyword. For example: &lt;b&gt;import java.awt.*;&lt;/b&gt;. (&lt;b&gt;#include&lt;/b&gt;             does not directly map to &lt;b&gt;import&lt;/b&gt;, but it has a similar feel to             it).&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java, like C++, has primitive types for efficient access. In Java,             these are &lt;b&gt;boolean&lt;/b&gt;, &lt;b&gt;char&lt;/b&gt;, &lt;b&gt;byte&lt;/b&gt;, &lt;b&gt;short&lt;/b&gt;, &lt;b&gt;int&lt;/b&gt;,             &lt;b&gt;long&lt;/b&gt;, &lt;b&gt;float&lt;/b&gt;, and &lt;b&gt;double&lt;/b&gt;. All the primitive             types have specified sizes that are machine independent for             portability. (This must have some impact on performance, varying             with the machine.) Type-checking and type requirements are much             tighter in Java. For example:&lt;br /&gt;&lt;br /&gt;1. Conditional expressions can be only&lt;/span&gt; &lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt; boolean&lt;/b&gt;, not integral.&lt;br /&gt;&lt;br /&gt;2. The result of an expression like X + Y must be used; you can’t             just say &quot;X + Y&quot; for the side effect.&lt;/span&gt; &lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;b&gt;char&lt;/b&gt; type uses the international 16-bit Unicode             character set, so it can automatically represent most national             characters.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Static quoted strings are automatically converted into &lt;b&gt;String&lt;/b&gt;             objects. There is no independent static character array string like             there is in C and C++.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java adds the triple right shift &lt;b&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/b&gt; to act as a             &quot;logical&quot; right shift by inserting zeroes at the top end;             the &lt;b&gt;&amp;gt;&amp;gt;&lt;/b&gt; inserts the sign bit as it shifts (an             &quot;arithmetic&quot; shift).&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Although they look similar, arrays have a very different structure             and behavior in Java than they do in C++. There’s a read-only &lt;b&gt;length&lt;/b&gt;             member that tells you how big the array is, and run-time checking             throws an exception if you go out of bounds. All arrays are created             on the heap, and you can assign one array to another (the array             handle is simply copied). The array identifier is a first-class             object, with all of the methods commonly available to all other             objects.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;All objects of non-primitive types can be created only via &lt;b&gt;new&lt;/b&gt;.             There’s no equivalent to creating non-primitive objects &quot;on             the stack&quot; as in C++. All primitive types can be created only             on the stack, without &lt;b&gt;new&lt;/b&gt;. There are wrapper classes for all             primitive classes so that you can create equivalent heap-based             objects via &lt;b&gt;new&lt;/b&gt;. (Arrays of primitives are a special case:             they can be allocated via aggregate initialization as in C++, or by             using &lt;b&gt;new.&lt;/b&gt;)&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;No forward declarations are necessary in Java. If you want to use             a class or a method before it is defined, you simply use it – the             compiler ensures that the appropriate definition exists. Thus you             don’t have any of the forward referencing issues that you do in             C++.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has no preprocessor. If you want to use classes in another             library, you say &lt;b&gt;import&lt;/b&gt; and the name of the library. There             are no preprocessor-like macros.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java uses packages in place of namespaces. The name issue is taken             care of by putting everything into a class and by using a facility             called &quot;packages&quot; that performs the equivalent namespace             breakup for class names. Packages also collect library components             under a single library name. You simply &lt;b&gt;import&lt;/b&gt; a package and             the compiler takes care of the rest.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Object handles defined as class members are automatically             initialized to &lt;b&gt;null&lt;/b&gt;. Initialization of primitive class data             members is guaranteed in Java; if you don’t explicitly initialize             them they get a default value (a zero or equivalent). You can             initialize them explicitly, either when you define them in the class             or in the constructor. The syntax makes more sense than that for             C++, and is consistent for &lt;b&gt;static&lt;/b&gt; and non-&lt;b&gt;static&lt;/b&gt;             members alike. You don’t need to externally define storage for &lt;b&gt;static&lt;/b&gt;             members like you do in C++.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There are no Java pointers in the sense of C and C++. When you             create an object with &lt;b&gt;new&lt;/b&gt;, you get back a reference (which I’ve             been calling a &lt;i&gt;handle&lt;/i&gt; in this book). For example:&lt;/span&gt; &lt;span style=&quot;font-size: small;&quot;&gt;String s = new           String(&quot;howdy&quot;);                      However, unlike C++ references that must be initialized when created           and cannot be rebound to a different location, Java references don’t           have to be bound at the point of creation. They can also be rebound at           will, which eliminates part of the need for pointers. The other reason           for pointers in C and C++ is to be able to point at any place in           memory whatsoever (which makes them unsafe, which is why Java doesn’t           support them). Pointers are often seen as an efficient way to move           through an array of primitive variables; Java arrays allow you to do           that in a safer fashion. The ultimate solution for pointer problems is           native methods (discussed in Appendix A). Passing pointers to methods           isn’t a problem since there are no global functions, only classes,           and you can pass references to objects.&lt;br /&gt;The Java language promoters initially said &quot;No pointers!&quot;,           but when many programmers questioned how you can work without           pointers, the promoters began saying &quot;Restricted pointers.&quot;           You can make up your mind whether it’s &quot;really&quot; a pointer           or not. In any event, there’s no pointer &lt;i&gt;arithmetic&lt;/i&gt;.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has constructors that are similar to constructors in C++. You             get a default constructor if you don’t define one, and if you             define a non-default constructor, there’s no automatic default             constructor defined for you, just like in C++. There are no copy             constructors, since all arguments are passed by reference.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There are no destructors in Java. There is no &quot;scope&quot; of             a variable per se, to indicate when the object’s lifetime is ended             – the lifetime of an object is determined instead by the garbage             collector. There is a &lt;b&gt;finalize(&amp;nbsp;)&lt;/b&gt; method that’s a             member of each class, something like a C++ destructor, but &lt;b&gt;finalize(&amp;nbsp;)&lt;/b&gt;             is called by the garbage collector and is supposed to be responsible             only for releasing &quot;resources&quot; (such as open files,             sockets, ports, URLs, etc). If you need something done at a specific             point, you must create a special method and call it, not rely upon &lt;b&gt;finalize(&amp;nbsp;)&lt;/b&gt;.             Put another way, all objects in C++ will be (or rather, should be)             destroyed, but not all objects in Java are garbage collected.             Because Java doesn’t support destructors, you must be careful to             create a cleanup method if it’s necessary and to explicitly call             all the cleanup methods for the base class and member objects in             your class.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has method overloading that works virtually identically to             C++ function overloading.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java does not support default arguments.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There’s no &lt;b&gt;goto&lt;/b&gt; in Java. The one unconditional jump             mechanism is the &lt;b&gt;break &lt;/b&gt;&lt;i&gt;label&lt;/i&gt; or &lt;b&gt;continue &lt;/b&gt;&lt;i&gt;label&lt;/i&gt;,             which is used to jump out of the middle of multiply-nested loops.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java uses a singly-rooted hierarchy, so all objects are ultimately             inherited from the root class &lt;b&gt;Object&lt;/b&gt;. In C++, you can start a             new inheritance tree anywhere, so you end up with a forest of trees.             In Java you get a single ultimate hierarchy. This can seem             restrictive, but it gives a great deal of power since you know that             every object is guaranteed to have at least the &lt;b&gt;Object&lt;/b&gt;             interface. C++ appears to be the only OO language that does not             impose a singly rooted hierarchy.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has no templates or other implementation of parameterized             types. There is a set of collections: &lt;b&gt;Vector&lt;/b&gt;, &lt;b&gt;Stack&lt;/b&gt;,             and &lt;b&gt;Hashtable&lt;/b&gt; that hold &lt;b&gt;Object&lt;/b&gt; references, and through             which you can satisfy your collection needs, but these collections             are not designed for efficiency like the C++ Standard Template             Library (STL). The new collections in Java 1.2 are more complete,             but still don’t have the same kind of efficiency as template implementations would allow.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Garbage collection means memory leaks are much harder to cause in             Java, but not impossible. (If you make native method calls that             allocate storage, these are typically not tracked by the garbage             collector.) However, many memory leaks and resouce leaks can be             tracked to a badly written &lt;b&gt;finalize(&amp;nbsp;) &lt;/b&gt;or to not             releasing a resource at the end of the block where it is allocated             (a place where a destructor would certainly come in handy). The             garbage collector is a huge improvement over C++, and makes a lot of             programming problems simply vanish. It might make Java unsuitable             for solving a small subset of problems that cannot tolerate a             garbage collector, but the advantage of a garbage collector seems to             greatly outweigh this potential drawback.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has built-in multithreading support. There’s a &lt;b&gt;Thread&lt;/b&gt;             class that you inherit to create a new thread (you override the &lt;b&gt;run(&amp;nbsp;)&lt;/b&gt;             method). Mutual exclusion occurs at the level of objects using the &lt;b&gt;synchronized&lt;/b&gt;             keyword as a type qualifier for methods. Only one thread may use a &lt;b&gt;synchronized&lt;/b&gt;             method of a particular object at any one time. Put another way, when             a &lt;b&gt;synchronized&lt;/b&gt; method is entered, it first &quot;locks&quot;             the object against any other &lt;b&gt;synchronized&lt;/b&gt; method using that             object and &quot;unlocks&quot; the object only upon exiting the             method. There are no explicit locks; they happen automatically. You’re             still responsible for implementing more sophisticated             synchronization between threads by creating your own             &quot;monitor&quot; class. Recursive &lt;b&gt;synchronized&lt;/b&gt; methods             work correctly. Time slicing is not guaranteed between equal             priority threads.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Instead of controlling blocks of declarations like C++ does, the             access specifiers (&lt;b&gt;public&lt;/b&gt;, &lt;b&gt;private&lt;/b&gt;, and &lt;b&gt;protected&lt;/b&gt;)             are placed on each definition for each member of a class. Without an             explicit access specifier, an element defaults to             &quot;friendly,&quot; which means that it is accessible to other             elements in the same package (equivalent to them all being C++ &lt;b&gt;friend&lt;/b&gt;s)             but inaccessible outside the package. The class, and each method             within the class, has an access specifier to determine whether it’s             visible outside the file. Sometimes the &lt;b&gt;private &lt;/b&gt;keyword is             used less in Java because &quot;friendly&quot; access is often more             useful than excluding access from other classes in the same package.             (However, with multithreading the proper use of &lt;b&gt;private&lt;/b&gt; is             essential.) The Java &lt;b&gt;protected&lt;/b&gt; keyword means &quot;accessible             to inheritors &lt;i&gt;and&lt;/i&gt; to others in this package.&quot; There is             no equivalent to the C++ &lt;b&gt;protected &lt;/b&gt;keyword that means             &quot;accessible to inheritors&lt;i&gt; only&lt;/i&gt;&quot; (&lt;b&gt;private             protected&lt;/b&gt; used to do this, but the use of that keyword pair was             removed).&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Nested classes. In C++, nesting a class is an aid to name hiding             and code organization (but C++ namespaces eliminate the need for             name hiding). Java packaging provides the equivalence of namespaces,             so that isn’t an issue. Java 1.1 has &lt;i&gt;inner classes&lt;/i&gt; that             look just like nested classes. However, an object of an inner class             secretly keeps a handle to the object of the outer class that was             involved in the creation of the inner class object. This means that             the inner class object may access members of the outer class object             without qualification, as if those members belonged directly to the             inner class object. This provides a much more elegant solution to             the problem of callbacks, solved with pointers to members in C++.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Because of inner classes described in the previous point, there             are no pointers to members in Java.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;No &lt;b&gt;inline&lt;/b&gt; methods. The Java compiler might decide on its             own to inline a method, but you don’t have much control over this.             You can suggest inlining in Java by using the &lt;b&gt;final&lt;/b&gt; keyword             for a method. However, &lt;b&gt;inline&lt;/b&gt; functions are only suggestions             to the C++ compiler as well.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Inheritance in Java has the same effect as in C++, but the syntax             is different. Java uses the &lt;b&gt;extends&lt;/b&gt; keyword to indicate             inheritance from a base class and the &lt;b&gt;super&lt;/b&gt; keyword to             specify methods to be called in the base class that have the same             name as the method you’re in. (However, the &lt;b&gt;super&lt;/b&gt; keyword             in Java allows you to access methods only in the parent class, one             level up in the hierarchy.) Base-class scoping in C++ allows you to             access methods that are deeper in the hierarchy). The base-class             constructor is also called using the &lt;b&gt;super&lt;/b&gt; keyword. As             mentioned before, all classes are ultimately automatically inherited             from &lt;b&gt;Object&lt;/b&gt;.&lt;b&gt; &lt;/b&gt;There’s no explicit constructor             initializer list like in C++, but the compiler forces you to perform             all base-class initialization at the beginning of the constructor             body and it won’t let you perform these later in the body. Member             initialization is guaranteed through a combination of automatic             initialization and exceptions for uninitialized object handles. &lt;/span&gt;&lt;pre&gt;&lt;span style=&quot;font-size: small;&quot;&gt;public class Foo extends Bar&lt;br /&gt;{&lt;br /&gt;   public Foo(String msg) {&lt;br /&gt;      super(msg); // Calls base constructor&lt;br /&gt;   }&lt;br /&gt;   &lt;br /&gt;   public baz(int i) { // Override&lt;br /&gt;      super.baz(i); // Calls base method&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Inheritance in Java doesn’t change the protection level of the             members in the base class. You cannot specify &lt;b&gt;public&lt;/b&gt;, &lt;b&gt;private&lt;/b&gt;,             or &lt;b&gt;protected&lt;/b&gt; inheritance in Java, as you can in C++. Also,             overridden methods in a derived class cannot reduce the access of             the method in the base class. For example, if a method is &lt;b&gt;public&lt;/b&gt;             in the base class and you override it, your overridden method must             also be &lt;b&gt;public&lt;/b&gt; (the compiler checks for this).&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java provides the &lt;b&gt;interface&lt;/b&gt; keyword, which creates the             equivalent of an abstract base class filled with abstract methods             and with no data members. This makes a clear distinction between             something designed to be just an interface and an extension of             existing functionality via the &lt;b&gt;extends&lt;/b&gt; keyword. It’s worth             noting that the &lt;b&gt;abstract&lt;/b&gt; keyword produces a similar effect in             that you can’t create an object of that class. An &lt;b&gt;abstract&lt;/b&gt;             class &lt;i&gt;may&lt;/i&gt; contain abstract methods (although it isn’t             required to contain any), but it is also able to contain             implementations, so it is restricted to single inheritance. Together             with interfaces, this scheme prevents the need for some mechanism             like virtual base classes in C++.&lt;br /&gt;&lt;br /&gt;To create a version of the &lt;/span&gt; &lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;interface&lt;/b&gt; that can be             instantiated, use the &lt;b&gt;implements &lt;/b&gt;keyword, whose syntax looks             like inheritance: &lt;/span&gt;&lt;pre&gt;&lt;span style=&quot;font-size: small;&quot;&gt;public interface Face {&lt;br /&gt;   public void smile();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Baz extends Bar implements Face {&lt;br /&gt;   public void smile( ) {&lt;br /&gt;      System.out.println(&quot;a warm smile&quot;);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There’s no &lt;b&gt;virtual&lt;/b&gt; keyword in Java because all non-&lt;b&gt;static&lt;/b&gt;             methods always use dynamic binding. In Java, the programmer doesn’t             have to decide whether to use dynamic binding. The reason &lt;b&gt;virtual&lt;/b&gt;             exists in C++ is so you can leave it off for a slight increase in             efficiency when you’re tuning for performance (or, put another             way, &quot;If you don’t use it, you don’t pay for it&quot;),             which often results in confusion and unpleasant surprises. The &lt;b&gt;final&lt;/b&gt;             keyword provides some latitude for efficiency tuning – it tells             the compiler that this method cannot be overridden, and thus that it             may be statically bound (and made inline, thus using the equivalent             of a C++ non-&lt;b&gt;virtual&lt;/b&gt; call). These optimizations are up to the             compiler.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java doesn’t provide multiple inheritance (MI), at least not in             the same sense that C++ does. Like &lt;b&gt;protected&lt;/b&gt;, MI seems like a             good idea but you know you need it only when you are face to face             with a certain design problem. Since Java uses a singly-rooted             hierarchy, you’ll probably run into fewer situations in which MI             is necessary. The &lt;b&gt;interface&lt;/b&gt; keyword takes care of combining             multiple interfaces.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Run-time type identification functionality is quite similar to             that in C++. To get information about handle &lt;b&gt;X,&lt;/b&gt; you can say,             for example:&lt;br /&gt;&lt;code&gt;X.getClass().getName();&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;To perform a type-safe downcast you say:&lt;/span&gt;            &lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;code&gt;derived d = (derived)base;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;just like an old-style C           cast. The compiler automatically invokes the dynamic casting mechanism           without requiring extra syntax. Although this doesn’t have the           benefit of easy location of casts as in C++ &quot;new casts,&quot;           Java checks usage and throws exceptions so it won’t allow bad casts           like C++ does.&lt;/span&gt; &lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Exception handling in Java is different because there are no             destructors. A &lt;b&gt;finally&lt;/b&gt; clause can be added to force execution             of statements that perform necessary cleanup. All exceptions in Java             are inherited from the base class &lt;b&gt;Throwable&lt;/b&gt;, so you’re             guaranteed a common interface.&lt;/span&gt; &lt;pre&gt;&lt;span style=&quot;font-size: small;&quot;&gt;public void f(Obj b) throws IOException {&lt;br /&gt;   myresource mr = b.createResource();&lt;br /&gt;   try {&lt;br /&gt;      mr.UseResource();&lt;br /&gt;&lt;br /&gt;   } catch (MyException e) {&lt;br /&gt;      // handle my exception&lt;br /&gt;   } catch (Throwable e) {&lt;br /&gt;      // handle all other exceptions&lt;br /&gt;   } finally {&lt;br /&gt;      mr.dispose(); // special cleanup&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Exception specifications in Java are vastly superior to those in             C++. Instead of the C++ approach of calling a function at run-time             when the wrong exception is thrown, Java exception specifications             are checked and enforced at compile-time. In addition, overridden             methods must conform to the exception specification of the             base-class version of that method: they can throw the specified             exceptions or exceptions derived from those. This provides much more             robust exception-handling code.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has method overloading, but no operator overloading. The &lt;b&gt;String&lt;/b&gt;             class does use the &lt;b&gt;+&lt;/b&gt; and &lt;b&gt;+= &lt;/b&gt;operators to concatenate             strings and &lt;b&gt;String &lt;/b&gt;expressions use automatic type conversion,             but that’s a special built-in case.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The &lt;b&gt;const&lt;/b&gt; issues in C++ are avoided in             Java by convention. You pass only handles to objects and local             copies are never made for you automatically. If you want the             equivalent of C++’s pass-by-value, &lt;a href=&quot;http://draft.blogger.com/post-edit.g?blogID=3365499786104773673&amp;amp;postID=6298238698984965482&quot; name=&quot;OLE_LINK8&quot;&gt;you             call &lt;b&gt;clone(&amp;nbsp;)&lt;/b&gt; to produce a local copy of the argument &lt;/a&gt;(although             the &lt;b&gt;clone(&amp;nbsp;) &lt;/b&gt;mechanism is somewhat poorly designed –             see Chapter 12). There’s no copy-constructor that’s             automatically called.&lt;br /&gt;&lt;br /&gt;To create a compile-time constant value, you say, for example:&lt;/span&gt; &lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;static final int SIZE = 255;&lt;br /&gt;static final int BSIZE = 8 * SIZE;&lt;br /&gt;&lt;/code&gt;&lt;/span&gt;               &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Because of security issues, programming an &quot;application&quot;             is quite different from programming an &quot;applet.&quot; A             significant issue is that an applet won’t let you write to disk,             because that would allow a program downloaded from an unknown             machine to trash your disk. This changes somewhat with Java 1.1             digital signing, which allows you to unequivocally &lt;i&gt;know&lt;/i&gt;             everyone that wrote all the programs that have special access to             your system (one of which might have trashed your disk; you still             have to figure out which one and what to do about it.). Java 1.2             also promises more power for applets&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Since Java can be too restrictive in some cases, you could be             prevented from doing important tasks such as directly accessing             hardware. Java solves this with &lt;i&gt;native methods&lt;/i&gt; that allow you             to call a function written in another language (currently only C and             C++ are supported). Thus, you can always solve a platform-specific             problem (in a relatively non-portable fashion, but then that code is             isolated). Applets cannot call native methods, only applications.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java has built-in support for comment documentation, so the source             code file can also contain its own documentation, which is stripped             out and reformatted into HTML via a separate program. This is a boon             for documentation maintenance and use.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java contains standard libraries for solving specific tasks. C++             relies on non-standard third-party libraries. These tasks include             (or will soon include):             &lt;/span&gt; &lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;             Networking&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;             Database Connection (via JDBC)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;             Multithreading&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;             Distributed Objects (via RMI and CORBA)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;             Compression&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;             Commerce             &lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The availability and standard nature of these libraries allow for             more rapid application development.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java 1.1 includes the Java Beans standard, which is a way to             create components that can be used in visual programming             environments. This promotes visual components that can be used under             all vendor’s development environments. Since you aren’t tied to             a particular vendor’s design for visual components, this should             result in greater selection and availability of components. In             addition, the design for Java Beans is simpler for programmers to             understand; vendor-specific component frameworks tend to involve a             steeper learning curve.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If the access to a Java handle fails, an exception is thrown. This             test doesn’t have to occur right before the use of a handle; the             Java specification just says that the exception must somehow be             thrown. Many C++ runtime systems can also throw exceptions for bad             pointers.&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Generally, Java is more robust, via:&lt;/span&gt; &lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Object handles initialized to &lt;b&gt;null&lt;/b&gt; (a keyword)             &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Handles are always checked and exceptions are thrown for             failures             &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;All array accesses are checked for bounds violations             &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Automatic garbage collection prevents memory leaks             &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Clean, relatively fool-proof exception handling             &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Simple language support for multithreading             &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Bytecode verification of network applets           &lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;</content><link rel="related" href="http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html" title="Comparison: C++ and Java"/><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/6298238698984965482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=6298238698984965482&amp;isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6298238698984965482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/6298238698984965482'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/04/comparison-c-and-java.html' title='Comparison: C++ and Java'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-8038232440031367909</id><published>2011-04-30T22:28:00.002+05:30</published><updated>2011-05-04T19:40:53.624+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java-a revolutionary language"/><title type='text'>The Inside Java: The Java Programming Language</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h3 align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Inside Java :&lt;br /&gt;The Java Programming Language&lt;/span&gt;&lt;/h3&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: large;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;blockquote style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java - an island of Indonesia, a         type of coffee, and a         programming language.&amp;nbsp;Three very different meanings, each in         varying degrees         of importance. Most programmers, though, are interested in the Java         programming language. In just a few short years (since late 1995), Java has taken the         software community by storm. Its phenomenal success has made Java the         fastest growing programming language ever. There&#39;s plenty of hype about         Java, and what it can do. Many programmers, and end-users, are confused         about exactly what it is, and what Java offers.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h3 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Java is a revolutionary language&lt;/span&gt;&lt;/h3&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The properties that make Java so attractive are present         in other programming languages. Many languages are ideally suited for         certain types of applications, even more so than Java. But Java brings         all these properties together, in one language. This is a revolutionary         jump forward for the software industry.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Let&#39;s look at some of the properties in more detail: -&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;ul style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;object-oriented&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;portable&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;multi-threaded&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;automatic garbage collection&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;secure&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;network and &quot;Internet&quot; aware&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;             &lt;div align=&quot;left&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;simplicity and ease-of-use&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h4&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Object-oriented&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Many older languages, like C and Pascal, were &lt;i&gt;procedural&lt;/i&gt;         languages. Procedures (also called functions) were blocks of code that         were part of a module or application. Procedures passed parameters         (primitive data types like integers, characters, strings, and floating         point numbers). Code was treated separately to data. You had to pass         around data structures, and procedures could easily modify their         contents. This was a source of problems, as parts of a program could         have unforeseen effects in other parts. Tracking down which procedure         was at fault wasted a great deal of time and effort, particularly with         large programs.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;In some procedural language, you could even obtain the         memory location of a data structure. Armed with this location, you could         read and write to the data at a later time, or accidentally overwrite         the contents.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java is an &lt;i&gt;object-oriented&lt;/i&gt; language. An         object-oriented language deals with &lt;b&gt;objects&lt;/b&gt;. Objects contain         both data (member variables) and code (methods). Each object belongs to         a particular &lt;b&gt;class&lt;/b&gt;, which is a blueprint describing the member         variables and methods an object offers. In Java, almost every variable         is an object of some type or another - even strings. Object-oriented         programming requires a different way of thinking, but is a better way to         design software than procedural programming.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;There are many popular object-oriented languages         available today. Some like Smalltalk and Java are designed from  the         beginning to be object-oriented. Others, like C++, are partially  object-oriented, and partially procedural. In C++, you can still         overwrite the contents of data structures and objects, causing  the         application to crash. Thankfully, Java prohibits direct access  to memory         contents, leading to a more robust system.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h4&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Portable&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Most programming languages are designed for a specific         operating system and processor architecture. When source code (the         instructions that make up a program) are compiled, it is converted to &lt;b&gt;machine         code&lt;/b&gt; which can be executed only on one type of machine. This process         produces native code, which is extremely fast.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Another type of language is one that is &lt;b&gt;interpreted&lt;/b&gt;.         Interpreted code is read by a software application (the interpreter),         which performs the specified actions. Interpreted code often doesn&#39;t         need to be compiled - it is translated as it is run. For this reason,         interpreted code is quite slow, but often portable across different         operating systems and processor architectures.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java takes the best of both techniques. Java code is         compiled into a platform-neutral machine code, which is called Java &lt;b&gt;bytecode&lt;/b&gt;.         A special type of interpreter, known as a Java Virtual Machine (JVM), reads the         bytecode, and processes it. Figure One shows a disassembly of a small Java         application. The bytecode, indicated by the arrow, is represented in text         form here, but when compiled it is represented as bytes to conserve         space.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;center&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;176&quot; src=&quot;http://www.javacoffeebreak.com/articles/inside_java/bytecode.gif&quot; width=&quot;600&quot; /&gt;&lt;br /&gt;&lt;b&gt;Figure One - Bytecode disassembly for &quot;HelloWorld&quot;&lt;/b&gt;&lt;/span&gt;         &lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The approach Java takes offers some big advantages over         other interpreted languages. Firstly, the source code is protected from         view and modification - only the bytecode needs to be made available to         users. Secondly, security mechanisms can scan bytecode for signs of         modification or harmful code, complimenting the other security         mechanisms of Java. Most of all though, it means that Java code can be         compiled once, and run on any machine and operating system combination         that supports a Java Virtual Machine (JVM). Java can run on Unix,         Windows, Macintosh, and even the Palm Pilot. Java can even run inside a         web browser, or a web server. Being portable means that the application         only has to be written once - and can then execute on a wider range of         machines. This saves a lot of time, and money.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h4&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Multi-threaded&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If you&#39;ve ever written complex applications in C, or         PERL, you&#39;ll probably have come across the concept of multiple processes         before. An application can split itself into separate copies, which run         concurrently. Each copy replicates code and data, resulting in increased         memory consumption. Getting the copies to talk together can be complex,         and frustrating. Creating each process involves a call to the operating         system, which consumes extra CPU time as well.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;A better model is to use multiple threads of execution,         referred to as &lt;b&gt;threads&lt;/b&gt; for short. Threads can share data and         code, making it easier to share data between thread instances. They also         use less memory and CPU overhead. Some languages, like C++, have support         for threads, but they are complex to use. Java has support for multiple         threads of execution built right into the language. Threads require a         different way of thinking, but can be understood very quickly. Thread         support in Java is very simple to use, and the use of threads in         applications and applets is quite commonplace.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Automatic garbage collection&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;No, we&#39;re not talking about taking out the trash (though         a computer that could literally do that would be kind of neat). The term         garbage collection refers to the reclamation of unused memory space.         When applications create objects, the JVM allocates memory space for         their storage. When the object is no longer needed (no reference to the         object exists), the memory space can be reclaimed for later use.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Languages like C++ force programmers to allocate and         deallocate memory for data and objects manually. This adds extra         complexity, but also causes another problem - memory leaks. When         programmers forget to deallocate memory, the amount of free memory         available is decreased. Programs that frequently create and destroy         objects may eventually find that there is no memory left. In Java, the         programmer is free from such worries, as the JVM will perform automatic         garbage collection of objects.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Secure&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Security is a big issue with Java. Since Java applets         are downloaded remotely, and executed in a browser, security is of great         concern. We wouldn&#39;t want applets reading our personal documents,         deleting files, or causing mischief. At the API level, there are strong         security restrictions on file and network access for applets, as well as         support for digital signatures to verify the integrity of downloaded         code. At the bytecode level, checks are made for obvious hacks, such as         stack manipulation or invalid bytecode. The strong security mechanisms         in Java help to protect against inadvertent or intentional security         violations, but it is important to remember that no system is perfect.         The weakest link in the chain is the Java Virtual Machine on which it is         run - a JVM with known security weaknesses can be prone to attack. It is         also worth noting that while there have been a few identified weaknesses         in JVMs, they are rare, and usually fixed quickly.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Network and &quot;Internet&quot; aware&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java was designed to be &quot;Internet&quot; aware, and         to support network programming. The Java API provides extensive network         support, from sockets and IP addresses, to URLs and HTTP. It&#39;s extremely         easy to write network applications in Java, and the code is completely         portable between platforms. In languages like C/C++, the networking code         must be re-written for different operating systems, and is usually more         complex. The networking support of Java saves a lot of         time, and effort.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java also includes support for more exotic network         programming, such as remote-method invocation (RMI), CORBA and Jini.         These distributed systems technologies make Java an attractive choice         for large distributed systems.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h4 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Simplicity and ease-of-use&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java draws its roots from the C++ language. C++ is         widely used, and very popular. Yet it is regarded as a complex language,         with features like multiple-inheritance, templates and pointers that are         counter-productive. Java, on the other hand, is closer to a         &quot;pure&quot; object-oriented language. Access to memory pointers is         removed, and object-references are used instead. Support for multiple-inheritance         has been removed, which lends itself to clearer and simpler class         designs. The I/O and network library is very easy to use, and the Java         API provides developers with lots of time-saving code (such as         networking and data-structures).&amp;nbsp; After using Java for awhile, most         developers are reluctant to return to other languages, because of the         simplicity and elegance of Java.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: small;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;h3 align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Summary&lt;/span&gt;&lt;/h3&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: large;&quot;&gt;         &lt;/span&gt;&lt;br /&gt;&lt;div align=&quot;left&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Java provides developers with many advantages. While         most of these are present in other languages, Java combines all of these         together into one language. The rapid growth of Java has been nothing         short of phenomenal, and shows no signs (yet!) of slowing down. In next         month&#39;s column, I&#39;ll talk more about the heart of Java - the Java         Virtual Machine.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/8038232440031367909/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=8038232440031367909&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/8038232440031367909'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/8038232440031367909'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/04/inside-java-java-programming-language.html' title='The Inside Java: The Java Programming Language'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-3486350441094333046</id><published>2011-04-24T17:37:00.001+05:30</published><updated>2011-05-04T19:41:35.753+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="advantages of using java"/><category scheme="http://www.blogger.com/atom/ns#" term="scope of java"/><title type='text'>Java : Advantages, Scope and Future</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;JAVA is an object oriented programming language and it was intended to serve as a new way to manage software complexity. Java refers to a number of computer software products and specifications from Sun Microsystems that together provide a system for developing application software and deploying it in a cross-platform environment. Java is used in a variety of computing platforms from embedded devices and mobile phones on the low end, to enterprise servers and supercomputers on the high end. Java is nearly everywhere in mobile phones, Web servers and enterprise applications, and while less common on desktop computers; Java applets are often used to provide improved functionality while browsing the World Wide Web.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Some advantages of JAVA:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;It is an open source, so users do not have to struggle with heavy license fees each year&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Platform independent&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Java API&#39;s can easily be accessed by developers&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Java perform supports garbage collection, so memory &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; management is automatic&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Java always allocates objects on the stack&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Java embraced the concept of exception specifications&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Multi-platform support language and support for web-services&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Using JAVA we can develop dynamic web applications&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;It allows you to create modular programs and reusable codes&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Another advantage of JAVA is that, ones the program is written in java we can run it anywhere means that application developed through Java is platform independent. JAVA based enterprise applications perform well because stable JAVA standards help developers to create multilevel applications with a component based approach.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;JAVA programming enables secure and high performance software development on multiple platforms. Many companies in India have well-qualified software engineers having expertise in Java, Java Script, J2SE, JSP, and J2ME, JAVA Programming Services help your businesses to do better. They provide variety of Java development services including project solutions.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/3486350441094333046/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=3486350441094333046&amp;isPopup=true' title='58 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/3486350441094333046'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/3486350441094333046'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/04/java-advantages-scope-and-future.html' title='Java : Advantages, Scope and Future'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>58</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3365499786104773673.post-1472528691942275376</id><published>2011-04-23T21:52:00.001+05:30</published><updated>2011-05-04T19:42:28.723+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="introduction to java"/><title type='text'>Introduction to Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial,Helvetica,Geneva,sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;History&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;div style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Java is a programming language created by James Gosling from Sun Microsystems in 1991. The first public available version of Java (Java 1.0) was released 1995. Over time several version of Java were released which enhanced the language and its libraries. The current version of Java is Java 1.6 also known as Java 6.0.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;From the Java programming language the Java platform evolved. The Java platform allows that code is written in other languages then the Java programming language and still runs on the Java virtual machine.&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;b&gt;&lt;u&gt;Overview&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;div style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The Java programming language consists out of a Java compiler, the Java virtual machine, and the Java class libraries. The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The Java compiler translates Java coding into so-called byte-code. The Java virtual machine interprets this byte-code and runs the program.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The Java virtual machine is written specifically for a specific operating system.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The Java runtime environment (JRE) consists of the JVM and the Java class libraries.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;Characterstics&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small; font-weight: normal;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The target of Java is to write a program once and then run this program on multiple operating systems.&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Java has the following properties:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;div class=&quot;itemizedlist&quot;&gt;&lt;ul type=&quot;disc&quot;&gt;&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Platform independent: Java programs use the Java virtual machine as abstraction and do not access the operating system directly. This makes Java programs highly portable. A Java program which is standard complaint and follows certain rules can run unmodified all several platforms, e.g. Windows or Linux.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Object-orientated programming language: Except the primitive data types, all elements in Java are objects.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Strongly-typed programming language: Java is strongly-typed, e.g. the types of the used variables must be pre-defined and conversion to other objects is relatively strict, e.g. must be done in most cases by the programmer.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Interpreted and compiled language: Java source code is transfered into byte-code which does not depend on the target platform. This byte-code will be interpreted by the Java Virtual machine (JVM). The JVM contains a so called Hotspot-Compiler which translates critical byte-code into native code.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Automatic memory management: Java manages the memory allocation and de-allocation for creating new objects. The program does not have direct access to the memory. The so-called garbage collector deletes automatically object to which no active pointer exists.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The Java syntax is similar to C++. Java is case sensitive, e.g. the variables myValue and myvalue will be treated as different variables.&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;u&gt;Development with Java&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small; font-weight: normal;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;The programmer writes Java source code in an text editor which supports plain text. Normally the programmer uses an&amp;nbsp;&lt;a class=&quot;ulink&quot; href=&quot;http://www.vogella.de/articles/Eclipse/article.html&quot; style=&quot;color: #0000cc;&quot; target=&quot;_top&quot;&gt;IDE&lt;/a&gt;&amp;nbsp;(integrated development environment) for programming. An IDE support the programmer in the task of writing code, e.g. it provides auto-formatting of the source code, highlighting of the important keywords, etc.&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;At some point the programmer (or the IDE) calls the Java compiler (javac). The Java compiler creates platform independent code which is called bytecode. This byte-code is stored in &quot;.class&quot; files.&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Bytecode can be executed by the Java runtime environment. The Java runtime environment (JRE) is a program which knows how to run the bytecode on the operating system. The JRE translates the bytecode into native code and executes it, e.g. the native code for Linux is different then the native code for Windows.&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.codeinjava.com/feeds/1472528691942275376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3365499786104773673&amp;postID=1472528691942275376&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1472528691942275376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3365499786104773673/posts/default/1472528691942275376'/><link rel='alternate' type='text/html' href='http://www.codeinjava.com/2011/04/introduction-to-java.html' title='Introduction to Java'/><author><name>Harish Sharma</name><uri>https://plus.google.com/116164131645780363159</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh4.googleusercontent.com/-GtIS_0_YYQM/AAAAAAAAAAI/AAAAAAAAEOA/Liz-AYg1RSs/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry></feed>