Welcome to a tutorial on the use of comments within Java.
What are comments?
Comments are just like the standard definition of comments anywhere in the world, they are used to explain something within the midst of the code. Comments are not read by Java and as such do not affect the program running.
When are comments used?
Comments are normally used to either make a note for the developer their self so they know where they need to go back to, or bookmark a specific location within a Java file. Comments are also very handy for large scripts, or for sending a block of sample code as you are able to annotate what each line does.
What is the format for a comment?
To submit a comment within Java, there are two ways to do this, the first one is a simple, one line comment which begins with two forward slashes ("//"). The second way allows multiple comment lines in a row which start with an asterix ("*") and is initialised by "/**" and terminated with "*/". Examples below.
Here is an example of both types of comment...
- //System.out.println("1 This line is a comment and so is not ran by Java.");
- System.out.println("2 This line is not a comment and so is ran by Java.");
- * System.out.println("3 This line is a comment and so is not ran by Java.");
- * System.out.println("3.5 This line is a comment and so is not ran by Java.");
- System.out.println("4 This line is not a comment and so is ran by Java.");
2 This line is not a comment and so is ran by Java. 4 This line is not a comment and so is ran by Java.
As you can see, the three lines in the comment types are not ran by Java.