my dog learned polymorphism*
The moose likes Java in General 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
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?

Charandeep Singh
Ranch Hand

Joined: May 06, 2007
Posts: 57
How many objects are created by the following two statements?

int[] a = new int[10];
int[][] b = new int[10][10];
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

First, what are your thoughts?
vikram bollu
Greenhorn

Joined: Sep 07, 2009
Posts: 9
It creates only 2 objects as java creates object in memory for arrays just like other objects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
That looks incorrect to me, I am afraid.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Hint: In Java, multidimensional arrays are really arrays of arrays.


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

Joined: Sep 19, 2008
Posts: 385
I believe it is 12


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Siva Masilamani wrote:I believe it is 12
Not sure, but I think that is correct.
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Could any body explain why it will create only 12 objects ?

Thnaks ,
S
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
's':
It's because Java doesn't have the concept of multidimensional arrays. So, when the original poster did this:

A 10-element array is created, and each element of that array references a 10-element array. Adding in the first line (int[] a = new int[10]), and you get 12 objects.

John.
 
wood burning stoves
 
subject: How many objects are created by the following two statements?
 
Similar Threads
Question on inner classes
How many objects created?
How many objects are created by the following two statements?
Objects and References....Constructors and new
how many objects will be created.