Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplained parenthesise in Java

Tags:

java

What do parenthesis do in Java other than type casting.

I've seen them used in a number of confusing situations, here's one from the Java Tutorials:

//convert strings to numbers
float a = (Float.valueOf(args[0]) ).floatValue();  
float b = (Float.valueOf(args[1]) ).floatValue();

I only know only two uses for parenthesis, calls, and grouping expressions. I have searched the web but I can't find any more information.

In the example above I know Float.valueOF(arg) returns an object. What effect does parenthesize-ing the object have?

like image 248
code shogan Avatar asked Aug 03 '11 14:08

code shogan


1 Answers

Absolutely nothing. In this case they are not necessary and can be removed. They are most likely there to make it more clear that floatValue() is called after Float.valueOf().

So this is a case of parenthesis used to group expressions. Here it's grouping a single expression (which does obviously nothing).

It can be shortened to:

float a = Float.valueOf(args[0]).floatValue();
float b = Float.valueOf(args[1]).floatValue();

which can then be logically shortened to

float a = Float.parseFloat(args[0]);
float b = Float.parseFloat(args[1]);
like image 57
tskuzzy Avatar answered Sep 29 '22 23:09

tskuzzy