Basic Syntax


🙋 Need help? Ask an expert now!

A "comment" is a message written to a human reader of the program. A comment is denoted by // at the beginning of the line. Those two characters (//) and everything that follows on the one line are disregarded by the Java compiler. Please take a look at the example below showing the use of comments.
package exlcode;

public class CommentsExample {
  /* Comment Block

  You can write as much as you want inside the comment block

  // this is another example of a comment

  public static void main (String[] args){
    // this will not print because it is a comment
    // System.out.println("Hello!");
    System.out.println("Hello World!");

In the example above, some comments span for more than one line. To get the Java compiler to ignore multiple lines, we use /* at the beginning of the comment and */ at the end of the numerous lines of comments. When using this method to add a lengthy comment, make sure there are no characters between '/' and '*'. The /* and */ can start and stop anywhere on a line. Everything between the pair is a comment ignored by the compiler.

In addition to marking an actual comment in this way, we can also use the comment functionality for debugging. If we run into a situation where the program has a syntax error, one way to search for the root of the error is to remove some of the code from being compiled by turning it into a comment. If the modified program works as expected, the error must be in the section of code that was made into a comment. From there, we can decrease the amount of code in the comments until we pinpoint the error. Please remember this useful and important method for debugging long lines of code.

Edit Me on GitHub!

Application Question

Which of the following lines are NOT proper Java comments?