Created
September 4, 2012 13:20
-
-
Save khotyn/3621097 to your computer and use it in GitHub Desktop.
Conditional Expression Type Oddity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Conditional Expression Test. The result is | |
* | |
* <pre> | |
* 114.0 | |
* class java.lang.Double | |
* </pre> | |
* | |
* So why the result is 114.0, the ASCII value of <code>'r'</code>? First, the | |
* <code>new Double(0)</code> is convert to primary type <code>double</code>, | |
* and if there is a <code>double</code> in the expression, other primary type | |
* will be converted to <code>double</code>. After that, since the conditional | |
* expression result is a <code>Object</code>, so double is boxed to | |
* <code>Double</code>. | |
* | |
* @author khotyn | |
* | |
*/ | |
public class ConditionalExpressionTest { | |
public static void main(String[] args) { | |
Object obj = true ? 'r' : new Double(0); | |
System.out.println(obj); | |
System.out.println(obj.getClass()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment