Learn about comments, keywords & blank lines in Java Programming

Comments in Java Programming Language
Comments in Java programming language can be used to describe the purpose of a segment or line of Java code. It is also used to explain a Java program and make it more readable for other developers. Additionally, it can be used to prevent execution when testing alternative code blocks.
Types of Comments in Java Programming Language
Single-line comments
A single-line comment begins by writing two forward slashes //
. The Java compiler will ignore and not execute any text, code, or content that is preceded by the two forward slashes //
symbol.
- Java Code
// This example demonstrates the use of a single-line comment
public class TutorialClass {
// This is main function will print 'Hello World Cheers! From Sikademy.' as the output
public static void main(String[] args) {
System.out.println("Hello World\n\nCheers! From Sikademy."); // This line uses the println to print the text
//This code will not be executed.
// System.out.println("Hello World\n\nCheers! From Sikademy.");
}
}
Save, compile, and execute the program. If there are no other errors, your output should read:
- Output
Cheers! From Sikademy.
Multi-line comments
Multi-line comments begin with the symbol /*
(a forward slash followed by a star) and end with the symbol */
(a star followed by a forward slash). The Java compiler will ignore and not execute any text, code, or content written between the /*
and */
symbol.
Let's rewrite the example above using a multi-line comment
- Java Code
/* This example demonstrates the use of a multi-line comment
also called a comment block
In the class "TutorialClass", the main function will print
'Hello World Cheers! From Sikademy.' as the output
It makes use of the println to print the text */
public class TutorialClass {
public static void main(String[] args) {
System.out.println("Hello World\n\nCheers! From Sikademy.");
/* This code will not be executed.
System.out.println("Hello World\n\nCheers! From Sikademy.");
*/
}
}
Save, compile, and execute the program.
Your output should read:
- Output
Cheers! From Sikademy.
Reserved Keywords in Java Programming
Reserved words are words that cannot be used as names of objects, variables, classes, or any other identifiers in a Java program because they are already used by the syntax of the Java programming language.
The following list shows the reserved words in the Java Programming language
abstract | assert | boolean | break | byte | case |
catch | char | class | const | continue | default |
do | double | else | enum | extends | false |
final | finally | float | for | goto | if |
implements | import | instanceof | int | interface | long |
native | new | null | package | private | protected |
public | return | short | static | strictfp | super |
switch | synchronized | this | throw | throws | transient |
true | try | void | volatile | while |
What Happens if You Use a Reserved Keyword as an Identifier?
If you attempt to use any of the words listed above as an identifier in your Java programs, you will get an error like the one below. Let's say you try to create a new class and name it using a reserved word, like this:
- Java Code
// you can't use super as it's a reserved word!
public class super {
public static void main(String[] args) {
System.out.println("Hello World\n\nCheers! From Sikademy.");
}
}
Save and compile the program. You should get a result similar to the one below:
- Java Error
TutorialClass.java:2: error: expected
public class super {
^
TutorialClass.java:3: error: illegal start of expression
public static void main(String[] args) {
^
TutorialClass.java:3: error: illegal start of expression
public static void main(String[] args) {
^
TutorialClass.java:3: error: ';' expected
public static void main(String[] args) {
^
TutorialClass.java:3: error: '.class' expected
public static void main(String[] args) {
^
TutorialClass.java:3: error: ';' expected
public static void main(String[] args) {
^
TutorialClass.java:6: error: reached end of file while parsing
}
^
7 errors
Using Blank Lines in Java Codes
A line containing only white space, and sometimes with a comment, is known as a blank line. It can be used to enhance the readability of codes and nothing else. In Java, it will be totally be ignored.
- Java Code
/* This example demonstrates the use of a multi-line comment
also called a comment block
In the class "TutorialClass", the main function will print
'Hello World Cheers! From Sikademy.' as the output
It makes use of the println to print the text */
public class TutorialClass {
public static void main(String[] args) {
System.out.println("Hello World\n\nCheers! From Sikademy.");
/* This code will not be executed.
System.out.println("Hello World\n\nCheers! From Sikademy.");
*/
}
}
Save, compile, and execute the program.
Your output should read:
- Output
Cheers! From Sikademy.
Tutorial Exercise
1. Create a file with the name "Super.java" and rewrite the class in our example as "Super". Compile and Execute the Program. What output do you get?
2. Again, Create a file with the name "super.java" and rewrite the class in our example as "super". Compile and Execute the Program. What output do you get?
3. Compare both outputs with the output in the keywords section of this tutorial.