public static int sum ( int [ ] values , int start , int length ) { long sum = 0 ; for ( int index = start ; index < length ; index ++ ) { sum += values [ index ] ; } if ( sum < Integer . MIN_VALUE ) { die ( "overflow the sum is too small" , sum ) ; } if ( sum > Integer . MAX_VALUE ) { die ( "overflow the sum is too big" , sum ) ; } return ( int ) sum ; }
Sum Provides overflow protection .