Java CommentsThe Java comments are the statements in a program that are not executed by the compiler and interpreter. Why do we use comments in a code?
Types of Java CommentsThere are three types of comments in Java.
1) Java Single Line CommentThe single-line comment is used to comment only one line of the code. It is the widely used and easiest way of commenting the statements. Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java. Syntax: Let's use single line comment in a Java program. CommentExample1.java Output: 10 2) Java Multi Line CommentThe multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code snippet or to comment multiple lines of code at a time (as it will be difficult to use single-line comments there). Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by Java. Syntax: Let's use multi-line comment in a Java program. CommentExample2.java Output: 10 Note: Usually // is used for short comments and /* */ is used for longer comments.3) Java Documentation CommentDocumentation comments are usually used to write large programs for a project or software application as it helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods, arguments, etc., are used in the code. To create documentation API, we need to use the javadoc tool. The documentation comments are placed between /** and */. Syntax: javadoc tagsSome of the commonly used tags in documentation comments:
Let's use the Javadoc tag in a Java program. Calculate.java Compile it by javac tool: Create Document Create documentation API by javadoc tool: Now, the HTML files are created for the Calculate class in the current directory, i.e., abcDemo. Open the HTML files, and we can see the explanation of Calculate class provided through the documentation comment. Are Java comments executable?Ans: As we know, Java comments are not executed by the compiler or interpreter, however, before the lexical transformation of code in compiler, contents of the code are encoded into ASCII in order to make the processing easy. Test.java Output: The above code generate the output because the compiler parses the Unicode character \u000d as a new line before the lexical transformation, and thus the code is transformed as shown below: Test.java Thus, the Unicode character shifts the print statement to next line and it is executed as a normal Java code. Next TopicJava Programs |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India