Ad Code

Comments in Java


 Comments in java

Comments in java are written either single-lined  or multi-lined and are simply ignored by the java interpreter

A set of Lines of code commented will not be executed 

How Comments are Useful?

1)Comments make the program readable 

2)Comments give much information regarding the functionality of variables, Function, class, Object  

3)Comments can be used to eliminate the execution of current code while testing alternative code

Types of Comments

1)Single Line Comments:

A simple way to use comments is to have two forward slashes (//) at the start of the text 

Programmer must note that forward slashes(//) are used only for commenting single-line  

Syntax:

//Single Line comment

Let's get into depth 

Code:

public class SingleLineComment{

public static void main(String args[]){

int id = 20; //variable id gives student-related information

System.out.println("Id of Student:"+id);

}

}

Output:

Id of Student:20


In few cases, Commenting over the Declaration of variables, classes, and functions can give Rise to compile time Errors

Consider the below code:

public class SingleLineComment{

public static void main(String args[]){

//int id = 20; 

System.out.println("Id of Student:"+id);

}

}

output:

Exception in thread "main" java. lang.Error: Unresolved compilation problem: 

id cannot be resolved to a variable

at Comment.main(Comment.java:4)

2)Mutli-Line Comments

Forward Slash(//) Comment becomes complex for multiple Single Lined Snippet code, Thus to Eliminate this scenario multi-lined Comments are used

Syntax:

/* Multi-Lined Comments

Multi-Lined

Multi-Lined

...

...

..

*/

Example:

public class MultipleComment{

public static void main(String[] args){

/*This Class gives Student Related

information 

*/
System.out.println("Multi-Lined Comment");
}
}

Output:
Multi-Lined comment


3) Documentation Comments

Documentation Comments are used while working on a real-time project when writing code for software package

TagDescriptionSyntax
@authorAdds The author of a class.@author name-text
{@code}Displays text in code font without interpreting the text as HTML markup or nested Javadoc tags.{@code text}
{@docRoot}Represents the relative path to the generated document’s root directory from any generated page.{@docRoot}
@deprecatedAdds a comment indicating that this API should no longer be used.@deprecated deprecated text
@exceptionAdds a Throws subheading to the generated documentation, with the class name and description text.@exception class-name description
{@inheritDoc}Inherits a comment from the nearest inheritable class or implementable interface. Inherits a comment from the immediate superclass.
{@link}Inserts an in-line link with the visible text label that points to the documentation for the specified package, class, or member name of a referenced class.{@link package.class#member label}
{@linkplain}Identical to {@link}, except the link’s label is displayed in plain text than code font.{@linkplain package.class#member label}
@paramAdds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section.@param parameter-name description
@returnAdds a “Returns” section with the description text.@return description
@seeAdds a “See Also” heading with a link or text entry that points to a reference.@see reference
@serialUsed in the doc comment for a default serializable field.@serial field-description | include | exclude
@serialDataDocuments the data written by the writeObject( ) or writeExternal( ) methods.@serialData data-description
@serialFieldDocuments an ObjectStreamField component.@serialField field-name field-type field-description
@sinceAdds a “Since” heading with the specified since-text to the generated documentation.@since release
@throwsThe @throws and @exception tags are synonyms.@throws class-name description
{@value}When {@value} is used in the doc comment of a static field, it displays the value of that constant.{@value package.class#field}
@versionAdds a “Version” subheading with the specified version-text to the generated docs when the -version option is used.@version version-text
Example:

public class Compute
{
 /* <h1>Findaddition of three numbers!</h1>
* @author Vinay
* @version 1.0
* @since 2022-09-18
*/

public int Calculate(int a, int b, int c)
{
return (a+b+c);
}


public static void main(String args[])
{
Compute c = new Compute();
int res= c.calculate(30,40,100);

System.out.println(res);

}
}

output:
170










Ad Code

Responsive Advertisement