• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object creation  RSS feed

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference in Java compiling or execution between below statements?

Integer x = new Integer(1);

Integer x = 1;

They both create a new object to my knowledge.
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer x = 1 is not the same as Integer x = new Integer(1), the former is equivalent to

which uses cached Integer objects if the value is within a specific range (-128 to at least 127).
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run the following code

You will get:
true
false
false

Which shows that, as Tobias has already said, each call to new Integer(1) creates a new Object whereas directly assigning an integer is equivalent to Integer.valueOf(1) which for a certain range of values returns a cached Integer object
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the clear explanation. And what does it mean for the cached Integer objects if the value is 128 and more?

Integer x = Integer.valueOf(128);
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that just using a long literal ?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running the code with 128 and see what happens.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:is that just using a long literal ?

Not sure what you mean by that. We are dealing with int and Integer objects
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when Integer a and b are both 128 they are no longer from the same cached Object?



Console output:

false
false
false
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tony, forget my remark "is that just using a long literal ?" it didn't make sense.
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:So when Integer a and b are both 128 they are no longer from the same cached Object?

Right, because integers greater than 127 are not cached.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . integers greater than 127 are not cached.
That is the minimum size of cache; it is quite possible that more values are cached.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!