Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have some questions regarding arrays in java:

How many objects are created in the following expressions?

(a) new int[0] : There is one object created, an array of size 0.???

(b) new int[2][2] : There is one object created, an array with 2 rows and columns.???

(c) new int[2][] : There is no object created???

I was wondering if my solutions next to the expressions are correct. If not, hopefuly you can help and give me some explanation about them. I don't really get what im supposed to do.

Thanks in advance!

share|improve this question
    
explain your question –  Manish Nagar Dec 22 '12 at 10:51
1  
Looks like an exam question. "I'm not quite sure about the meaning of this question"?? –  Henrik Dec 22 '12 at 10:52
1  
a) One object b) 3 objects c) 1 object –  nhahtdh Dec 22 '12 at 10:52
1  
Now I've read it further it probably hardly belongs here much more than meta where you posted it a few minutes ago. –  PeterJ Dec 22 '12 at 10:53

2 Answers 2

up vote 3 down vote accepted

new int[0]

Yes this is an empty array, one object is created.

    int[] emptyArray = new int[0];
    System.out.println(emptyArray.length); // Outputs 0

new int[2][2]

Yes this creates and array with 2 rows and columns, 3 objects are created.

    int[][] bar = new int[2][2];
    System.out.println(bar.getClass()); // Outputs class [[I
    int[] bar1 = bar[0];
    System.out.println(bar1.getClass());  // Outputs class [I
    int[] bar2 = bar[1];
    System.out.println(bar2.getClass());  // Outputs class [I

new int[2][]

Java supports jagged arrays. This means when you create int[2][] this means you have an array of various sized int[]. Only 1 object is created here.

    int[][] foo = new int[2][];
    System.out.println(foo[0]);  // Outputs null
    System.out.println(foo[1]);  // Outputs null
    foo[0] = new int[10];
    foo[1] = new int[5];
share|improve this answer
    
I understand what you did, but how many "objects" are created?. I mean if you just code int[][] foo = new int[2][]; there won't be any object right? Because the empty[][] has to be filled first? –  user1923369 Dec 22 '12 at 10:59
1  
I believe new int[2][] creates one object only, which is the array object itself. I might be wrong. –  Eng.Fouad Dec 22 '12 at 11:01
    
Yes, I think Eng.Found is correct. Only one object in each and every situation because the user is adding data types. But, multiple MEMORY LOCATIONS will be allocated –  Hope Dec 22 '12 at 11:07
    
@user1923369 - I have updated my answer to include how many objects are created. –  Blaise Doughan Dec 22 '12 at 11:08
    
@BlaiseDoughan: I don't think lot of objects will be created in any of the above arrays, instead the array object. because there is no 'new' keyword. Multiple memory locations will be created and they will be filled with data types, but can we call them as objects? –  Hope Dec 22 '12 at 11:10

The following is an excerpt from the Java Specification:

In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

This means every array is an own object, which makes your answer a) correct.

b) 3 objects are created: 1 array first and two arrays containing each 2 ints. I wouldn't count the int-entries as they are primitive types in Java.

c) 1 object is created: 1 array with two null-entries.

share|improve this answer
    
I believe I understand this now, thanks alot for you help! –  user1923369 Dec 22 '12 at 11:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.