Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

question: Wrappers are immutable

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this is from the practice exam1, the answer is D,
i thought it was B.
isn't Wrappers are immutable, and i and i2 both uses the int 420 in the pool.
i actually tested it out.



is there anybody have the same question? thanks!.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you change in your example to you get false. In the K & B book it says on page 246 that if you compare (with ==) two wrapper object of type Integer with the same values then you get true when the common value is between -128 and 127.

John
 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if wrapper1 and wrapper2 corresponding wrapper classes containing same primitive values then it is always the case that wrapper1 == wrapper2 as true when a char in the range \u0000 to \u007f(0-127), and remaining primitive numbers between -128 and 127.
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is work when you are assigning the literal only if you are assigning the objects it show different result.

you can watch this thread it may be helpful to you.

https://coderanch.com/t/536466/java-programmer-SCJP/certification/Integer-class-ocp-exams-book
 
Ruobo Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhhhhhhh yes! can't believe i forgot about this!.

thank you john and naveen!
reply
    Bookmark Topic Watch Topic
  • New Topic