Course Content
Java Basics
Java Basics
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:
Main
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
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.
Swipe to begin your solution
Your task is to identify the error and comment out the code fragment that contains it.
Solution
solution
Thanks for your feedback!
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:
Main
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
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.
Swipe to begin your solution
Your task is to identify the error and comment out the code fragment that contains it.
Solution
solution
Thanks for your feedback!