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 Version) In Simple words int is primitive and Integer is wrapper object for int.
One example where to use Integer vs int, When you want to compare and int variable again null it will throw error.
There are many reasons to use wrapper classes:
Well, in Java an int is a primitive while an Integer is an Object. Meaning, if you made a new Integer:
You could call some method on i:
Whereas with an int:
You cannot call any methods on it, because it is simply a primitive. So:
would produce an error, because int is not an object.
int is one of the few primitives in Java (along with char and some others). I'm not 100% sure, but I'm thinking that the Integer object more or less just has an int property and a whole bunch of methods to interact with that property (like the toString() method for example). So Integer is a fancy way to work with an int (Just as perhaps String is a fancy way to work with a group of chars).
I know that Java isn't C, but since I've never programmed in C this is the closest I could come to the answer. Hope this helps!
Integer object javadoc
Integer Ojbect vs. int primitive comparison
I'll add to the excellent answers given above, and talk about boxing and unboxing, and how this applies to Java (although C# has it too). I'll use just Java terminology, because I am more au fait with that.
As the answers mentioned,
int
is just a number (called the unboxed type), whereasInteger
is an object (which contains the number, hence a boxed type). In Java terms, that means (apart from not being able to call methods onint
), you cannot storeint
or other non-object types in collections (List
,Map
, etc.). In order to store them, you must first box them up in its corresponding boxed type.Java 5 onwards have something called auto-boxing and auto-unboxing which allow the boxing/unboxing to be done behind the scenes. Compare and contrast: Java 5 version:
Java 1.4 or earlier (no generics either):
It must be noted that despite the brevity in the Java 5 version, both versions generate identical bytecode. Thus, although auto-boxing and auto-unboxing is very convenient because you write less code, these operations do happen behind the scenes, with the same runtime costs, so you still have to be aware of their existence.
Hope this helps!
01. Integer can be null. But int cannot be null.
02. Only can pass Wrapper Classes type values to any collection class.
(Wrapper Classes - Boolean,Character,Byte,Short,Integer,Long,Float,Double)
But normally we add primitive values to collection class? Is point 02 correct?
Yes 02 is correct, beacouse
autoboxing.
Then 5 convert as Integer value by autoboxing.
In Java there are two basic types in the JVM. 1) Primitive types and 2) Reference Types. int is a primitive type and Integer is a class type (which is kind of reference type).
Primitive values do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that type.
An object is a dynamically created class instance or an array. The reference values (often just references) are pointers to these objects and a special null reference, which refers to no object. There may be many references to the same object.
Also in Java everything is passed by value. With objects the value that is passed is the reference to the object. So another difference between int and Integer in java is how they are passed in method calls. For example in
The variable two is passed as the primitive integer type 2. Whereas in
The variable two is passed as a reference to an object that holds the integer value 2.
@WolfmanDragon: Pass by reference would work like so:
When increment is called it passes a reference (pointer) to variable a. And the increment function directly modifies variable a.
And for object types it would work as follows:
Do you see the difference now?