Reputation: 13108
Is unboxking expensive that it's better avoiding it? From this java tutorial:
public class ValueOfDemo {
public static void main(String[] args) {
// this program requires two
// arguments on the command line
if (args.length == 2) {
// convert strings to numbers
float a = (Float.valueOf(args[0])).floatValue();
float b = (Float.valueOf(args[1])).floatValue();
Why don't they just leave it like float a = (Float.valueOf(args[0]));
? If they introduced the diamond operator in order to save time to programmers, why would not they make use of the unboxing function? Is it less expensive doing it manually like they do?
Thanks in advance.
Upvotes: 2
Views: 629
Reputation: 51711
Unboxing is not expensive in the sense that it's simply doing what you would've been doing yourself in pre-Java 5 environments. It's just that it's now being done automatically.
But, like anything else it's expensive if invoked unnecessarily. Like in your case you should have used the parseFloat()
directly instead of creating a wrapper in between.
At other times unboxing is so transparent that you fail to realize its hampering performance. For example, if you miss out noticing that you've used a wrapper type as a counter inside a loop it would unnecessarily be unboxing, incrementing and boxing again at every iteration.
Upvotes: 0
Reputation: 117587
Actually the following two java applications produce the same binary code (using JDK 7 u25 x64):
ValueOfDemo.java
public class ValueOfDemo
{
public static void main(String[] args)
{
float a = Float.valueOf("1.5").floatValue();
}
}
Compiling:
javac ValueOfDemo.java
Disassembling:
javap -c -cp . ValueOfDemo
public class ValueOfDemo {
public ValueOfDemo();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String 1.5
2: invokestatic #3 // Method java/lang/Float.valueOf:(Ljava/lang/String;)Ljava/lang/Float;
5: invokevirtual #4 // Method java/lang/Float.floatValue:()F
8: fstore_1
9: return
}
ValueOfDemo2.java
public class ValueOfDemo2
{
public static void main(String[] args)
{
float a = Float.valueOf("1.5");
}
}
Compiling:
javac ValueOfDemo2.java
Disassembling:
javap -c -cp . ValueOfDemo2
public class ValueOfDemo2 {
public ValueOfDemo2();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String 1.5
2: invokestatic #3 // Method java/lang/Float.valueOf:(Ljava/lang/String;)Ljava/lang/Float;
5: invokevirtual #4 // Method java/lang/Float.floatValue:()F
8: fstore_1
9: return
}
Upvotes: 3
Reputation: 111239
Unboxing is cheap; what you suggest is compiled into the code you have seen in the tutorial. The point of the tutorial is to show how to use the floatValue so not showing it would probably be confusing.
What is not so cheap is creating a Float object in the first place just to parse a string. Use Float.parseFloat instead.
float f = Float.parseFloat(string);
Upvotes: 0