Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes How many objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many objects?" Watch "How many objects?" New topic
Author

How many objects?

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
int[][] i=new int[1][2];//total 4 objects are created

int[][] i=new int[2][2];//total 7 objects are created

Am i correct? is there anyway to find out how many objects are created in coding way?Please help.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
Mhhh, in my opinion the following is correct:

A) int[][] i=new int[1][2]; // will create 2 Objects

B) int[][] i=new int[2][2]; // will create 3 Objects


In A) the first object is referenced by "i", the second by "i[0]".
In B) the first is ref. by "i", second and third by "i[0]" and "i[1]".


Edit. Added comment.
[ February 25, 2007: Message edited by: Anton Uwe ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Good golly, and I'm' here to suggest a totally different answer???

When I run this code I see 3 objects for the first case and 5 for the second:



It also depends on what you mean by "objects". I'm refering to places in memory that hold a value.
/Pete
[ February 25, 2007: Message edited by: pete stn ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I agree with Anton. The elements of an int[] aren't objects.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Keith Lynn:
I agree with Anton. The elements of an int[] aren't objects.
Oops I stand corrected. Keith Lynn is correct. My lame excuse is that I come to Java from the c# world, and in c# everything, even a primitive type, is a subclass of Object.
[ February 25, 2007: Message edited by: pete stn ]
 
 
subject: How many objects?
 
Similar Threads
garbage collection
Strings
object creation in GC
How many Objects are created?
How many objects will be created in the string pool?