Comments in Java are identical to those in C++. Everything between /* and */ is ignored by the compiler, and everything on a single line after // is also thrown away. Therefore the following program is, as far as the compiler is concerned, identical to the first HelloWorld program.

// This is the Hello World program in Java
class HelloWorld {

  public static void main (String args[]) { 
    /* Now let's print the line Hello World */
    System.out.println("Hello World!");
  } // main ends here

} // HelloWorld ends here

The /* */ style comments can comment out multiple lines so they're useful when you want to remove large blocks of code, perhaps for debugging purposes. // style comments are better for short notes of no more than a line. /* */ can also be used in the middle of a line whereas // can only be used at the end. However putting a comment in the middle of a line makes code harder to read and is generally considered to be bad form.

Comments evaluate to white space, not nothing at all. Thus the following line causes a compiler error:

int i = 78/* Split the number in two*/76;

Java turns this into the illegal line

int i = 78 76;

not the legal line

int i = 7876;

This is also a difference between K&R C and ANSI C.

Previous | Next | Top | Cafe au Lait

Copyright 1997-2006 Elliotte Rusty Harold
Last Modified September 5, 1997