Add two integers, checking for overflow.

 
public class Main {
  public static int addAndCheck(int x, int y) {
      long s = (long)x + (long)y;
      if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
          throw new ArithmeticException("overflow: add");
      }
      return (int)s;
  }
}
  
Home 
  Java Book 
    Runnable examples  

Data Type Int:
  1. Create Integer from int value
  2. Convert Integer to Binary String
  3. Convert int to String
  4. Convert octal number to decimal number
  5. Convert binary number to decimal number
  6. Convert decimal integer to octal number
  7. Convert decimal integer to hexadecimal number
  8. Convert hexadecimal number to decimal number
  9. Convert integer to readable format
  10. Convert Integer object to Numeric primitive types
  11. Shift int left
  12. Shift int Right
  13. Unsigned shift int right
  14. Min and Max values of datatype int
  15. Int Overflow
  16. Add two integers, checking for overflow.