Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Comments

Java Comments

Comments in Java serve the dual purpose of explaining code for better readability and temporarily disabling code execution when experimenting with alternative approaches.

Single-line Comments

Single-line comments in Java begin with two forward slashes ( // ).

Any text following // until the end of the line is disregarded by Java and will not be executed.

Here’s an example demonstrating the usage of a single-line comment preceding a line of code:

Example

// This is a comment
System.out.println("Hello World");

In this example, a single-line comment is placed at the end of a line of code.

Example

System.out.println(“Hello World”); // This is a comment

Java Multi-line Comments

Multi-line comments in Java are initiated with /* and concluded with */.

Any text enclosed between /* and */ will be disregarded by Java.

Here’s an example demonstrating the usage of a multi-line comment, also known as a comment block, to elucidate the code:

Example

/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");

Single or multi-line comments?

The choice between using // or /* */ for comments is yours. Typically, // is used for brief comments, while / * */ is preferred for longer explanations.