aspose file tools*
The moose likes Java in General and the fly likes Ternary operator - seeking advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ternary operator - seeking advice" Watch "Ternary operator - seeking advice" New topic
Author

Ternary operator - seeking advice

Ilya Kalujny
Greenhorn

Joined: Mar 20, 2008
Posts: 9
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]


Kalujny Ilya, SCJP.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ilya Kalujny
Greenhorn

Joined: Mar 20, 2008
Posts: 9
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
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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

Joined: Mar 20, 2008
Posts: 9
Thanks,

It's all clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ternary operator - seeking advice