Java: Comment Text Introduction Comment text is text included in source code that is ignored by the compiler and does not cause any machine-language object code to be generated. It is written into the source code to add clarity for the reader. This article demonstrates inclusion of comment text in source code. The comment program expands on the hello program and adds comments. Concepts construct Programs are made up of (or constructed from) programming language constructs. A programming language construct is a portion of the program that defines an entity within it. A programming language construct can be defined as any building block of the program. comment A comment is a programming language construct used to embed information into the source code of a computer program. When the source code is compiled, comments are ignored. Comment text does not affect the running of the compiled program or increase the size of the executable file. It is written into the source code to add clarity for the reader. Comments are normally used to document code for future reference and to clarify it. The comment program adds comments to source code from the hello program.
Source Code The source code listing is as follows: /* comment.java Program containing comment text. environment: language Java platform console */ // Program. class comment { // Entry point. } public static void main( String[] args ) { System.out.println( "Hello World" ) ; // Display "Hello World". }
Compiling and Running 1. Save the source code listing into a file named comment.java. 2. Launch a Windows command prompt. 3. Navigate to the directory comment.java was saved in. 4. To compile the program, type: > javac comment.java 5. To test the program, type: > java comment
Code Explanation Comment text is inserted into the code in one of the following ways: // The two sequential characters // followed by any sequence of characters on the same line of source code are ignored by the compiler. The ignored text begins with // and ends with a carriage return (new line). This enables a single line of comments to be inserted into the code either following an instruction or on a line by itself. This kind of comment is therefore commonly called a single-line comment. /* */ The two sequential characters /* followed by any sequence of characters and then followed by the two sequential characters */ are ignored by the compiler. The ignored text begins with /* and ends with */. This enables multiple lines of comments to be inserted. This kind of comment is therefore commonly called a multi-line comment. /* comment.java Program containing comment text. environment: language Java platform console */ Text beginning with characters /* and ending with */ is ignored by the compiler. This enables inclusion of comment text which extends to more than one line. This multi-line comment is used to provide introductory text for the program. // Program. Text that begins with characters // and continues to the end of the line of code is ignored by the compiler. This single-line comment is used to describe the program. In this example all text from // to Program. is ignored by the compiler. // Entry point. This single-line comment is used to describe the main method. In this example all text from // to point. is ignored by the compiler.
System.out.println( "Hello World" ) ; // Display "Hello World". Comment text can be inserted into the code following an instruction. This comment explains the purpose of statement System.out.println( "Hello World" ) ;.