This segment will deal with a very important and good practice for you to follow while writing programs. How to write comments in Java and why it makes sense to do this are what you will learn here.
Whenever you are writing programs in Java, it is a good practice to write comments also in Java programs. So basically, comments are statements such as these which give you more clarification as to what you are writing in your code. A comment in Java can be written by putting two slashes like this. So if you wish to write a comment, you can just put two lashes like this and write your comment here.
So basically, for example, in a program like this, if here you are declaring the marks of two marks in two different integer variables, it is good that you write a comment here indicating that you are declaring variables. Similarly, in these two statements, when you are assigning values to these two marks, I have written a comment here indicating that I am assigning values and initializing these variables similarly, a comment for declaring variables in a single line and finally printing out the values of marks and of student names. In small programs like these, you might be able to understand the code even without these comments. But imagine that you are making huge projects which consists of tens and hundreds of files and complex programs. In those cases, it is a good practice to write comments for each and every important portion of the code, so that even if some other programmer tries to understand the code, he or she is easily able to comprehend what the code is actually doing. In comments, usually we declare or we write whatever the following code is going to do. For example, if you are doing a calculation here such as adding marks one and marks two, say int total is equal to marks one plus marks two, then it is a good practice to ensure that even other programmers can easily understand what is being talked about. So this is about comments in Java.
Writing comments in Java programs is a good practice to provide clarification about the code.
Comments in Java are written by putting two slashes (//) and writing the comment.
Comments should be written for declaring variables, assigning values, and important portions of code.
In large projects with complex programs, comments help other programmers understand the code easily.
Comments usually declare or explain what the following code is going to do.
Writing comments is important even for simple calculations to ensure easy comprehension by other programmers.
You just saw how writing comments makes a code look completely different when it comes to understanding what the code and various functions inside it are doing. You will find them even more useful as you progress through the course and start writing bigger and more complex codes.