In the definition of variables, there are many problems to be aware, accidentally there will be loss of accuracy or incompatibility types and other issues.
1. When defining long data, you must add the suffix l or L
Long l = 123456789012345L
2. When defining a single precision type (7-8 significant digits), the suffix f or F must be added
Float f = 12.5F
3. boolean type can not be converted to other data types.
Of these, we often encounter data type conversion problem, the most common to be an implicit conversion and coercion, and we analyze.
From small to large, can be implicit conversion, the data type will automatically upgrade.
Byte, short, char -> int -> long -> float -> double
Note: long is 8 bytes, float is 4 bytes.
Long is an integer, float is a floating-point, integer and floating-point storage rules are not the same, remember that the scope of a little long is less than float.
Byte a = 10;
Int b = a;
When compiling intb = a, a is implicitly converted to an int.
From large to small (if you explicitly know that the data can be used to represent the data type, you can use the cast)
(Converted data type) variable or value.
Note: Under normal circumstances, do not recommend the use of mandatory type conversion.
example 1 :
Int a = 10;
Byte b = (byte) a;
When compiling byte b = (byte) a, a is cast to byte.
Public static void main (String  args)
Byte b = (byte) 130;
System.out.println (b); // Print the result -126
Data 130 is the default type of int decimal data,
The first step: decimal 130 converted to binary data.
The second step: 130 in memory is represented as follows
Original: 0000000000000000 00000000 10000010
The third step: seeking int130 complement
Because 130 is a positive number, so the code and complement are the original code and consistent.
Complement: 0000000000000000 00000000 10000010
The fourth step: the complement code to intercept, leaving only the last 8.
(Byte) 130 of the complement is: 10000010
The fifth step: the complement code into the original code.
Since the sign bit (first bit) is 1, the number is negative,
Counter-code: 10000001 (complement-1)
Original code: 11111110 (sign bit unchanged data bit inversion)
Converted to decimal -126, so the final print -126.
Shorts = 1;
S = s + 1;
Shorts = 1;
S + = 1;
Is there a problem? Why?
The first program will report error: Error: Incompatible type: From int to short there may be losses
Reason: s = s + 1; s + 1 will be implicitly converted to int type, when an int type assignment to the short type is, may be lost.
The second program can compile and run.
S + = 1, although there can be seen as s = s +1, but there are differences, s + = 1 in a coercion, namely s = (short) (s + 1) Cast to short type, it will not error.
Data type conversion problems If some small program, we may be able to see at a glance, but when writing a large system, with a huge amount of data, these small problems may lead to system errors or even collapse, so the previous code The rigor of preparation must rely on our own grasp.