I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int
and an Integer
in Java/C# (Object Oriented Programming Languages).
So, what is the difference?
Java:
int
,double
,long
,byte
,float
,double
,short
,boolean
,char
- primitives. Used for hold the basic data types supported by the language. the primitive types are not part of the object hierarchy, and they do not inherit Object. Thet can'be pass by reference to a method.Double
,Float
,Long
,Integer
,Short
,Byte
,Character
, andBoolean
, are type Wrappers, packaged injava.lang
. All of the numeric type wrappers define constructors that allow an object to be constructed from a given value, or a string representation of that value. Using objects can add an overhead to even the simplest of calculations.Beginning with JDK 5, Java has included two very helpful features: autoboxing and autounboxing. Autoboxing/unboxing greatly simplifies and streamlines code that must convert primitive types into objects, and vice versa.
Example of constructors:
Example of boxing/unboxing:
Example of autoboxing/autounboxing:
P.S. Herbert Schildt's book was taken as a reference.
An int and Integer in Java and C# are two different terms used to represent different things. It is one of the the primitive data types that can be assigned to a variable that can store exactly. One value of its declared type at a time.
For example:
Where
int
is the datatype assigned to the variable number which holds the value seven. So anint
is just a primitive not an object.While an
Integer
is a wrapper class for a primitive data type which has static methods. That can be used as an argument to a method which requires an object, where as int can be used as an argument to a method which requires an integer value, that can be used for arithmetic expression.For example:
Regarding Java 1.5 and autoboxing there is an important "quirk" that comes to play when comparing Integer objects.
In Java, Integer objects with the values -128 to 127 are immutable (that is, for one particular integer value, say 23, all Integer objects instantiated through your program with the value 23 points to the exact same object).
Example, this returns true:
While this returns false:
The == compares by reference (does the variables point to the same object).
This result may or may not differ depending on what JVM you are using. The specification autoboxing for Java 1.5 requires that integers (-128 to 127) always box to the same wrapper object.
A solution? =) One should always use the Integer.equals() method when comparing Integer objects.
More info at java.net Example at bexhuff.com
In Java int is a primitive data type while Integer is a Helper class, it is use to convert for one data type to other.
For example:
Primitive data types are store the fastest available memory where the Helper class is complex and store in heep memory.
reference from "David Gassner" Java Essential Training.
int
is predefined in library function c# but in java we can create object ofInteger
One more thing that I don't see in previous answers: In Java the primitive wrappers classes like Integer, Double, Float, Boolean... and String are suposed to be invariant, so that when you pass an instance of those classes the invoked method couldn't alter your data in any way, in opositión with most of other classes, which internal data could be altered by its public methods. So that this classes only has 'getter' methods, no 'setters', besides the constructor.
In a java program String literals are stored in a separate portion of heap memory, only a instance for literal, to save memory reusing those instances