• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Regarding Output Of The Code

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

I would be thankful if anyone explain why "ob3 is equal to ob4" is not part of output?



Thanks In Advance,
Gopal
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== is used to compare only primitives. Use equals() method to compare wrapper classes and objects. For your code

if(ob3.equals(ob4))

will result in both the Integers to be true
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is because the Interger is cashed from -128 to 127, anything that's not in that range the result is not guaranteed.
what do I mean when I say it is cashed.
if you have Interger i =20; and Integer j =20, i and j refers to the same, but if you go like Interger i =128 and Interger j =128, two objects are created in the heap, just like when you go like String s = new String("Don"); and String k =new String("Don"), so when you use the == to refers to these integers it will say..no they are not equal
 
reply
    Bookmark Topic Watch Topic
  • New Topic