This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects are created by the following two statements?

 
Charandeep Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, what are your thoughts?
 
vikram bollu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It creates only 2 objects as java creates object in memory for arrays just like other objects.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks incorrect to me, I am afraid.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: In Java, multidimensional arrays are really arrays of arrays.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it is 12
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:I believe it is 12
Not sure, but I think that is correct.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could any body explain why it will create only 12 objects ?

Thnaks ,
S
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic