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

Cursusinhoud

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.

Taak

Swipe to start coding

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

Oplossing

java

solution

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 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.

Taak

Swipe to start coding

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

Oplossing

java

solution

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 7
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt