• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many objects are created?  RSS feed

 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me how many objects are created in this code. My guess is only 3, although i am a bit confused!!!

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write the code on a sheet of paper, and mark each new object as it is created. Then number and count them, and I don't think it will come to 3.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the count to 3 comes to as....

Integer i=new Integer(5);

next i changes to int and 1 is added following which we get 6.

so we have

i=new Integer(6);
Next Integer j is made to refer to the same object as i

and again j=i+j is equivalent to j=new Integer(12);

so thats how the count to 3 objects is coming. can you please tell me where i am going wrong!!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I vote for three as well.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another vote for three.

I could technically also say zero because of the caching of values between -128 and 127. These are created when the Integer class is loaded. The statements in this example don't use "new Integer" but "Integer.valueOf" which uses this cache. So the following is what occurs:
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so three objects is the right answer???
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Another vote for three.

I could technically also say zero because of the caching of values between -128 and 127. These are created when the Integer class is loaded. The statements in this example don't use "new Integer" but "Integer.valueOf" which uses this cache. So the following is what occurs:



so does that mean that for any value in the range of -128 to 127, an Integer object is never created?
 
Jared Malcolm
Ranch Hand
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running the debugger on that code only shows two object ever being created (well three but only two related to the given code snippet).

I stand corrected Integer i received two different id values so the answer would be three from what I'm seeing
objects.png
[Thumbnail for objects.png]
Debug Console
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:

I vote for 256.
If this is the first use of the cache it will cause it to be initialised, thus creating 256 Integer objects.
And that of course assumes the default size of the cache is being used. It is possible to configure the upper bound, so you could argue there is insufficient information in the question to answer it
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Rob Spoor wrote:

I vote for 256.
If this is the first use of the cache it will cause it to be initialised, thus creating 256 Integer objects.
And that of course assumes the default size of the cache is being used. It is possible to configure the upper bound, so you could argue there is insufficient information in the question to answer it



This is getting confusing by the minute!!!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I was mistaken about the j = i bit. Sorry. But it turns out not to be three after all.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jishnu dasgupta wrote:This is getting confusing by the minute!!!

Yes, unfortunately the true answer to "how many objects are created" is often more complex than we want (or need) to know about. Perhaps a better question here is, how many Integer objects are being used in this code? Regardless of whether they are created here as part of a larger group, or were previously created and are now being re-used. The answer to this question is three, period, for the reasons you already gave. I think that's all you need to understand here, for now.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jishnu dasgupta wrote:so does that mean that for any value in the range of -128 to 127, an Integer object is never created?

Actually Joanne is right. Whenever Integer.valueOf(int) is used for the first time, 256 Integer objects with values ranging from -128 to 127* are created and stored until the JVM exits.
Also, when you use new Integer it always creates a new Integer object, even for values in that range.

* There is a system property that allows you to increase the upper bound, but this isn't used for most programs.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Whenever Integer.valueOf(int) is used for the first time, 256 Integer objects with values ranging from -128 to 127 are created

Plus the two class literals Integer.class and Integer.IntegerCache.class. And if no other subclass of Number has already been loaded, Number.class.

Total: 259 objects
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
jishnu dasgupta wrote:This is getting confusing by the minute!!!

Perhaps a better question here is, how many Integer objects are being used in this code? Regardless of whether they are created here as part of a larger group, or were previously created and are now being re-used. The answer to this question is three, period, for the reasons you already gave. I think that's all you need to understand here, for now.


Agreed!!! Thanks for the insights!! What i thought started as a simple question, really help me know something i didnt know!!!..Thanks folks!!!
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Rob..one last question to close this thread!!!...


I beleive i am correct in my assumptions??
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct.

edit It would be more correct to say that the second line creates one new object, regardless of the fact that caching is done. That caching will take place when the Integer class is loaded, whether you want/use it or not.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup!!!...which means there would be the total cache ie 256 objects+three class literals plus the object refernced by j!! finally got it!!..Thanks a lot darryl!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!