This article introduces data operation overflow, type conversion and operator priority.

The overflow

Each of the data types in Java has a range that can be represented.

public class NumOverflow { public static void main(String[] args) { int intMin = Integer.MIN_VALUE; int intMax = Integer.MAX_VALUE; long longMin = Long.MIN_VALUE; long longMax = Long.MAX_VALUE; System.out.println(" intMin + "~" + intMax); System.out.println("long = "+ longMin +" ~ "+ longMax); }}Copy the code

The output is:

Int ranges from -2147483648 to 2147483647 long ranges from -9223372036854775808 to 9223372036854775807Copy the code

Consider the following question: If we add a value to a variable of type int and it exceeds the maximum value int can represent, what will happen? Will the program report an error?

public class NumOverflow { public static void main(String[] args) { int intMin = Integer.MIN_VALUE; int intMax = Integer.MAX_VALUE; int a = intMax + 10; int b = intMin - 10; // -2147483639 System.out.println(a); // 2147483638 System.out.println(b); }}Copy the code

In fact, the program does not report an error, it will still evaluate normally, but the sign bit has changed, so the result is not as expected.

2147483647: 0111 1111 1111 1111 1111 1111 1111 1111 +10: 0000 0000 0000 0000 0000 0000 0000 1010 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 2147483639: 1000 0000 0000 0000 0000 0000 1001Copy the code

Spillovers may seem like a minor issue, but they often get in the way of companies’ r&d teams.

For example, in the early stage of the company’s development, fields such as user ID and commodity ID may be defined as int types, or the data types defined at the front and back ends are inconsistent. As the amount of data continues to grow, data overflow occurs, resulting in online faults. At this point, the research and development team will spend some effort to identify data overflow potential in each production line.

Integer operations will report an error when the divisor is 0, whereas floating-point operations will not report an error when the divisor is 0, but will return several special values:

  • NaNNot a Number
  • InfinityInfinity
  • -InfinityMinus infinity

Such as:

Public class FloatOverflow {public static void main(String[] args) {// NaN double d = 0.0/0; // Infinity double e = 10.0/0; // -infinity double f = -10.0/0; System.out.println(e); System.out.println(Double.isNaN(d)); System.out.println(Double.isFinite(e)); System.out.println(Double.isInfinite(f)); }}Copy the code

The Double class provides three methods to determine whether a floating point number is infinite and not a number.

Type conversion

When two numbers are evaluated, an implicit conversion is performed to convert one type to the other, and two variables of the same type are then manipulated.

The rule for implicit conversions is to convert in the direction of the arrow.

  • If there is a double operand, the other number goes to double;
  • If I have float, I go to float;
  • If you have long, turn long;
  • Otherwise int;

In the figure above, the orange arrow is a lossless precision conversion, and the black arrow is a lossless precision conversion.

In addition to implicit conversions, Java runs casts. Such as:

Public class NumberCast {public static void main(String[] args) {double f = 89.98; // a = 89 int a = (int) f; long b = 900L; a = (int) b; Long c = a; }}Copy the code

Operator priority

category The operator Correlation between
The suffix () []. (dot operator) Left to right
One yuan expr++ expr– From left to right
One yuan ++expr –expr + – ~! From right to left
multiplicative * / % Left to right
additive + – Left to right
shift >> >>> << Left to right
Relationship between > > < < = = Left to right
equal = =! = Left to right
Bitwise and & Left to right
The bitwise exclusive or ^ Left to right
Bitwise or
Logic and && Left to right
Logic or
conditions ? : From right to left
The assignment = + = = * = / = % = > > < < = = = ^ = | = From right to left

A few key points to remember:

  • The parenthesis has the highest priority and the assignment operator is the lowest.
  • The four operations of addition, subtraction, multiplication and division accord with the definition of priority in mathematics.
  • Autoincrement and subtraction are higher than arithmetic operators;
  • The arithmetic operators are superior to the shift and relational operators.

When writing a program, if you are unsure about the associativity and precedence of the operators, you can add parentheses to make your code more readable and less likely to make errors.

conclusion

  • Overflows can cause unexpected results. Be careful when writing code.
  • Type conversions are classified into implicit conversions and cast conversions. Implicit conversions are automatically upward conversions.
  • Operators distinguish precedence and parentheses when in doubt.

Info The above code is hosted on Github.