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

Course Content

Java Basics

Comments 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 allow you to leave notes on precisely what the code does. You can do this for yourself or for other programmers who will be editing your code;
  • Comments can also temporarily exclude a code fragment from compilation. For example, if your code is producing an error, and you suspect which specific code fragment may be causing it, you can use /* code fragment that can cause an error */ to temporarily disable it;
  • You can use comments to write documentation for your code. When you become an advanced senior programmer, you will frequently write code that other programmers will use. Documentation is crucial for large projects, but you don't have to worry too much about it while learning. 🙂

You could see an example of commenting in the previous chapter, where there was a comment in place of the code you were supposed to write.

Here's another example of using commenting:

java

Main.java

Only the first and third messages will be displayed here because the second fragment is commented out, and the compiler doesn't see it.

Let's look at the example of multi-line code commenting:

java

Main.java

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

Task

Now, let's examine a code fragment that contains an error. Your task is to identify the error and comment out the code fragment that contains it.

Note

Remember to enclose the text in double quotes!

Everything was clear?

Section 1. Chapter 7
toggle bottom row
course content

Course Content

Java Basics

Comments 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 allow you to leave notes on precisely what the code does. You can do this for yourself or for other programmers who will be editing your code;
  • Comments can also temporarily exclude a code fragment from compilation. For example, if your code is producing an error, and you suspect which specific code fragment may be causing it, you can use /* code fragment that can cause an error */ to temporarily disable it;
  • You can use comments to write documentation for your code. When you become an advanced senior programmer, you will frequently write code that other programmers will use. Documentation is crucial for large projects, but you don't have to worry too much about it while learning. 🙂

You could see an example of commenting in the previous chapter, where there was a comment in place of the code you were supposed to write.

Here's another example of using commenting:

java

Main.java

Only the first and third messages will be displayed here because the second fragment is commented out, and the compiler doesn't see it.

Let's look at the example of multi-line code commenting:

java

Main.java

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

Task

Now, let's examine a code fragment that contains an error. Your task is to identify the error and comment out the code fragment that contains it.

Note

Remember to enclose the text in double quotes!

Everything was clear?

Section 1. Chapter 7
toggle bottom row
some-alt