Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Autoboxing Tricky Question

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, everyone.

Here it goes one of those tricky questions you love the most.

What gets printed? Take your time, be careful, there is an autoboxing issue here.



Once you have finished the first test, change the second autoboxin declaration this way:



...now tell me why?

[ January 12, 2006: Message edited by: Edwin Dalorzo ]
[ January 12, 2006: Message edited by: Edwin Dalorzo ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program does not compile
Integer fiveHundred1 = 500;
You can not assign a int value to Integer object
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mani, comrade, you have to compile it with Tiger (JSE 1.5), there is not such thing as Autoboxing in previous versions of Java.
[ January 12, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like having the scientific explanation. It seems to me that when the value is up to 8 bits (from -128 to 127) Java applies auto-unboxing (Object to primitive) therefore ten1= 10 is equals ten2=10, (both are primtive). When the values is greater that 8 bits, Java wraps the value; since the == operator is used to compare object references no their contents it prints nothing!

I proved this:
The output is : fiveHundred1 is equal fiveHundred2
I need an explanation, too!
[ January 12, 2006: Message edited by: Enrique Villamizar ]
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of those code exhibits I get all exited from ...

The JLS for Java 5.0 indicates that when both operands are numeric and are within the range of -128 and 127 then the equality of both operands are true.

In all other cases, boxing conversion returns a unique reference and both references are compared.

As a side-note the JLS mentions also that the JVM implementation may cache these values.

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.



Please don't use "==" and "!=" to compare objects in your professional environment.

To make things more weird ...
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I feel that if you go through the details in the link given below, it may be helpful.

http://java.sun.com/developer/JDCTechTips/2005/tt0405.html
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Thomas, Sandeep and Enrique.

Yeah, I love this kind of questions too.

Thomas reply is more than correct is the complete answer. I read Sandeep article, it mentions this issue, and hey Enrique, in a primitive way you found the answer.

I guess we must be careful with autoboxing, not only in questions in the exam, but in real life programming.

Thanks to all of you guys. I had fun with this if you have any tricky question I will enjoy playing with them!

[ January 13, 2006: Message edited by: Edwin Dalorzo ]
[ January 13, 2006: Message edited by: Edwin Dalorzo ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic