Learn Java

Learn Java Comments

Learn Java Comments

We use Comments to explain Java code and to make it more readable. Comments are also used to prevent execution when testing the alternative or another code.

 

Single-Line Comments

If you want to make a line comment, then use double forward slashes like this //. Any text after these two double slashes will be ignored by java. This means the code after these double quotes will not be executed.

This example uses a single-line comment before a line of code:

public class first {


    public static void main(String[] args) {


        System.out.println("Hello From H4C");
        System.out.println("Hello From Home4Cloud or H4C");
        // System.out.println("I will print on the same line.");


    }


}

 

Output

PS C:\Users\Priyanka\Documents\java> javac first.java
PS C:\Users\Priyanka\Documents\java> java first
Hello From H4C
Hello From Home4Cloud or H4C
PS C:\Users\Priyanka\Documents\java>

 

Image for Output:

double slashes comments

 

 

Java Multi-line Comments

All the multi-line comments start with /* and end with */.

Any text between /* and */ is ignored by Java.

This example uses a multi-line comment or a comment block to explain the code:

 

Example:

public class first {
    public static void main(String[] args) {
        System.out.println("Hello From H4C");
        System.out.println("Hello From Home4Cloud or H4C");
        /*
         * System.out.println("I will print on the same line.");
         * Write anything here..
         * it will be ignopred by the java compiler
         */
    }
}

Output:

PS C:\Users\Priyanka\Documents\java> javac first.java
PS C:\Users\Priyanka\Documents\java> java first
Hello From H4C
Hello From Home4Cloud or H4C

 

Output with an Image:

multi line code in java