Copyright

Comments in Java: Syntax & Example

Instructor: Racquel Fulton

Racquel is a Real Estate Licensee and holds a New Jersey Title Insurance Producer Certification

Many times after you write code you will want to go back to make changes. Putting down comments in your program will help you keep track of increasingly complex programs. In this lesson , you will learn how to write notes for yourself or instructions for someone else with comments.

Comments in Your Code

Let's say that a few months have passed since you wrote a computer program.

You wanted to make changes to your code but can't remember which parts to revise. The good thing is that you do not have to rely on a good memory if you write comments. Comments are a way to write notes for yourself or explanations for other programmers within your code. Comments are only readable to humans and ignored by the compiler. A compiler is a program that converts code making it readable to a computer.

Using the example of a basic 'hello world' application, we will walk through the three ways you can write comments in Java.

Single Line Comments

A single line comment is similar to a sentence. Two forward slashes are placed at the beginning of a line of text.

//my first comment

In the following example, the comment is written above the code. The compiler will run the entire application and ignore the line of text.

//my first comment
public class HelloWorld {
  public static void main(String[] args) {
   System.out.println("Hello World!");
  }
}

Nested Comments

A single line comment can also be written within a block of code. This is called nesting. Nesting is when a comment or an additional block of code is placed inside of another block of code. The compiler will skip over the comment and continue to process the lines of code that follows.

public class HelloWorld {
  //my nested comment
  public static void main(String[] args) {
   System.out.println("Hello World!");
  }
}

You can also write multiple single line comments between several lines of code. See the following example:

//my first comment
public class HelloWorld {
  //my second comment
  public static void main(String[] args) {
   //my third comment
   System.out.println("Hello World!");
  }
}

However, unless you want to document each line of code, a simpler way to write several comments is by grouping each comment together into a multi-line comment.

Multi-Line Comments

Multi-line comments contain a group of text similar to a paragraph. Unlike single line comments, multi-line comments have a beginning and an ending. A Multi-line comment begins and ends with a forward slash and an asterisk . See the following example:

/* This is the beginning of a
multi-line comment
this is the end */
public class HelloWorld {
  public static void main(String[] args) {
   System.out.println("Hello World!");
  }
}

Nested Multi-Line Comments

Multi-line comments can also be nested inside of a code block. The compiler will read the code and skip over each line of the comments no matter how many lines of text there are.

public class HelloWorld {
  /* The Beginning
   This is my first computer program
   written entirely in Java. I am so
   proud of myself
  The End */
  public static void main(String[] args) {
   System.out.println("Hello World!");
  }
}

Single Line Comments Nested Inside Multi-Line Comments

Single line comments can be nested inside of multi-line comments. However, multi-line comments cannot be nested inside of other multi-line comments. See the following example:

public class HelloWorld {
  /* The Beginning
  of a multi line comment
  // This is a nested single line comment
   this is the End */
  public static void main(String[] args) {
   System.out.println("Hello World!");
  }
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support