aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects are created by the following two statements? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects are created by the following two statements? " Watch "How many objects are created by the following two statements? " New topic
Author

How many objects are created by the following two statements?

kishore srimat
Greenhorn

Joined: Dec 18, 2007
Posts: 5
int[] a = new int[10];
int[][] b = new int[10][10];
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
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.


"Control time instead of letting time control you."
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
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

Joined: Jan 30, 2000
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:


"I'm not back." - Bill Harding, Twister
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
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

Joined: Jan 30, 2000
Posts: 18671
No.


And perhaps I should correct the second comment in my previous post:
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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

Joined: Oct 21, 2007
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

Joined: Nov 24, 2007
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

Joined: Jan 30, 2000
Posts: 18671
It's not possible to explain that, because it's not true.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects are created by the following two statements?