Reputation: 36412
I have a basic doubt, when I was analyzing object and reference.
int a; //here we are creating an object a of type integer
Integer b=new Integer(); // here we are creating a reference variable b points to an object of type Integer in heap.
What is the advantage of each one ? where to use "int a" and the other one?
In case of arrays:
int[] a=new int a[5];
if "int a" is possible why " int a[5] " is not possible, because the below code throws null pointer exception:
int a[5];
a[0]=10;
System.out.println(a[0]); //gives null pointer exception
The code works well when:
int[] a=new int[5];
a[0]=5;
System.out.println(a[0]);
Why in case the former case its excepting a reference needs to be created when "int a" works?
Upvotes: 2
Views: 138
Reputation: 6608
int a
is declaring a primitive variable, called a
, that is of type int
. Primitives are not objects, but arrays are of primitives are objects.
Arrays are objects, but they are a special type of object that Java has special syntax for using. When you are using int[] a
you are declaring an object, called a
, whose type is an array of ints. When you assign it with a = new int[5]
, it is as though you are calling a constructor in a special way. It is as though you were really calling some constructor and passing the size as a parameter: a = new IntArray(5)
or something. Java gives you special syntax so that this type of construct is not needed.
Similarly, when you say a[0] = 5
, it is as though you are calling a method on your object a
, but Java gives you special syntax for this. It's almost like you are calling a.setValue(0, 5)
, but Java gives nice syntax so that this type of thing is not necessary either.
So if you tried to declare int a[5]
, that would be like trying to declare a variable using new IntArray(5)
...that doesn't make sense as a variable declaration. You would basically be using a call to the constructor to declare a variable, which wouldn't make much sense.
Upvotes: 1
Reputation: 89169
(declaration is invalid in java). int a[5];
is understood implicitly as int a[5] = null;
int[] a=new int[5];
creates an array of length 5 with each element of the array containing a default value of 0
. Integer[] a = new Integer[5]
creates an array of Integer
of length 5, all elements have null
as default value.
There are ways to initialize an array (Just an extra note). Example:
int a[] = {1, 2, 3, 4, 5};
This creates an array of int
of length 5, and each element (counting from 0) are assigned values from the braces {}
starting from the left to the right.
Furthermore, int
is a primitive type, so JVM creates a memory space for the size of int
(e.g 4 bytes int) while Integer
is an Object
so JVM creates a memory space to hold the full object of Integer
(which is bigger than the memory space of int
).
where to use "int a" and the other one
Use Integer
if you need to use Generics (since you can't use primitive types in generics but int[]
or primitive arrays are allowed) or need functions from the Integer
class. Else, storage of values to a persistent storage, use int
.
Upvotes: 0
Reputation: 3984
a) First of all, int a;
doesn't declare an object. it's just a var to hold an integer.
b) Always use primitive datatypes over wrapper classes to avoid the overhead of creating an object just to represent a primitive.
c) int a[5]
is a reference to an array of 5 integers pointing to null
, hence a[0]
makes no sense
d) the code int[] a = new int[5];
works properly because you've initialized a
to an array of 5 integers, each of which will have a default value of 0.
e) int a
is a primitive data type declaration hence it doesn't necessarily need to be initialized unless it's a local variable. int[] a
, however, is an int array reference, and since arrays are Objects in Java, you've to initialize before using them.
Upvotes: 0
Reputation: 3523
int
is the primitive type that Integer
uses. int
is more efficient as Integer
is a class with methods, thus uses more memory.
The first array example doesn't work because with just int a[5];
you're merely creating a reference, not the actual values (thus throwing a NullPointerException
when you try to use it). With int[] a=new int[5];
you create the actual array to use, so after that you can use the values.
Upvotes: 0
Reputation: 26809
This is because int isn't Object - it's primitive type and it cannot be null. Integer is object and reference can be null.
int a[] = new int[5]; //is legal and all elements has 0
Integer a[] = new Integer[5]; //is legal too but in this case you have null
Common case of using Integer is Collections, in this case it has to be object, e.g:
List<int> list = new ArrayList<int> // Illegal
List<Integer> list = new ArrayList<Integer> // OK
Upvotes: 1