• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects are created by the following two statements?

 
kishore srimat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] a = new int[10];
int[][] b = new int[10][10];
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.


And perhaps I should correct the second comment in my previous post:
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry because henry told for this statement int[][]a = new int[10][10] and he/she is right.
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prasun Howlader:
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
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not possible to explain that, because it's not true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic