seems to work without any error although I have not initialized the array of object of the Integer class, using the code :

for(int i=0;i<2;i++)
n[i] = new Integer();

How is it that, the array of objects of the Integer class and that of the class created by me differ in this case.

Is it that the objects for the class that I create requires initialization and the objects for the class Integer does not require initialization. Am I getting it right ? I just wanted to know how the classes that users create and classes that already exist differ.

1 Answer
1

I have not initialized the array of object of the Integer class, using the code :

Yes, you did initialize the contents of the array n. Just like any reference array, an Integer[] is initialized with all nulls.

However, you did supply values:

n[0] = 9;
n[1] = 18;

You just didn't use new, because Java will autobox the int literals into Integer objects. You are storing the Integer objects corresponding to 9 and 18.

It doesn't matter if the objects are for classes that you create or not. It matters whether you are using a "wrapper" type for Java's primitive values. You can always have Java autobox primitive values into wrapper types, e.g. int to Integer.