• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many objects are created by the following two statements?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] a = new int[10];
int[][] b = new int[10][10];
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not possible to explain that, because it's not true.
 
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic