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

Kursinnehåll

Java Basics

Java Basics

1. Getting Started
2. Basic Types and 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.

Uppgift

Swipe to start coding

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

Lösning

java

solution

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

Uppgift

Swipe to start coding

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

Lösning

java

solution

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 7
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt