As you become more of a proficient programmer and start to create larger more complex programs, you will find out how important it is to organize your code and be able to quickly identify what your code is doing. Fortunately, Java has a way to write comments within our code that we can write to ourselves, or for others who will be working with our code.
The ways to create Java comments are either with 2 backslashes
// or with backslashes and apostrophes:
/* insert comment here */ . Also, although not necessary for our usage yet, you can create what is called a Javadoc comment. This is created by adding another apostrophe at the beginning:
/** insert Javadoc comment here */ . Let’s go over some examples!
Using the double backslash method we would take the following broken code:
And simply add the 2 backslashes in front of our comment to make it work:
Also, it is important to know that using the double backslash commenting technique only comments one line.
But what about if we want to comment out multiple lines of code? The best way to do that is to use the /* */ technique.
Instead of using backslashes for each line as show below:
It is much more simple to use /* */ .
One more piece of advice is that good code should not have a plethora of comments. Although it is a helpful tool, there can be too much of a good thing. Instead, try to write clear, concise, and organized code to help yourself and others know how your program works.