What do you think about the following line of code?:
String s= "10.0";
float f = Float.valueOf(s).floatValue();//1
Is it necessary? Why would it be better using such a syntax rather than using:
float f = Float.valueOf(s);//2
It still gives the same results taking advantage of the autoboxing function.
In short my question is: Why should one choose for the first syntax instead of the second one? Are they completely the same?
Well, I would use neither of them, because both of them will generate intermediate
Floatobject, which is almost always not needed. And wherever it will be needed, we will get it to work with boxing.For now, you should rather just use
Float.parseFloat(String)method, that generates a primitive float.As far as similarity is concerned, no they are not completely the same. 2nd one involves
auto-unboxingfromFloattofloat, while there is nounboxingin first case. It does the conversion using the given method.