• 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

Query with == for Boolean

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
The K&B book says that

"In order to save memory, 2 instances of the following wrapper objects will always be == when their primitive values are same -
  • Boolean
  • Byte
  • Short and Integer from -128 to 127
  • Character from \u0000 to \u007f


  • So in the following code snippet


    should print true true true
    but prints true false false

    It seems correct according to the traditional definition of == but wrong according to the K&B explanation.

    Why is " true false false " the correct output/

    Thanks in advance.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    "In order to save memory, 2 instances of the following wrapper objects will always be == when their primitive values are same -




    Did you happen to take the statement out-of-context? Maybe it was referring to the valueOf() method? Or maybe it was talking about autoboxing?

    Henry
     
    Chaitanya Lele
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not really...
    the example given is


    this produces the output

    same object
    meaningfully equal

    and what follows is the above statement about behavior of == for some wrapper objects.

    So unless I am missing something the explanation in K&B has to be wrong.
     
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Chaitanya Lele,

    To my understanding, == says whether the two objects are the same or not.




    It should give
    true
    false

    Whenever you use a new and invoke a constructor explicitly, an object is created.
    So, two different objects are created for lineA and lineB.

    However, in lineX an object is created and in lineY the reference variable is made to refer to the same object that was created in the previous line.

    HTH,
    Vishwa
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    So unless I am missing something the explanation in K&B has to be wrong.



    Unfortunately, you are missing something.... Notice that the example uses autoboxing. And notice that the example has them as the same object.


    It is your example that doesn't use autoboxing. And it is your example that shows them as different. The book has the correct example, and shows it as explained.

    Henry
     
    Chaitanya Lele
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh ok
    I think I get it now...

    Correct me if I am wrong -

    When the first wrapper reference variable is declared, a wrapper object is instantiated with an implicit call to new like
    Subsequently any Integer reference variable will point to the same object that ( in this case a refers to) if they have the same value...
    unless of course it is explicitly instantiated like

     
    Ranch Hand
    Posts: 757
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    <please delete>
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yep.

    Subsequently any Integer reference variable will point to the same object that ( in this case a refers to) if they have the same value...



    Ya, but remember even when they have the same values, two different objects might get created. It happens for Shorts and Integers when the values are greater than 127... as you quoted there from the book...


    Vishwa
     
    Chaitanya Lele
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot Henry and Vishwanath
     
    A timing clock, fuse wire, high explosives and a tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic