Comments in Java

Comments in Java, as in most programming languages, do not show up in the executable program. Thus, you can add as many comments as needed without fear of bloating the code. Java has three ways of marking comments.

The most common form is a //. Use this for a comment that runs from the // to the end of the line.

System.out.printtn(“We wit! not use ‘Hello, World!'”); // is this too cute?

When longer comments are needed, you can mark each line with a //, or you can use the /* and */ comment delimiters that let you block off a longer comment.

Finally, a third kind of comment is used to generate documentation automat­ically. This comment uses a /** to start and a */ to end. You can see this type of comment in Listing 3.1. For more on this type of comment and on automatic documentation generation, see Chapter 4.

Listing 3.1 FirstSample/


* This is the first sample program in Core Java Chapter 3

* @version 1.01 1997-03-22

* @author Gary Cornell


public class FirstSample


public static void main(String[] args)


System.out.println(“We will not use ‘Hello, World!'”);



Source: Horstmann Cay S. (2019), Core Java. Volume I – Fundamentals, Pearson; 11th edition.

Leave a Reply

Your email address will not be published. Required fields are marked *