Skip to content

Instantly share code, notes, and snippets.

@Ayrx
Created August 20, 2013 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ayrx/6281768 to your computer and use it in GitHub Desktop.
Save Ayrx/6281768 to your computer and use it in GitHub Desktop.
A function to safely cast a long to an int without relying on any external libraries. From the brilliant Jon Skeet @ http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
public static int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new IllegalArgumentException
(l + " cannot be cast to int without changing its value.");
}
return (int) l;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment