title: Returning null as an int permitted with ternary operator but not if statement
body: <p>Let's look at the simple Java code in the following snippet:</p>
  
  <pre><code>public class Main {
  
      private int temp() {
          return true ? null : 0;
          // No compiler error - the compiler allows a return value of null
          // in a method signature that returns an int.
      }
  
      private int same() {
          if (true) {
              return null;
              // The same is not possible with if,
              // and causes a compile-time error - incompatible types.
          } else {
              return 0;
          }
      }
  
      public static void main(String[] args) {
          Main m = new Main();
          System.out.println(m.temp());
          System.out.println(m.same());
      }
  }
  </code></pre>
  
  <p>In this simplest of Java code, the <code>temp()</code> method issues no compiler error even though the return type of the function is <code>int</code>, and we are trying to return the value <code>null</code> (through the statement <code>return true ? null : 0;</code>). When compiled, this obviously causes the run time exception <code>NullPointerException</code>. </p>
  
  <p>However, it appears that the same thing is wrong if we represent the ternary operator with an <code>if</code> statement (as in the <code>same()</code> method), which <em>does</em> issue a compile-time error! Why?</p>