Comments in java, Overview:
Comments in Java– In this article, you will learn how to use single line and multiline comments in Java programming. Comments are almost provided in all the programming languages. You will find comments in C/C++, Visual Basic, Arduino IDE, C#, Visual C++, etc. The comments syntax may be different but used for the same purpose. Of course comments are optional, but it is considered to be a good programming practice, if the comments are frequently used.
Who does not know the situation? A longer calculation has been carried out, wrote an article or worked out any sketch – and must do this work now explain to other people. Unfortunately the invoice, the article or the Sketch a few days older and you can’t remember every detail every logical step. How can you quickly understand your work? A few days back I opened an old code which I think I wrote in 2012, I was amazed, when I was not able to understand the logic, and kept thinking how I did it? Why I did it? I forgot it completely.
In important cases, it was therefore ensured that someone else (or oneself) can understand this work later. Marginal notes, footnotes and explanatory diagrams are used for this purpose – additional comments that are not part of the actual paper are. Our programs are also getting bigger over time. We therefore need a possibility to provide our text with explanatory comments. However, since highlighters do badly on the monitor, the language has Java their own way of dealing with comments:
Suppose we have written a line of program and want to work on it later remember what it’s all about. The simplest way would be to use a Insert a remark or a single line comment directly into the program text. In our example this is done as follows:
a = b + c; // a comment begins here
As soon as the Java compiler finds the characters // on a line, it recognizes a single line Comment. Everything that follows after these characters is “none of your business” and Java is ignored by the translator. The comment can therefore be from all possible There are characters that Java provides as input characters. The comment ends with the end of the line in which it started. However, sometimes there may be comments on more than one Line should extend. We can of course add a comment character to each line provided something like this:
// Line 1
// Line 2
// line n
This means, however, that we also add further multiline comment do not forget to put the comment characters at the beginning of each line. For this reason, Java provides a second form of comment.
We start a multiline comment with the characters / * and end him with * /. Text of any length can be placed between these characters (the Of course, the character string * / must not contain, otherwise the comment will already be would be ended at this point), as the following example shows:
/ * Comment …
still comment …
last comment line …
We want to get down to the habit of commenting on our programs make it as sensible as possible and comment frequently. If you have to store an equally large number of files, the overview is not that easy. To give us the right style when commenting on Java source texts to get used to it, we want to stick to the so-called JavaDoc format. JavaDoc is a very helpful additional program that every JDK (Java Development Kit) or JSDK (Java Software Development Kit)  is included free of charge. With the help of JavaDoc, after successful programming, generate complete documentation for the programs created, which can save you a lot of work in retrospect, provided you follow your program has commented diligently. The functional scope of JavaDoc is of course much larger than what we see in the Within the scope of this section. However, it would make little sense to to go into this point more closely – after all, we want to learn to program!
Apart from that, the program is constantly being developed – interested Readers should therefore read the JavaDoc documentation, which is included in the Online documentation for each JDK is included, is warmly recommended. JavaDoc comments, like general comments, always begin with the character string / ** and end with * /. It has become natural, at the beginning everyone Line of the comment to put an additional * to include comments visually different from the rest of the source code. A typical JavaDoc comment at the beginning would be, for example, the following:
- This program calculates the lottery numbers for the next
Week. In doing so, it achieves an accuracy on the cut
- @version 1.0
Let’s take a look at the details in this example:
- The first few lines contain a general description of this Program. It is important to ensure that the information is also provided without the source text at hand should make sense, as JavaDoc from these comments separate files are created later – ideally someone has to who reads the help documents generated by JavaDoc without looking at it on the source code can understand what the program does and how to it calls.
- Next we see various comment commands that always start with the Characters @ are introduced. Here you can find certain predefined information for this program. The order plays a role the information does not matter. JavaDoc now also recognizes a lot more Comment commands than those listed here, but let’s get down to business first focus on the essentials.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!