Skip to main content

Java Comments

Comment is line of text which are used to describe the logical thinking and code explanation of source code. That is useful for programmers to understand the concept of code. In other words this are small descriptive notes and instruction which are written by programmer. Commented line are not read by compiler or by compiler is ignored this commented text.

Comment Syntax and Usage

In Java programming comment are mainly three types. short notes can be written by single line comment, when comment text is to long for one line then you'll can used multiline comment. And other is form of documented comments which are normally used by programmer. In this section given explanation of all of them. Let's First view the syntax of single line comments.

// comment text

Single line comment start with double (//) forward slash. and after that defining text content is part of single line comment. Here given few example which are use single line comment.

//This is an student class
public class Student{
  
  //Execution of Program start within the main program
  public static void main(String[] args){
    //Define programming instruction inside main method

    int marks,age; //Defined  the two integer variable

  }
}

Single line comment are commonly defined after the statement and they are defined top of the statement in separate line. Both are valid.

probably all special character are possible to use within the commented text. But some special unicode character which are create \u000a new line that is possible it not work all cases. For example

class Test{
  public static void main(String[] args) {

    // This is main method \u000a Write your code
  }
}
Test.java:4: error: ';' expected
    // This is main method \u000a Write your code
                                            ^
Test.java:4: error: not a statement
    // This is main method \u000a Write your code
                                             ^
Test.java:4: error: ';' expected
    // This is main method \u000a Write your code
                                                 ^
3 errors

We can avoid this type of situation in two way remove unicode characters. or use again use that // double forward slash after the unicode character.

class Test{
  public static void main(String[] args) {

    // This is main method \u000a //Write your code
  }
}

This code are compiled without any error. Unfortunate when commented text are exist unicode of new line after that in this comment are exist any java statement then it will work. see this

class Test{
  public static void main(String[] args) {
    int a = 10;
    // Increment \u000a a++;
    System.out.println(a);
  }
}
11

Multiline Comment

That is second variant of comment. This comment text is surrounded by the ( start with /* forward slash abstract and end with */ ).

/* That is example of 
Multiline comment */




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment