Java TUTORIAL for beginners


It is always a good idea to add lots of comment in the source code. This makes the program easy to understand for others. More importantly, it is easy for youself, should you need to refer your own code a couple of years later.

Adding comments in Java is no different than adding comments in C or javascript. There are two ways you can add comments in Java. The single line comments start with // and can be instered at the end of a Java statement. Look at the following example

  1. class HelloWorld {
  2.  
  3. public static void main (String args[]) {
  4.  
  5. System.out.println("Hello World!"); // Prints Hello world
  6. // The following prints something in another line
  7. System.out.println("Learning Java is easy and fun with referencedesigner.com tutorial!");
  8. }
  9.  
  10. }
  11.  


There is absolutely no difference in the way the program compiles or gives output. Everything is same. All we have done is addes two comments. In complex programs, comments do wonders in enhancing the readability of the code.

A multiline comment starts with /* and ends with */. See how a multiline comment looks like.

  1. class HelloWorld {
  2.  
  3. public static void main (String args[]) {
  4.  
  5. System.out.println("Hello World!"); // Prints Hello world
  6. /* Multi line Commet Starts
  7. We will print another line using System.out.println
  8. */
  9. System.out.println("Learning Java is easy and fun with referencedesigner.com tutorial!");
  10. }
  11.  
  12. }
  13.  


As you can see, the multiline comments come handy if you wish to provide more information about your source code.

There is yet another type of comment that starts with /** and ends with */. In terms of functionaliry it is same as the one that starts with /* and ends with */. The only difference is it is called a documentation comment. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.

In the next post we will spend a little bit of time understanding the code behind our Hello World program