This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Saturday, 20 January 2018
JavaDoc: @throws: specify the exception thrown by method
@throws
tag is used to specify the exception thrown by a method (or) constructor.
Syntax
@throws
class-name description
Can I use multiple
throws tags to specify different exceptions?
Yes
Example
/**
*
* @throws IllegalArgumentException
*
thrown if radius < 0.
*/
public
double areaOfCircle(int radius) throws IllegalArgumentException;
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 * * @throws IllegalArgumentException * thrown if radius < 0. */ public double areaOfCircle(int radius) throws IllegalArgumentException; }
javadoc is generated like below.
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); }
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; } }
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
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: @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); }
Subscribe to:
Posts (Atom)






