Java Comments : In any java program, we can use comments which can be more helpful to programmer to understand that what exactly is that method or line of code is all about and it will make more easier to next coder to understand meaning of any such line of code. The java comments are statements that will get ignored by java compiler while execution.
1) Single Line Comment : Java Single line comment used to comment only one specific line of code. You can use “//” at multiple time in program, there is no restrictions on number of time you are using slash to comment single line.
Syntax : // single line comment example in java
Example :
package com.ajitation.javaComments;
public class SingleLineComment {
public static void main(String[] args) {
// single line comment example in java
String s = "ajitation.com";
System.out.println(s);
}
}
Output: ajitation.com
2) Multi Line Comment : Java Multi line comment used to comment multiple line of code. Instead of using “//” at multiple line it is always better to use java multi line comment syntax, this will save time and efforts of a coder. The multi-line comment starts with “/*” and ends with “*/”. That means any line of code written between /* and */ will be ignored while execution of program.
Syntax : // single line comment example in java
Example :
package com.ajitation.javaComments;
public class SingleLineComment {
public static void main(String[] args) {
// single line comment example in java
String s = "ajitation.com";
System.out.println(s);
}
}
Output: ajitation.com
3) Documentation comment : The Documentation comment used to write any documentation type of line which will help coder to get better idea of implemented class, method, etc.
Syntax :
/**
This is
Documentation comment example
In java
*/
Example :
package com.ajitation.javaComments;
public class DocumentationComment {
public void getStudentname(String name) {
/*
* this method will get name of student
*/
System.out.println("Student_Name : " +name);
}
public static void main(String[] args) {
/*
* this is Documentation comment example in java
*/
DocumentationComment dc= new DocumentationComment();
dc.getStudentname("Ajit");
}
}
Output:
Student_Name : Ajit
So in this section of blog we have seen what exactly Java Comments are, why to use it and syntax of using it.