Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Comments | Section
Practice
Projects
Quizzes & Challenges
Frågesporter
Challenges
/
Java Fundamentals

bookComments

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 are notes in the code that are ignored by the computer but help explain what the code does. They make programs easier to understand and maintain, and can also be used to temporarily disable parts of the code for testing or debugging.

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:

Main.java

Main.java

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:

Main.java

Main.java

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.

Uppgift

Swipe to start coding

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

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookComments

Svep för att visa menyn

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 are notes in the code that are ignored by the computer but help explain what the code does. They make programs easier to understand and maintain, and can also be used to temporarily disable parts of the code for testing or debugging.

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:

Main.java

Main.java

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:

Main.java

Main.java

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.

Uppgift

Swipe to start coding

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

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6
single

single

some-alt