• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Autoboxing/unboxing conundrum

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I understood the ins and outs of auto(un)boxing, until I thought of this simple situation:



When this code runs it outputs "true." I guessed that it would output "false," since I assumed that i would be autoboxed into a new Integer, and that the two Integer objects (their values being >127) would be separate. Why does this example seem to prefer the unboxing over the boxing? What is the rule?

Thanks!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because one of the operands of == is a primitive, the wrapper is autounboxed to a primitive, and the comparison is made.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matthew,

I suggest looking at the "Chew on this!" thread. I have a similar question.

Franz
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For reference, section 5.6.2 of the Java Language Specification defines binary numeric promotion.

5.6.2 Binary Numeric Promotion
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (�5.1.2) to convert operands as necessary:

* If any of the operands is of a reference type, unboxing conversion (�5.1.8) is performed. Then:
* If either operand is of type double, the other is converted to double.
* Otherwise, if either operand is of type float, the other is converted to float.
* Otherwise, if either operand is of type long, the other is converted to long.
* Otherwise, both operands are converted to type int.

After the type conversion, if any, value set conversion (�5.1.13) is applied to each operand.

Binary numeric promotion is performed on the operands of certain operators:

* The multiplicative operators *, / and % (�15.17)
* The addition and subtraction operators for numeric types + and - (�15.18.2)
* The numerical comparison operators <, <=, >, and >= (�15.20.1)
* The numerical equality operators == and != (�15.21.1)
* The integer bitwise operators &, ^, and | (�15.22.1)
* In certain cases, the conditional operator ? : (�15.25)
 
Matthew Alesi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton, this clears a lot up!!
 
Do you pee on your compost? Does this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic