Archangel Macsika Sikademy Image
Archangel Macsika

Learn about comments, keywords & blank lines in Java Programming

Tutorial 5 of 9 | 5 minutes read
Learn about comments, keywords & blank lines in Java Programming - Sikademy

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 begin 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.

Save, compile, and execute the program. If there are no other errors, your output should read:

Hello World

Cheers! From Sikademy.

Multi-line comments

Multi-line comments begin with the symbol /*(a forward slash followed by a star) and ends 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

Save, compile and execute the program.

Your output should read:

Hello World

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 Java Programming language

abstractassertbooleanbreakbytecase
catchcharclassconstcontinuedefault
dodoubleelseenumextendsfalse
finalfinallyfloatforgotoif
implementsimportinstanceofintinterfacelong
nativenewnullpackageprivateprotected
publicreturnshortstaticstrictfpsuper
switchsynchronizedthisthrowthrowstransient
truetryvoidvolatilewhile

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:

Save and compile the program. You should get a result similar to the one below:

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 readability of codes and nothing else. In Java it will be totally be ignored.

Save, compile and execute the program.

Your output should read:

Hello World

Cheers! From Sikademy.

Tutorial Exercise

1. Create a file with 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 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.


Enjoy this Tutorial? Please don't forget to share.