Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
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 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

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 fragment is commented out, and the compiler doesn't see it.

Let's look at the 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 being displayed in the console because the first and the second ones are commented out.

Task
test

Swipe to show code editor

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!

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

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 fragment is commented out, and the compiler doesn't see it.

Let's look at the 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 being displayed in the console because the first and the second ones are commented out.

Task
test

Swipe to show code editor

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!

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