How Exactly Does @param Work - Java

前端 未结 5 970
猫巷女王i
猫巷女王i 2020-12-05 09:50

How does the annotation @param work?

If I had something like this:

/* 
*@param testNumber;
*/

int testNumber = 5;
if (testNumber < 6         


        
相关标签:
5条回答
  • 2020-12-05 10:29

    @param is a special format comment used by javadoc to generate documentation. it is used to denote a description of the parameter (or parameters) a method can receive. there's also @return and @see used to describe return values and related information, respectively:

    http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#format

    has, among other things, this:

    /**
     * Returns an Image object that can then be painted on the screen. 
     * The url argument must specify an absolute {@link URL}. The name
     * argument is a specifier that is relative to the url argument. 
     * <p>
     * This method always returns immediately, whether or not the 
     * image exists. When this applet attempts to draw the image on
     * the screen, the data will be loaded. The graphics primitives 
     * that draw the image will incrementally paint on the screen. 
     *
     * @param  url  an absolute URL giving the base location of the image
     * @param  name the location of the image, relative to the url argument
     * @return      the image at the specified URL
     * @see         Image
     */
     public Image getImage(URL url, String name) {
    
    0 讨论(0)
  • 2020-12-05 10:30

    @param won't affect the number. It's just for making javadocs.

    More on javadoc: http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html

    0 讨论(0)
  • 2020-12-05 10:33

    @param will not affect testNumber.It is a Javadoc comment - i.e used for generating documentation . You can put a Javadoc comment immediately before a class, field, method, constructor, or interface such as @param, @return . Generally begins with '@' and must be the first thing on the line.

    The Advantage of using @param is :- By creating simple Java classes that contain attributes and some custom Javadoc tags, you allow those classes to serve as a simple metadata description for code generation.

        /* 
           *@param testNumber
           *@return integer
        */
        public int main testNumberIsValid(int testNumber){
    
           if (testNumber < 6) {
              //Something
            }
         }
    

    Whenever in your code if you reuse testNumberIsValid method, IDE will show you the parameters the method accepts and return type of the method.

    0 讨论(0)
  • 2020-12-05 10:49

    It is basically a comment. As we know, a number of people working on the same project must have knowledge about the code changes. We are making some notes in the program about the parameters.

    0 讨论(0)
  • 2020-12-05 10:56

    You might miss @author and inside of @param you need to explain what's that parameter for, how to use it, etc.

    0 讨论(0)
提交回复
热议问题