- Just as you could use the number sign (#) to let the Perl interpreter
know that a line in the codewas a comment, you can specify comments in Java
-
Actually, there are several ways todenote comments in Java
-
For single line comments, you can use the "//" notation. For example, consider the following code:
// Initialize variables...
int age = 28; // Declare and initialize age
- Single line commentscan be defined on the same line as your code or on their ownline
- To specify multiline comments, however,you will use the "/*" and "*/" combination such as in the followingexample:
/*
This is a sample class which is used to demonstratethe use of multi-line comments.
It was written by Selena Sol for the Web Programming Tutorial
*/
// Define an Example class with a single
// main() method which will print out
// "Hello Cyberspace!" to standard output.
public class Example
{ public static void main(String[] args)
{ System.out.println("Hello Cyberspace!");
}
}
-
As you can see, if you use the "//" notation and your comment spans multiple lines,
you must begin each new line with a "//" combination.
- Finally, if you are writing comments which should be included as part of a javadoc document, you should use the "/**" "*/" combination such as in the followingexample:
/*
This is a sample class which is used to demonstratethe use of multi-line comments.
It was written by Selena Sol for the Web Programming Tutorial. This commentdoes not appear in the java documentation
*/
/**
Define an Example class with a single
main() method which will print out
"Hello Cyberspace!" to standard output.
This comment appears in the javadoc.
*/
public class Example
{
public static void main(String[] args)
{
System.out.println("Hello Cyberspace!");
}
}
Previous Page |Next Page
|