title: Safely casting long to int in Java
body: <p>What's the most idiomatic way in Java to verify that a cast from <code>long</code> to <code>int</code> does not lose any information?</p>
  
  <p>This is my current implementation:</p>
  
  <pre><code>public static int safeLongToInt(long l) {
      int i = (int)l;
      if ((long)i != l) {
          throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
      }
      return i;
  }
  </code></pre>