Hello, I would like to know what is the difference between declaring int[] vector or declaring Integer[] vector. In a class exercise it has been declared in both ways and I don't know exactly what the difference is.
The JLS says that:
4.2. Primitive Types and Values
A primitive type is predefined by the Java programming language and named by a reserved word (§3.9): (...) Primitive values do not share state with other primitive values. Numeric types are integral types and floating point types.
The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit, and 64-bit two's complement integers, respectively, (...) (§3.1).
The Integer Javadoc says:
The Integer class wraps the primitive type int in an object. An object of type Integer contains a single field whose value is of type int.
And of the arrays the JLS says :
Chapter 10. Arrays
In the Java programming language arrays are objects (§4.3.1), are created dynamically, and can be assigned to variables of type Object (§4.3.2). All methods of the Object class can be called on an array.
An array object contains a number of variables. The number of variables can be zero, in which case it is said to be an empty array. The variables contained in an array do not have names; instead they are referenced by array access expressions that use non-negative integers as index values. These variables are called components of the array. If an array has n components, we say that n is the length of the array; array components are referenced using integer indices from 0 to n-1, inclusive.
But that doesn't make clear to me the difference between int[] and Integer[]
The substantial difference is that, in JAVA,
int
it is a primitive type, not an object, whereas itInteger
is an object or a Class.Said in colloquial language: a
int
is a number, and aInteger
is a pointer that refers to a class that contains an integer. Or... more colloquially still: aInteger
is a box, and aint
is what is inside that box.What does this mean?
A
int
is much faster when it comes to calculating numbers in the range -2,147,483,648 [-2 31 ], that is,Integer.MIN_VALUE
and +2,147,483,647 [2 31 -1], that is,Integer.MAX_VALUE
. In other words, aint
has at our disposal 32 bits of information to be used directly. See specifications .Variables
int
are mutable. Unless marked asfinal
, they can change their value at any time. A typical example of usingint
to change the value of the counter within loopsfor
,while
, etc.A
Integer
, is an object that contains a single fieldint
. AInteger
is much bulkier than aint
. ObjectsInteger
are immutable. If you want to affect the value of a variableInteger
, the only way is to create a new objectInteger
and discard the old one.A. The primitive
int
Variables of type int store the actual binary value for the integer they represent.
Therefore, the following code is wrong in Java:
because
int
it has no methods, it can only be declared to store a value.B. The Class
Integer
Integer
, as I said, is a Class, like any other Java class, with its methods.This code is correct in Java:
This is a call to the static method
parseInt
of the classInteger
, which returns aint
, not aInteger
.We could say that it
Integer
is a class with a single field of typeint
. This class is used where a is neededint
to be treated like any other object, such as in generic types or situations where null values are needed.When should you use one or the other?
Here is a small table with some indicators:
EDIT: Autoboxing and UnBoxing
Not all that glitters is gold, watch out!
Since the indications of the previous table were questioned by @LuiggiMendoza in the comments, I wanted to add this section because it can spread a false concept about the use of autoboxing and unboxing
Since Java 1.5 it is allowed to convert primitives to objects (wrappers) or vice versa automatically. This is known as AutoBoxing and UnBoxing.
Much of what follows is taken from the Java documentation :
A. Autoboxing
Autoboxing is the automatic conversion that the Java compiler does between primitive types and their corresponding object container classes. For example, convert a
int
to aInteger
, adouble
to aDuoble
, and so on. If the conversion is done in another way, it is called unboxing.The Java compiler applies autoboxing when a primitive value is:
Here is the simplest example of autoboxing:
The rest of the examples in this section use generics...
Let's consider the following code:
Although
li
the values are addedint
as primitive types, rather than Integer objects, the code is compiled. Becauseli
it is a list of objectsInteger
, not a list of valuesint
. One may wonder why the Java compiler does not throw an error at compile time . The compiler does not generate an error because it creates an objectInteger
fromi
and adds the object toli
. Therefore, the compiler converts the above code to the following at run time:Some consider autoboxing - unboxing to be something magical, something great. Personally, I think it is not. And here it is worth asking, why delegate things to the compiler that the programmer can do? Be careful, because we could have a beautiful code, pleasant to read, but it could have a serious bug at any time.
That would suffice but... on top of that, the inappropriate use of autoboxing-unboxing would have a performance cost that may be slight for small operations, but could be the consequence of an error in the execution of the program without thinking about operations that have to handle large amounts of data or on devices with low memory capacity.
This, among other things, is clearly stated in the Java documentation :
B.Unboxing
Converting an object from a container type (
Integer
) to its corresponding primitive value (int
) is called unboxing.The Java compiler applies unboxing when an object of a wrapper class is:
Let's consider the following method:
Since the (%) and (+ =) operators do not apply to objects
Integer
, one might wonder why the Java compiler compiles the method without throwing an error. The compiler does not raise an error because it calls the intValue method to convert aInteger
to aint
at run time :According to Java, it's a matter of aesthetics , not performance:
That's why Java says:
(*) Impedance fault (impedance mismatch). To understand this concept I will give an example: If we are expecting values from a column of a database that admits nulls, and we assign that value in a variable of the type,
int
we are facing a case ofimpedance mismatch
since asint
it does not admit nulls, the program could give an error , or at most it will assign the value0
instead ofNULL
. Or vice versa, if we send the values from Java to the DB, if we use aint
for INSERT or UPDATE in the DB, we could be sending the value to some columns instead ofNULL
the value0
. This, which may seem silly , could be something very serious in some cases.And let's not say anything if we have to make comparisons . We could have unexpected results, since autoboxing - unboxing mask the real values, because a will never
Integer
be a substitute for aint
. One thing is the box, and another what is inside the box. If we confuse the two things we can make very serious mistakes.There are more reasons why it is not good to make Java do what one should do. But that would be stretching too far...
It is notable to note that in Java each primitive type has an equivalent wrapper class:
byte
havebyte
short
haveShort
int
haveInteger
long
haveLong
boolean
haveBoolean
char
haveChar
float
haveFloat
double
haveDouble
First you have to differentiate what is a primitive type and an object.
An object contains attributes and methods. These attributes can be either objects or primitive types.
An object can be assigned null, example:
Integer numero = null;
A primitive type contains neither attributes nor methods and represents a minimal unit of expression.
You cannot assign null to a primitive type, example: (compile error)
int numero = null;
For your case, int is the primitive type and Integer is the object that represents it.