Skip to content

Instantly share code, notes, and snippets.

@khotyn
Created September 4, 2012 13:20
Show Gist options
  • Select an option

  • Save khotyn/3621097 to your computer and use it in GitHub Desktop.

Select an option

Save khotyn/3621097 to your computer and use it in GitHub Desktop.

Revisions

  1. khotyn created this gist Sep 4, 2012.
    25 changes: 25 additions & 0 deletions ConditionalExpressionTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    /**
    * 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());
    }
    }