Comments in Java


Before Starting Know-How
Basic Output

The course is part of this learning path

Start course

In this course, we touch on key topics that should be known before learning the basics of Java. 

Learning Objectives

  • Java Output
  • Java Input
  • Java Comments

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge

Hi there. In this video, we will learn about Java comments and why and how to use them. The comments can be used to provide information or explanation about any statement, variable, method, and class. Note that comments should contain only relevant information to read and understand the program. Writing comments is a good practice and will help others to understand our code. It will also help us when we return later to our code. The comments can be used to hide program code in some cases. 

They are completely ignored by Java compilers. There are three types of comments in Java: single line comment, multi line comments, and documentation comment. The single line comment is used to comment on only one line. It starts with the // symbol. The compiler ignores everything from // to end of the line. This is the easiest typed comment. The multi line comment is used to comment on multiple lines of code. It starts with /* and ends with */. The compiler ignores everything between /* and */. The doc or documentation comment is used to comment for the document. It starts with /** and ends with */. This indicates a documentation comment, doc comment for short. 

The compiler ignores this kind of comment, just like it ignores comments that use multi line comments. If you want, let's move on to the Eclipse and make some exercise. In the exercise project, right click on the source folder and select new class. Specify package name as comment, and class name as Comment, and select the checkbox to add to the main method. Okay, first I will print the "Welcome to Java" message to the console by using the println method. Let's run the code. You see the "Welcome to Java" text in the console. Now, let's add two single line comments above the println method. We use the // to add a single comment. 

Here you can write any expression you want. Anything you type until the end of the line is ignored by the compiler. As I mentioned before, comments are mostly used to increase the clarity of the codes. Okay, let's run the code again. The compiler ignores these comments. Also Eclipse has a quick shortcut for you to add a single line comment. You can use the CTRL and 7 combination from your keyboard. If you use this combination on the code line, Eclipse automatically adds a // at the beginning of the line. If you use this combination on the same line again, then Eclipse removes the comment marks. Okay, now let's add a multi line comment above the main method. 

We use the multi-comment line between /* and */. Anything you type between /* and */ is ignored by the compiler. We use this more so that a block of code is ignored by the compiler. Let's run the code again. As you can see, the result is the same. We can add a multi line comment to the code block. Let's declare three variables with int type. Firstly, a and assign 5. Second, b and assign 2. And last sum with initial value as 0, and assign a + b to the sum variable. Let's print the variable sum on the screen by using the println method. Okay, let's run the code. If we want to add a multi line comment to a code block, we put /* to the first and end of the code block. Let's run the code. You see only the "Welcome to Java" text because the compiler ignores the comments block. And lastly, let's look at the documentation comment. 

Just above the main method, I put / and ** and press 'Enter'. This is a documentation or doc comment in Java, because after the slash there are two stars. Also notice that the argument of the main method is automatically written here. This comment is mostly used to define the purpose of the method or the class. Later, when you want to use the related method or class in another place, the points you have commented on here are presented to you as a document. Let's create a new method to understand this better if you want. Of course, at the moment, you don't know what the method is or how it is created, but for now, I want you to focus only on comments. 

You'll learn detailed information about the methods in our next lessons. This method will be a method that adds the two values given to it and returns the result of the addition. Let the name of the method be mySumMethod() and I write integer a and integer b as parameters. This method returns the sum of the a and b. Now let's create a doc comment for this method. I put /** on top of the method and press 'Enter'. As you can see, the parameters of the method were created and the return statement were created automatically. Now let's enter our own explanations here. For example, I write "This method sums two integer values" here. 

Let's also enter a description for the parameters. Here I write, a is the first parameter. Here I write, b is the second parameter. Also here I write, this method returns the sum of the a and b. Now let's call this method inside the main method. I type mySumMethod and press 'Enter'. Do not forget to put a semicolon at the end of the line. Now let's position the mouse cursor over this method. As you can see, the statements we wrote as comments are presented here as an explanation of this method. I think the documentation comment is better understood now. Yes friends, here are the comments in Java. I think that you have understood the subject of the comments in Java clearly. Let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics