Small Java Programs

This chapter discusses naming and coding conventions as well as reserved words in Java. When you go through this chapter, you'll get some hands-on experience with writing in Java.

18. Commenting Out Code


No. The compiler ignores comments when creating the bytecodes.
Comments are just for human readers of the source program.

Commenting Out Code

public class BuggyHaiku
  public static void main ( String[] args )
    System.out.println("On a withered branch" );
    System.out.println("A crow has just alighted:");
    System.out.println("Nightfall" in autumn.");

Comments are useful for debugging. For example, the above program has a syntax error. Let's say that you are having problems finding the error. One way to narrow down the problem is to remove some of the code from consideration by turning it into a comment. Compile and run the program.

If the modified program works as expected, the error must be in the commented section. Gradually decrease the commented section until you find the error.

Question 18:

What is the error in the program?

Why would you ever want to use comments to help a person understand your program?