I am new to Java. Can any one tell me the difference between the following:
They seem to do the same.Code:Integer first_i = 2;
int second_i = 2;
Integer third_i = new Integer(2);
Printable View
I am new to Java. Can any one tell me the difference between the following:
They seem to do the same.Code:Integer first_i = 2;
int second_i = 2;
Integer third_i = new Integer(2);
In j2se 1.5 autoboxing was introduced which automatically converts the int value "2" to an Integer object in this statement.
Before j2se 1.5 this would have caused a compile error.Code:Integer first_i = 2;
Integer is a wrapper class that wraps the primitive int value and makes it an Object for use where objects are required such as for use in the interface Comparable or for Collection interface implementations, eg, ArrayList.
For more see The Numbers Classes.