• 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

Confusion in '==' concept

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was reading katthy and sierra and i came across this fact that if wrappers objects for example


For integers if the range is in -127-128, the objects are considered to be equal. Now consider the
following example


Considering the above code, in case 1, they are equal, while in case 2, they are no equal. Why? Aren't they in range of -127 to 128.
OR here NEW is creating the magic of creating a new object. Please help, why case1 differs from case 2?

Arhaan
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new keyword returns a new object instance. It doesn't matter what value it contains, it will always be a different than an object created by a different constructor invocation.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stephan,

I was looking into this post
and it refers to something like a pooling constant. Is it a valid explanation of this concept?

Thanks again for your prompt response.

Arhaan
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is how you can think about what the program actually does:
As you can see, i1 and i2 will refer to the same object (which is also stored in the pool). i3 and i4 each refer to two other different objects.
The == operator checks whether two operands are the exact same object. This is only true for i1 and i2.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow!!

Thanks Stephen for an awesome explanation. I am quiet clear about it. Thanks a ton.

Arhaan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic