Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Comments | Getting Started
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

book
Comments

Hiding code in from the compiler

You can hide code from the compiler by commenting out the code.

Code comments are fragments that the compiler will not process.

You can comment out lines of code using the // symbols or enclose a specific fragment of text or code using /* */.

Why do we need comments?

  • Comments let you write notes to explain what parts of the code do. These notes are ignored by the computer but make the code easier to understand for yourself or others;

  • Comments can temporarily disable parts of your code. For instance, if you think a specific line is causing an error, you can comment it out (/* code fragment */) to test the program without that line;

  • Comments can be used to describe how your code works, making it easier to understand later or for others working with your code.

You saw an example of commenting in the previous chapter, where a comment was placed where the code was supposed to go.

Here's another example of how to use comments:

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { System.out.println("Message 1"); // System.out.println("Message 2"); System.out.println("Message 3"); } }

Only the first and third messages will be displayed here because the second part is commented out, so the compiler ignores it.

Now, let's take a look at an example of multi-line code commenting:

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { /* System.out.println("Message 1"); System.out.println("Message 2"); */ System.out.println("Message 3"); } }

As you can see, only the third message is displayed in the console because the first and second ones are commented out.

Task
test

Swipe to begin your solution

Your task is to identify the error and comment out the code fragment that contains it.

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
toggle bottom row

book
Comments

Hiding code in from the compiler

You can hide code from the compiler by commenting out the code.

Code comments are fragments that the compiler will not process.

You can comment out lines of code using the // symbols or enclose a specific fragment of text or code using /* */.

Why do we need comments?

  • Comments let you write notes to explain what parts of the code do. These notes are ignored by the computer but make the code easier to understand for yourself or others;

  • Comments can temporarily disable parts of your code. For instance, if you think a specific line is causing an error, you can comment it out (/* code fragment */) to test the program without that line;

  • Comments can be used to describe how your code works, making it easier to understand later or for others working with your code.

You saw an example of commenting in the previous chapter, where a comment was placed where the code was supposed to go.

Here's another example of how to use comments:

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { System.out.println("Message 1"); // System.out.println("Message 2"); System.out.println("Message 3"); } }

Only the first and third messages will be displayed here because the second part is commented out, so the compiler ignores it.

Now, let's take a look at an example of multi-line code commenting:

java

Main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { /* System.out.println("Message 1"); System.out.println("Message 2"); */ System.out.println("Message 3"); } }

As you can see, only the third message is displayed in the console because the first and second ones are commented out.

Task
test

Swipe to begin your solution

Your task is to identify the error and comment out the code fragment that contains it.

Solution

java

solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt