Auto-boxing
Auto-boxing: Starting with Java 5.0, conversion between primitive types and the corresponding wrapper classes is automatic.
Double d = 29.95; // auto-boxing; same as Double d = new Double(29.95);
double x = d; // auto-unboxing; same as double x = d.doubleValue();
Auto-boxing even works inside arithmetic expressions
Double e = d + 1;
Means:
auto-unbox d into a double
add 1
auto-box the result into a new Double
store a reference to the newly created wrapper object in e
What is the difference between the types double and Double?
Answer:
double
is one of the eight primitive types.
Double
is a class type.
Suppose data is an ArrayList<Double> of size > 0. How do you increment the element with index 0?
Answer: data.set(0, data.get(0) + 1);