File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 36486
    
  16
That looks incorrect to me, I am afraid.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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: 36486
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects are created by the following two statements?
 
Similar Threads
Objects and References....Constructors and new
Question on inner classes
how many objects will be created.
How many objects created?
How many objects are created by the following two statements?