So why is the Double type so much slower?
Because the value is wrapped inside an object which needs allocation, deallocation, memory management plus getters and setters
Why is it even implemented to allow mathematical operators?
Because autobox is meant to allow you to use such wrappers without worrying about the fact that they are not plain values. Would you prefer not being able to have an ArrayList<Double>? Performance is not always necessary and a drop of 3x-7x of performance according to situations maybe acceptable. Optimization is a requirement which is not always present.
This is true in every situation, using a LinkedList to random access elements could be overkill but this doesn't mean that LinkedList shouldn't be implemented at all. This neither means that using a linked list for few random accesses could interfere with performance so much.
A final note: you should let the VM warm up before benchmarking such things.
Answer from Jack on Stack OverflowSo why is the Double type so much slower?
Because the value is wrapped inside an object which needs allocation, deallocation, memory management plus getters and setters
Why is it even implemented to allow mathematical operators?
Because autobox is meant to allow you to use such wrappers without worrying about the fact that they are not plain values. Would you prefer not being able to have an ArrayList<Double>? Performance is not always necessary and a drop of 3x-7x of performance according to situations maybe acceptable. Optimization is a requirement which is not always present.
This is true in every situation, using a LinkedList to random access elements could be overkill but this doesn't mean that LinkedList shouldn't be implemented at all. This neither means that using a linked list for few random accesses could interfere with performance so much.
A final note: you should let the VM warm up before benchmarking such things.
You wouldn't normally use Double, Integer, etc. (Occasionally Integer etc. can be useful to store an 'optional' value - you might want it to be null sometimes. This is less likely with Double because NaN is available for those.)
The reason Double exists is as follows. Java has two main types of value: objects (essentially like C/C++ pointers without the arithmetic), and primitive values (e.g. double). Classes like ArrayList can be defined to accept any Object, which allows users to store String, File or whatever they like in one - but primitive values like double are not covered by such a definition. So classes like Double exist to make it easier for classes like ArrayList to store doubles, without requiring the authors of ArrayList to create special versions for all the primitive types.
Videos
This is called Autoboxing which is a feature that was added in Java 5. It will automatically convert between primitive types and the wrapper types such as double (the primitive) and java.lang.Double (the object wrapper). The java compiler automatically transforms the line:
Double d = 5.1;
into:
Double d = Double.valueOf(5.1);
It is called AutoBoxing
Autoboxing and Auto-Unboxing of Primitive Types Converting between primitive types, like int, boolean, and their equivalent Object-based counterparts like Integer and Boolean, can require unnecessary amounts of extra coding, especially if the conversion is only needed for a method call to the Collections API, for example.
The autoboxing and auto-unboxing of Java primitives produces code that is more concise and easier to follow. In the next example an int is being stored and then retrieved from an ArrayList. The 5.0 version leaves the conversion required to transition to an Integer and back to the compiler.
Before
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, new Integer(42));
int total = (list.get(0)).intValue();
After
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, 42);
int total = list.get(0);
Yes, this is good practice. Java makes auto-unboxing Double -> double.
P.S. By the way, correct comparison of double is Double.compare(one, two) == 0
Yes, comparing a Double to a double in this way is just fine. This is because Java automatically converts the Double to a double anyway through auto-unboxing.