This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question: Wrappers are immutable

 
Ruobo Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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!.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • 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
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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.

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

thank you john and naveen!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic