Skip to content

Instantly share code, notes, and snippets.

@khotyn
Created September 4, 2012 13:20
Show Gist options
  • Save khotyn/3621097 to your computer and use it in GitHub Desktop.
Save khotyn/3621097 to your computer and use it in GitHub Desktop.
Conditional Expression Type Oddity
/**
* 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