• 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

Using ==,equals with Wrappers and Strings Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



here,equals see's if both contain the same content.according to me,f1 and d1 have same content
-----------------------------------------------------------------------------------------------------------

here,equals see's if both contain the same content.according to me,f1 and d1 have same content

-------------------------------------------------------------------------------------------------------



does here equals checks if both have same values??

why are the above comparison not true
please anyone can give me detailed explanation for this
i have seen documentation and sun/tutorials but still not able to get why they are false

-------------------------------------------------------------------------------------------------------


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
here,equals see's if both contain the same content.according to me,f1 and d1 have same content
-----------------------------------------------------------------------------------------------------------



Float is not a Double . so instanceof check fails in equals method of Float for a Double object. see Float#equals
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:
Float is not a Double . so instanceof check fails in equals method of Float for a Double object. see Float#equals



a.where did instanceof came in equals ??
b.what's Float#equals
Seetharaman ,there are 2 more queries as below
--------------------------------------------------
Query-2



Here f1 would be unboxed to byte and comparison would be primitive to primitive,
so how's equals returninig false here


----------------------------------------------------
Query-3



output:false

what about this??
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:

a.where did instanceof came in equals ??
b.what's Float#equals


Have look on this
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
Query-2



Here f1 would be unboxed to byte and comparison would be primitive to primitive,
so how's equals returninig false here


No, not f1 is unboxed to primitive. But, the d1 is boxed to Wrapper, and then passed to the equal method. There, check my above link, as it was indicated. The instanceof operator is used. So, both are not same type return false even though they are posses same value!
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,Abimaran Kugathasan

but what about other 2 queries ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:thanks,Abimaran Kugathasan

but what about other 2 queries ?


mohitkumar gupta, check the String class equals method on that provided link, then you can easily get the idea! It's same!

And, one kind advise, please, don't rush here and there. First, clear a chapter, and goto the other chapter, it'll be easier for you to grasp the idea. This is my kind request. Otherwise, you'll loose the ideas.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,Abimaran Kugathasan

i have gone through the link
but i am unable to understand the output of QUERY-3
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
Query-3



output:false

what about this??


gas refers the object which is created on the heap, because, you've used the new operator! So, s is on the String Constant Pool, So, are not the same objects. Hence, false!

Got it?
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic