• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Boxing Doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B Chapter3 page 246
Boxing

two instances of the following wrapper objects will always be = = when their primitive values are the same:
  • Boolean
  • Byte
  • Character from \u0000 to \u007f (7f is 127 in decimal)
  • Short and Integer from -128 to 127


  • Output
    C:\SCJP\Chapter3\AutoBoxing>java Auto
    true
    true
    true
    true
     
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello please refer this thread Wrapper class
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rameshwar Soni wrote:Hello please refer this thread Wrapper class



    I didn't find answer of my question in your post. i1 and i2 are in given range then why they are not equal ?
     
    Rameshwar Soni
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    saloni jhanwar wrote: i1 and i2 are in given range then why they are not equal ?



    Yes i1 and i2 are in the given range but when you are using the new keyword to create Objects than irrespective of whether the number is in the given range or not Object will be created.

    That means i1 and i2 will have different address. Hope this helps
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rameshwar Soni wrote:

    saloni jhanwar wrote: i1 and i2 are in given range then why they are not equal ?



    Yes i1 and i2 are in the given range but when you are using the new keyword than irrespective of whether the number is in the given range or not Object will be created.

    That means i1 and i2 will have different address. Hope this helps



    two instances of the following wrapper objects will always be = = when their primitive values are the same:
  • Short and Integer from -128 to 127


  • I am not satisfied with your answer. Do you mean above written in K&B is wrong ?
     
    Rameshwar Soni
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    saloni jhanwar wrote:



    As per your comment in your code your question is why i1 != 12 is true it must have been false.

    As i said
    Number 1 : When you are using "new" keyword Object will be created irrespective of any thing which means that created Object will have its own address.
    Number 2 : == and != are used to check if 2 objects have same address or not and equals() method is used to check if 2 objects have same contents.

    So when you wrote
    i1 is created with address say for example 012fff
    Now you wrote
    i2 is created since you have used "new" keyword so i2 will have its own address say 013hhh

    Now you wrote
    Then definitely it will be true because i1 has a different address as compared to i2.

    This is what i have understood from your comment in question. If i am wrong then i am sorry or be more specific
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I know about equal method so i am not comparing it with == .So question is that as you are right that address will be different of both wrapper class objects so they can't be same, then what case will be when two wrapper class objects will be == when they have same primitive value ? as written in book.Can you give me an example about it. thanks
     
    Bartender
    Posts: 1558
    5
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi saloni jhanwar,

    What Rameshwar Soni is saying is correct. And what Kathy Sierra is saying is also correct.

    Similar to String pool, there's something called as Integer cache in Java (Java SE 5 onwards).
    e.g.
    Similarly, it works for Integer cache:
    But when we say
    What we say is:
    Now, this Integer.valueOf method, it works quite interestingly. What it does is, it pics an Integer reference from cache for all Integers ranging from -128 to 127 (this cache is initialized by IntegerCache which is inner class of Integer class), and hence, when we say
    It actually calls Integer.valueOf(5) two times, and since 5 falls in that range(-128 < 5 < 127), it will return exactly same object, and hence, i3 == i4 is true here.
    But if we say
    Then, cache won't be referred, and i3 == i4 would be false.

    Hence, as Kathy said, Integer would be '==' when values are between -128 to 127, and as Rameshwar said, it won't happen if we use new keyword.

    I hope this helps.
     
    Rameshwar Soni
    Ranch Hand
    Posts: 247
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    saloni jhanwar wrote:then what case will be when two wrapper class objects will be == when they have same primitive value ? as written in book.Can you give me an example about it.



    As == is to check address therefore 2 Objects will be == when they have same address and not same primitive value (keeping the range -128 and 127 in mind).

    Example :
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rameshwar Soni wrote:

    saloni jhanwar wrote:then what case will be when two wrapper class objects will be == when they have same primitive value ? as written in book.Can you give me an example about it.



    As == is to check address therefore 2 Objects will be == when they have same address and not same primitive value (keeping the range -128 and 127 in mind).

    Example :




    OMG thanks i will read it.
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rameshwar Soni wrote:

    saloni jhanwar wrote:then what case will be when two wrapper class objects will be == when they have same primitive value ? as written in book.Can you give me an example about it.



    As == is to check address therefore 2 Objects will be == when they have same address and not same primitive value (keeping the range -128 and 127 in mind).

    Example :



    Thanks Rameshwar Soni it was very good example.
     
    Rameshwar Soni
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic