dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jun 14, 2021

How to Write Comments in Java

When we create programs and applications in Java, it is always important to document - or add comments - to our code. These comments are invisible to the Java compiler, and thus, are completely ignored. However, they serve a very important purpose to you and any future programmers that might be reviewing your code.

Comments are a way of leaving notes to yourself, other members of the software development team, or any future Java developers that might need to fix or update your code. We want to use comments only where needed so as not to make our code cluttered and hard to read. The basic rule is this: only comment to explain the purpose of a code snippet and its purpose.

Single Line Comments in Java

There are two types of comments in Java (and most programming languages) - the single-line comment and the multi-line comment.

Single line comments begin with two forward slashes or //. Any text after the double forward slashes - and on the same line - is invisible to the compiler. Here is how we would write a single line comment:

// This is how you create a program to print text to the user's window

// Creating the class definition
 
class Main {
            	public static void main(String[] args) {
            	{
                            	//print statement to print some text
                            	System.out.printIn("Here is some text!");
            	}
}

Multi-Line Comments in Java

There are times where the comments you need to write span more than just a single line. Or, you might want to make your comments easier to read or designed in a certain way. In these instances, you could just use a bunch of single-line comments, back-to-back, like so:

//This program was created by:
//James Payne
//World's greatest writer and programmer!
 
// This is how you create a program to print text to the user's window
 
// Creating the class definition
 
class Main {
            	public static void main(String[] args) {
            	{
                            	//print statement to print some text
                            	System.out.printIn("Here is some text!");
            	}
}

However, this can be tedious over time and is less efficient than simply using multi-line comments. Multi-line comments begin with /* and end with */. The text in-between is the actual comments that the compiler will ignore. Here is how you would comment the above single line example using multi-line comments:

/*This program was created by:
James Payne
World's greatest writer and programmer!
This is how you create a program to print text to the user's window
*/
 
// Creating the class definition
 
class Main {
            	public static void main(String[] args) {
            	{
                            	//print statement to print some text
                            	System.out.printIn("Here is some text!");
            	}
}
James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date