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

Ternary operator - seeking advice

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

I`m here at your mercy hoping someone will pour some light on my ternary operator problem.

As specified by language, ternary operator requires either of its latter 2 operands to pass IS-A test.

Consider the following ( ugly ) code:



When compiled with

javac -source 1.4 Test.java

the code doesn't compile and compiler rightfully complains that neither Double nor Integer are subtype of another.

However, when compiled as 1.6 or as 1.5 source, it compiles fine, runs and always returns objects of type Double.

For example the code above outputs "We got a Double and its value is 10.0"

The question:

Which feature of Java 5 is at work here?

I know its not auto-boxing ( since auto-boxing only deals with boxing and un-boxing primitives where due ) or at least not only. IMHO the code shouldn't compile at all, since there is no way Integer can be cast to Double, and the types of ternary operator operands are specified explicitly as Integer and Double.

My own guess is Integer could be un-boxed to int, implicitly widened to double and then boxed into Double.

[edit]Newlines because lines off edge of screen. CR[/edit]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilya Kalujny wrote:Which feature of Java 5 is at work here?

I know its not auto-boxing ( since auto-boxing only deals with boxing and un-boxing primitives where due ) or at least not only. IMHO the code shouldn't compile at all, since there is no way Integer can be cast to Double, and the types of ternary operator operands are specified explicitly as Integer and Double.

My own guess is Integer could be un-boxed to int, implicitly widened to double and then boxed into Double.



The relevent quote for the JLS comes from section 15.25.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25

Specifically....

The type of a conditional expression is determined as follows:

blah. blah. blah...
Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:

blah. blah. blah...
Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion (§5.1.8) and value set conversion (§5.1.13).



Basically, the type of the ternary is of a primative double. The operands are unboxed (for both cases) and promoted to double (in case of the int), and then reboxed when assigned to the Object.

Henry
 
Ilya Kalujny
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your reply. The relevant parts of JLS make sense.

Henry Wong wrote:
Basically, the type of the ternary is of a primative double. The operands are unboxed (for both cases) and promoted to double (in case of the int), and then reboxed when assigned to the Object.
Henry



However can you elaborate a bit on this part? How could ternary operator operands be of type double when i explicitly specified them to be of type "Double" and "Integer"?

Is it "Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types" at work just to make the code compile?

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilya Kalujny wrote:
However can you elaborate a bit on this part? How could ternary operator operands be of type double when i explicitly specified them to be of type "Double" and "Integer"?

Is it "Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types" at work just to make the code compile?



Not sure of what you want me to elaborate -- that is how it is specified. If you specify the two operands, the JLS defines what the compiler will do with it... and in your case, it will do an unbox and cast to double.

And there is no "just to make the code compile" -- this is the specification, this is how the compiler has to compile it.

Henry
 
Ilya Kalujny
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

It's all clear now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic