Saturday, 20 January 2018

Javadoc: @since tag

@since tag is used to specify, this feature has existed since specific software release.

Syntax
@since since-text

Where can I use @since tag?
You can use on package, class, interface, constructor, method, or field.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 *
 * @param <T>
 *            Type of element to perform Arithmetic operations
 * 
 * @since 1.0
 */
public interface Arithemtic<T extends Number> {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * 
  * @return sum of two operands a and b
  * 
  * @since 1.1
  * 
  */
 T sum(T a, T b);
}




Previous                                                 Next                                                 Home

Javadoc: @serial: Document meaning of serial fields

@serial tag is used to document the acceptable values and description of serialized fields.

Javadoc tool places this information in the "Serialized Form" page for that class

Ex
/**
 * @serial Serialize PI {@value}
 */
public final double PI = 3.14;

Arithmetic.java

package com.java.tags;

import java.io.Serializable;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public class Arithmetic implements Serializable {

 /**
  * Serialization constant value is {@value}
  */
 private static final long serialVersionUID = 1L;

 /**
  * @serial Serialize PI {@value}
  */
 public final double PI = 3.14;

 public double areaOfCircle(int radius) {
  return PI * radius * radius;
 }

}





Previous                                                 Next                                                 Home

Javadoc: @see: Document other references

It adds a "See Also" heading with a link or text entry that points to a reference.

Syntax
@see reference

/**
 *
 * @see Math#sqrt(double)
 */
public double sqrt(double number) throws IllegalArgumentException;

Arithmetic.java

package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * 
  * @param number
  *            number to calculate square root for
  * 
  * @return square root of a number
  * 
  * @throws IllegalArgumentException
  *             if number <= 0
  * 
  * @see Math#sqrt(double)
  */
 public double sqrt(double number) throws IllegalArgumentException;

}

javadoc is generated like below.


Where can I use @see tag?
You can use on top of package, class, interface, constructor, method, or field

What is the difference between {@link} and @see tags?
{@link} tag generates an inline link rather than placing the link in the See Also section

How to reference a member of the current class?
@see #field
@see #method(Type, Type,...)
@see #method(Type argname, Type argname,...)
@see #constructor(Type, Type,...)
@see #constructor(Type argname, Type argname,...)

How to reference a member of other class?
@see Class#field
@see Class#method(Type, Type,...)
@see Class#method(Type argname, Type argname,...)
@see Class#constructor(Type, Type,...)
@see Class#constructor(Type argname, Type argname,...)
@see Class.NestedClass
@see Class

How to reference a member of other package?
@see package.Class#field
@see package.Class#method(Type, Type,...)
@see package.Class#method(Type argname, Type argname,...)
@see package.Class#constructor(Type, Type,...)
@see package.Class#constructor(Type argname, Type argname,...)
@see package.Class.NestedClass
@see package.Class
@see package


Previous                                                 Next                                                 Home

Javadoc: @return tag

@return tag is used to document the return value of the method.

Syntax
@return description

As per convention, do not use @return tag on void methods and constructors.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 *
 * @param <T>
 *            Type of element to perform Arithmetic operations
 */
public interface Arithemtic<T extends Number> {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * 
  * @return sum of two operands a and b
  * 
  * @since Version 1.1
  * 
  */
 T sum(T a, T b);
}

javadoc is generated like below.




Previous                                                 Next                                                 Home

Javadoc: @param tag

By using @param tag, we can document the description of method parameters.

Syntax
@param parameter-name description

This tag is valid only in a documentation comment for a method, constructor, or class.

The parameter-name can be the name of a parameter in a method or constructor, or the name of a type parameter of a class, method, or constructor. Use angle brackets around this parameter name to specify the use of a type parameter.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 *
 * @param <T>
 *            Type of element to perform Arithmetic operations
 */
public interface Arithemtic<T extends Number> {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * 
  * @return sum of two operands a and b
  * 
  * @since Version 1.1
  * 
  */
 T sum(T a, T b);
}





Previous                                                 Next                                                 Home

Javadoc: @exception: Specify the exception thrown by a method

@exception tag is used to specify the exception thrown by a method (or) constructor.

Syntax
@exception Exception_Class_Name

What is the difference between @throws and @exception tags?
No difference, both are identical (synonyms).

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * 
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  * 
  * @exception IllegalArgumentException
  *                thrown if radius &lt; 0.
  */
 public double areaOfCircle(int radius) throws IllegalArgumentException;

}

When you generate javadoc, it looks like below.




Previous                                                 Next                                                 Home

Javadoc: @deprecated: Document deprecated APIS

@deprecated tag is used to document the deprecated APIS. IDEs like Eclipse visualize the deprecated API by cross line on the api name.

Ex
Before adding @deprecated tag to the method ‘areaOfCircle’ in Eclipse.

After adding @deprecated tag to the method ‘areaOfCircle’ in Eclipse. You can observe a line across the method name.
Arithmetic.java

package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  *
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  * 
  * @exception IllegalArgumentException
  *                thrown if radius &lt; 0.
  * 
  * @deprecated Do not use this method, use {@link #getAreaOfCircle(int)}
  */
 public double areaOfCircle(int radius) throws IllegalArgumentException;

 /**
  * 
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  */
 public double getAreaOfCircle(int radius);

}

Javadoc is generated like below.



Previous                                                 Next                                                 Home