# How many objects are created by the following two statements?

kishore srimat
Greenhorn
Posts: 5
int[] a = new int[10];
int[][] b = new int[10][10];

Henry Wong
author
Marshal
Posts: 21227
81
int[] a = new int[10];

Just one. The int array object. The 10 ints are not objects.

int[][] b = new int[10][10];

Eleven. One array of int array objects. And 10 int array objects. Again, the ints are not objects.

Henry

Ranch Hand
Posts: 89
Dear Henry,
I am confused the second statement int[][]a = new int[10][];
You said here 11 object created. But i thought here 1 object created.This array hold 10 array reference variable.

ahmed yehia
Ranch Hand
Posts: 424
There are actually 11 objects created, because the array is instantiated using 'new' all ref. variables are assigned to new array objects.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
For the second statement, there is one int[][] array, and ten int[] arrays. The first array holds references to the other ten, which represent individual rows. The statement is equivalent to the following code:

Ranch Hand
Posts: 89
1. int[][] a = new int[10][];
2. int[][] b = new int[10][3];

This two statement each generate 11 object is it true?
[ January 26, 2008: Message edited by: Prasun Howlader ]

Jim Yingst
Wanderer
Sheriff
Posts: 18671
No.

And perhaps I should correct the second comment in my previous post:

Ranch Hand
Posts: 89
I am sorry because henry told for this statement int[][]a = new int[10][10] and he/she is right.

Henry Wong
author
Marshal
Posts: 21227
81
I am sorry because henry told for this statement int[][]a = new int[10][10] and he/she is right.

I don't understand your confusion here.

I mentioned:

int[] a = new int[10]; // creates one object

int[][] b = new int[10][10]; // creates eleven objects

Jim mentioned:

int[][]a = new int[10][]; // creates one object only

int[][]b = new int[10][3]; // creates eleven objects

There is no contradiction here. Your response implies that Jim stated something that conflicts with what I stated. Could you elaborate on why you think which of the four statements are in conflict?

Henry

Ranch Hand
Posts: 89
Both jim and henry were right. Actually i am confusing this statement

int[][]a = new int[10][];

Now i am clear.

Sid Robin
Ranch Hand
Posts: 53
why is that the statement Delta []a[] = new Delta[10][10] for Delta Java Program creates only one object "a" which is array object ? contrary to what every one has explained ?

Jim Yingst
Wanderer
Sheriff
Posts: 18671
It's not possible to explain that, because it's not true.