...
Code Block | ||
---|---|---|
| ||
public int do_operation(int a,int b)
{
int temp = a + b;
//Could result in overflow
//do other processing
return temp;
}
|
If the result of the addition, is greater than the maximum value that the int type can store or less than the minimum value that the int type can store, then the variable temp has a wrong result stored. Although, unlike C\C++ the integer overflow is almost impossible to exploit in Java because of the memory properties in this platform (e.g. explicit array bound checking; if temp has a negative value as a result of an overflow and we use it as an array index we get an java.lang.ArrayIndexOutOfBoundsException) the results of our operation are wrong and can lead to undefined or incorrect behavior
All the of the following operators can lead to overflow (same as in C\C++):
||
Operator |
---|
...
Overflow |
| Operator | Overflow |
| Operator | Overflow |
| Operator | Overflow |
---|
| + | yes | | -= | yes | | << | yes | | < | no |
...