Example of a Java Program with Comments
Take a look at the example below. You will notice that some of the text does not look like Java code - they are normal English sentences. Since Java will not understand these sentences, we use certain characters to denote that sections of text are comments.If we want to include a comment that is longer than one line, we put /* at the beginning of the comment and */ at the end.
If we want to include a comment on only one line, we use //. The text that appears after // is a comment.
/**
* MyFirstJavaProgram
*
* This program shows you how to print a sentence.
*/
public class MyFirstJavaProgram
{
public static void main(String args[])
{
System.out.println("This is what a Java program looks like");//example of a print statement
}
}
When a program is compiled, the compiler ignores the comments. This means that comments do not affect the program. So, why do we use comments? Comments are only for you and any one else that reads your source code. To see the value of comments, imagine someone gives you the source code to a long, complex program which does not contain any comments. If you wanted to understand the code you would have to spend a lot of time going through each line and figuring out what everything does. However, if this program had comments that explained the purpose of each class, method, and block of code, it would make understanding the code a lot easier.



Comments

