Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Java Comments

The java comments are statements that are not executed by the compiler and interpreter. The comments can be used to provide information or explanation about the variable, method, class or any statement. It can also be used to hide program code for specific time.

Types of Java Comments

There are 3 types of comments in java.

  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment

1) Java Single Line Comment

The single line comment is used to comment only one line.

Syntax:

// A comment is written here,
// single line comment


Example:

package com.ashutosh;
public class Java64bitDemo {
public static void main(String []ab)
{
// Single line comment
System.out.println("Single line comment above");
}
}


Output: Single line comment above

2) Multi Line Comment in Java

The multi line comment is used to comment multiple lines of code.

Syntax:

/*Starts comment
continues
continues
.
.
.
Comment ends*/


Example:

package com.ashu;
public class Java64bitDemo {
public static void main(String args[])
{
/*Comment line 1
Comment line 2
Comment line 3*/
System.out.println("Multi line comments above");
}
}


Output:Multi line comments above

3) Documentation Comment in Java

The documentation comment is used to create documentation API. To create documentation API, you need to use javadoc tool.

Syntax:

/**Start Comment
*
*Tags are used in order to specify a parameter
*or method or heading
*such as


*
*comment ends*/


Example:

package com.ashu;
public class Java64bitDemo
{
/**
* Method to find average
* @param numA- This is the first parameter to calculateSum method
* @param numB - This is the second parameter to calculateSum method
*/
int numA;
int numB;
Java64bitDemo(int numA,int numB)
{
this.numA=numA;
this.numB=numB;
}
public void calculateSum()
{
System.out.println("Sum of two numbers is "+(numA+numB));
}
static class Test
{
public static void main(String args[])
{
Java64bitDemo obj=new Java64bitDemo(5,15);
obj.calculateSum();
}
}
}

Output: Sum of two numbers is 20


Compile it by javac tool:

javac Calculator.java

Create Documentation API by javadoc tool:

javadoc Calculator.java

Now, there will be HTML files created for your Calculator class in the current directory. Open the HTML files and see the explanation of Calculator class provided through documentation comment.

Advertisement

Copyright ©2017 Design& develop