Is this :
String function() { return someString; }
Any different than this ?
String function() { return(someString); }
Its the return I'm interested in. I see the 2nd style less frequently, is it just convention or is the return in parenthesis actually doing something different?
Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing). The type of data returned by a method must be compatible with the return type specified by the method.
The return keyword finished the execution of a method, and can be used to return a value from a method.
With generic Java collections, we can return multiple values of a common type. The collections framework has a wide spectrum of classes and interfaces.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With