Function Return writing style in Java

后端 未结 5 1024
眼角桃花
眼角桃花 2020-12-06 16:08

Is this :

String function() { return someString; }

Any different than this ?

String function() { return(someString); }


        
相关标签:
5条回答
  • 2020-12-06 16:51

    There is no functional difference but both a far from writing style in Java because: 1. there are not functions. Java has methods. 2. method names never capitalized. 3. we do not write method body in the same line with its name.

    shortly this is the java style:

    String method() {
        return someSting;
    }
    
    0 讨论(0)
  • 2020-12-06 17:03

    No difference, just convention

    0 讨论(0)
  • 2020-12-06 17:03

    There was a fashion for formatting C return statements like function calls some decades ago. The obvious problem with that is that they aren't function calls.

    0 讨论(0)
  • 2020-12-06 17:11

    No, there is no functional difference at all between wrapping the return value in parentheses or not.

    According to the Java Coding Convention (section 7.3), you should stick with

    return expression;
    

    unless the paretheses makes it more clear:

    7.3 return Statements
    A return statement with a value should not use parentheses unless they make the return value more obvious in some way.

    Example:
    return;
    return myDisk.size();
    return insert(root, data);
    return (size ? size : defaultSize);

    0 讨论(0)
  • 2020-12-06 17:11

    The return with parentheses is not 'calling the return function with an argument', it is simply putting parentheses around the value of the return statement. In other words it is just like writing:

    a = (b + c);
    

    instead of

    a = b + c;
    

    It's perfectly legal but it doesn't add anything useful. And convention is that you don't write the parentheses.

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