I saw the following code and was wondering the intent of the coder. Is it related performance of autoboxing?
map.put("doesntMatter", Boolean.TRUE);
He could have done:
map.put("doesntMatter", true);
Any benefits of doing the first?
I saw the following code and was wondering the intent of the coder. Is it related performance of autoboxing?
map.put("doesntMatter", Boolean.TRUE);
He could have done:
map.put("doesntMatter", true);
Any benefits of doing the first?
I've written an example:
public class Demo {
Map<String, Boolean> map = new HashMap<>();
void primitive() {
map.put("a", true);
}
void object() {
map.put("b", Boolean.TRUE);
}
}
look at the bytecode of primitive()
0 aload_0
1 getfield #17 <Demo/map Ljava/util/Map;>
4 ldc #24 <a>
6 iconst_1
7 invokestatic #26 <java/lang/Boolean/valueOf(Z)Ljava/lang/Boolean;>
10 invokeinterface #32 <java/util/Map/put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;> count 3
15 pop
16 return
and the the bytecode of object()
0 aload_0
1 getfield #17 <Demo/map Ljava/util/Map;>
4 ldc #39 <b>
6 getstatic #41 <java/lang/Boolean/TRUE Ljava/lang/Boolean;>
9 invokeinterface #32 <java/util/Map/put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;> count 3
14 pop
15 return
Conclusion:
when using the primitive, there is an extra step calling Boolean.valueOf()
, but if you run that piece of code often, the JIT compiler will do it's job and optimize it away.
The benefits are not in execution time, as this little test code shows :
Results :
Time with primitives : 3334779619
Time with Object : 4092034749
Time with primitives : 3670851766
Time with Object : 2748035018
Time with Object : 3738916372
Time with primitives : 2975196722
Time with Object : 2514328271
Time with primitives : 2588980283
Time with Object : 2696162369
Time with primitives : 2615258656
Time with primitives : 2633824223
Time with Object : 2489779261
Code :
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
public class Test
{
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Start");
createWithPrimitive();
createWithObject();
createWithPrimitive();
createWithObject();
createWithObject();
createWithPrimitive();
createWithObject();
createWithPrimitive();
createWithObject();
createWithPrimitive();
createWithPrimitive();
createWithObject();
System.exit(0);
}
private static void createWithObject() {
long time = System.nanoTime();
Map<Integer, Boolean> testMap = new HashMap<Integer, Boolean>();
for (int i = 1; i <= 10000000; i++) {
if (i % 2 == 0) {
testMap.put(i, Boolean.TRUE);
} else {
testMap.put(i, Boolean.FALSE);
}
}
System.out.println("Time with Object : " + (System.nanoTime() - time));
}
private static void createWithPrimitive() {
long time = System.nanoTime();
Map<Integer, Boolean> testMap = new HashMap<Integer, Boolean>();
for (int i = 1; i <= 10000000; i++) {
if (i % 2 == 0) {
testMap.put(i, true);
} else {
testMap.put(i, false);
}
}
System.out.println("Time with primitives : " + (System.nanoTime() - time));
}
}