...
If we negate Integer.MIN_VALUE we get Integer.MIN_VALUE. So we explicitely check the range
Noncompliant Code Example
Code Block | ||
---|---|---|
| ||
int temp = -result;
|
Compliant Code Example
Code Block | ||
---|---|---|
| ||
if(a == Integer.MIN_VALUE)
throw ArithmeticException;
else
result = --a;
|
SHIFTING
The shift in java is quite different than in C\C++.
...
For instance operations on long are operations on 64 bits. For example addition:
Compliant Code Example
Code Block | ||
---|---|---|
| ||
java.math.BigInteger big_long_max = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)); System.out.println("big_long="+big_long_max); big_long_max = big_long_max.add(java.math.BigInteger.valueOf(1));//same as ++big_long_max System.out.println("big_long="+big_long_max); These print big_long=9223372036854775807 big_long=9223372036854775808//exceeds the maximum range of long, no problem java.math.BigInteger big_long_min = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)); System.out.println("big_long_min="+big_long_min); big_long_min = big_long_min.subtract(java.math.BigInteger.valueOf(1));//same as --big_long_min System.out.println("big_long_min="+big_long_min);//goes bellow minimum range of long, no problem These print: big_long_min=-9223372036854775808 big_long_min=-9223372036854775809 if(big_long < Long.MAX_VALUE && big_long > Long.MIN_VALUE)//value within range can go to the primitive type long value = big_log.longValue();//get primitive type else //Perform error handling. We can not downcast since the value can not be represented as a long |
...