From: http://jiangzhengjun.iteye.com/blog/652623
Value expression 1. Parity judgment
Do not use I % 2 = 1 to determine if it is an odd number, because I is not valid when it is a negative odd number, please use I % 2! = 0 to determine whether it is an odd number or use
Efficient (I & 1 )! = 0.
2. Accurately calculate Java code using decimals
- System. Out. println (2.00
-1.10
); // 0.8999999999999999
System. Out. println (2.00-1.10); // 0.8999999999999999
The result calculated above is not 0.9, but a series of decimal places. The problem is that the number 1.1 cannot be exactly expressed as a double, so it is
Shows the double value closest to it. This value is subtracted from 2, but the calculation result is not the double value closest to 0.9.
Generally speaking, the problem is that not all decimals can be exactly expressed using binary floating-point numbers.
Binary floating point is not suitable for currency calculation because it cannot represent 1.0 as any other negative power of 10.
The first way to solve the problem is to use the smallest unit (points) of the currency:
Java code
- System. Out. println (200
-110
); // 90
System. Out. println (200-110); // 90
The second method is to use bigdecimal, but must use the bigdecimal (string) constructor, rather than using bigdecimal (double) to construct (
The float or double type cannot be converted to a string and then bigdecimal (string) is used)
Because the float or double is converted to a string.
). For example, new bigdecimal (0.1), which returns a bigdecimal, that is
0.1000000000000000055511151231257827021181583404541015625
, Correct use of bigdecimal, the program can print out our period
Expected result 0.9:
Java code
- System. Out. println (New
Bigdecimal ("2.0"
). Subtract (New
Bigdecimal ("1.10"
); // 0.9
System. Out. println (New bigdecimal ("2.0"). Subtract (New bigdecimal ("1.10"); // 0.9
To compare the two floating point numbers, use the compareto method of bigdecimal.
For more information, see 《
Floating Point Number profiling in Java
"!
3. Int integer multiplication Overflow
We calculate the number of microseconds in a day:
Java code
- Long
Microsperday = 24
* 60
* 60
* 1000
* 1000
; // The correct result should be: 86400000000
- System. Out. println (microsperday); // actually: 500654080
Long microsperday = 24*60*60*1000*1000; // the correct result should be: 86420.1_system. Out. println (microsperday); // the actual result is: 500654080
The problem is that the computing process overflows. This formula is fully executed using the int operation, and the result is promoted
Long, but it is too late: The computing has exceeded.
The solution is to make the first factor of the computing expression clear as the long type, so that all subsequent calculations of the expression can be forced to use the long operation.
The sample result will not overflow:
Java code
- Long
Microsperday = 24l * 60
* 60
* 1000
* 1000
;
Long microsperday = 24l * 60*60*1000*1000;
4. Negative hexadecimal and octal literal Constants
The types of "numeric literal constants" are int type, regardless of the number of hexadecimal constants, so "2147483648" and "0x180000000 (hexadecimal, a total of 33
The value range of the integer is exceeded.) "the literal constant is incorrect. during compilation, the value exceeds the int value range.
"2147483648l" and "0x180000000l ".
A decimal literal constant has only one feature, that is, all decimal literal constants are positive numbers. To write a negative decimal constant
Simply add "-" before the literal constant.
The hexadecimal or octal literal constants may not necessarily be positive or negative, whether positive or negative, depending on the current situation: If the hexadecimal and octal characters
If the highest bit of a surface constant is set to 1, they are negative numbers:
Java code
- System. Out. println (0x80
); // 128
- // 0x81 is regarded as an int type, and the highest bit (32nd bits) is 0, so it is a positive number.
- System. Out. println (0x81
); // 129
- System. Out. println (0x8001)
); // 32769
- System. Out. println (0x70000001)
); // 1879048193
- // Literal 0x80000001 is int type, the highest bit (32nd bits) is 1, so it is a negative number
- System. Out. println (0x80000001)
); //-2147483647
- // Literal 0x80000001l is forcibly converted to long type, and the maximum bit (64th bits) is 0, so it is a positive number.
- System. Out. println (0x80000001l); // 2147483649
- // Minimum int type
- System. Out. println (0x80000000)
); //-2147483648
- // If the length is more than 32 bits, you must add L to the literal constant to convert it to long. Otherwise, an error occurs during compilation.
- System. Out. println (0x8000000000000000l); //-9223372036854775808
System. out. println (0x80); // 128 // 0x81 is regarded as an int type, and the highest bit (32nd bits) is 0, so it is a positive number system. out. println (0x81); // 129 system. out. println (0x8001); // 32769system. out. println (0x70000001); // The literal 0x80000001 is int type, and the highest (1879048193 bits) is 1, so it is a negative system. out. println (0x80000001); //-2147483647 // The literal 0x80000001l is forcibly converted to the long type. The maximum bit (64th bits) is 0, so it is a positive number system. out. println (0x80000001l); // 2147483649 // minimum int type system. out. println (0x80000000); //-2147483648 // If the length exceeds 32 characters, you must add L to the literal constant to convert it to long. Otherwise, the system will fail during compilation. out. println (0x8000000000000000l); //-9223372036854775808
It can be seen from the above that the hexadecimal literal constant represents the int type. If it exceeds 32 bits, you need to add "L" to the end; otherwise, it cannot be compiled. For example
If the value is 32, it is a positive negative int value. If the value exceeds 32 bits, it is a long value, but it must be explicitly specified as long.
Java code
- System. Out. println (long. tohexstring (0x00000000l + 0 xcafebabe
); // Cafebabe
System. Out. println (long. tohexstring (0x00000000l + 0 xcafebabe); // cafebabe
Why is the result not 0x1cafebabe? The addition executed by this program is a mixed calculation: The left operand is the long type, and the right operand is the int type.
. To perform this computation, Java converts int-type values from the extended native type to the long type, and then adds two long-type values. Because
Int Is a signed integer type, so this conversion is performed by the symbol extension.
The right operand 0xcafebabe of this addition is 32 bits and will be promoted to a value of the long type 0 xffffffffcafebabel. Then this value is added with the left operation.
0x00000000l. When it is regarded as an int type, the high 32-bit value of the right operand after symbol extension is-1, while the 32nd-bit value of the left operand is 1, and the two numbers
The value is added to 0:
0x 0 xffffffffcafebabel
+ 0x 000000000000000l
-----------------------------
0x 00000000 cafebabel
If you want to obtain the correct result 0x1cafebabe, you need to add "L" after the second operation array to explicitly consider it as a positive long type.
The scale symbol is 0:
Java code
- System. Out. println (long. tohexstring (0x00000000l + 0 xcafebabel); // 1 cafebabe
System. Out. println (long. tohexstring (0x00000000l + 0 xcafebabel); // 1 cafebabe
5. Extend the Java code using the symbol bit extension or zero extension when the narrow numeric type is upgraded to the width type
- System. Out. println (int
) (Char
) (Byte
)-1
); // 65535
System. Out. println (INT) (char) (byte)-1); // 65535
Why is the result 65535 instead of-1?
Symbol extension rules when a narrow integer is converted to a wider INTEGER: if the initial value type is signed, the extension is executed (that is, if the symbol bit
If it is 1, it is extended to 1. If it is zero, it is extended to 0). If it is Char, no matter what type it will be upgraded to, it will execute zero extension.
After learning about the above rules, let's take a look at the question: Because byte is a signed type, we are raising the byte value-1 (Binary: 11111111)
When it is upgraded to Char, the symbol bit is extended, and the symbol bit is 1, so we add 8 1 s, and finally 16 1 S. Then, when we upgrade from Char to int
The char type is upgraded to other types. Therefore, the int value is 65535 instead of the symbol extension.
If a char value C is transformed into a type with a wider width, it is only expanded with zero, but if it clearly expresses the intention to expand with zero, you can consider
Use a single bit mask:
Java code
- Int
I = C & 0 xFFFF
; // Essentially equivalent to: int I = C;
Int I = C & 0 xFFFF; // essentially equivalent to: int I = C;
If you want to convert a char value C to an integer with a wider width and a signed extension, convert the char to a short
Char has the same width, but it is signed:
Java code
- Int
I = (short
) C;
Int I = (short) C;
If you want to convert a byte value B to a char without symbolic extension, you must use a single bit mask to limit it:
Java code
- Char
C = (char
) (B & 0xff
); // Char c = (char) B; For signed Extension
Char c = (char) (B & 0xff); // char c = (char) B; For signed Extension
6. (byte) 0x90 = 0x90 )?
The answer is unequal. Although the appearance looks true, It is equal to false. To compare byte values (byte) 0x90 and INT values 0x90, Java
Increase byte to int by extending the native type, and then compare the two int values. Because byte is a signed type, the conversion is
Symbol extension, which increases the negative byte value to an equal int value (10010000111111111111111111111111 10010000) on the number ). In this example, the conversion is to increase (byte) 0x90 to the int value-112, which is not equal to 0x90 of the int value, that is, + 144.
Solution: Use a shielded code to eliminate the impact of symbol extension and convert byte to int.
Java code
- (Byte
) 0x90
& 0xff
) = 0x90
(Byte) 0x90 & 0xff) = 0x90
7. Ternary expressions (? :) Java code
- Char
X = 'X'
;
- Int
I = 0
;
- System. Out. println (true
? X: 0
); // X
- System. Out. println (false
? I: X); // 88
Char x = 'X'; int I = 0; system. Out. println (true? X: 0); // xsystem. Out. println (false? I: X); // 88
Rules for the result type of conditional expressions:
(1) If the second and third operands have the same type, it is the type of the conditional expression.
(2) If the type of an operation is t, t indicates byte, short, or char, And the other operand is an int type "literal constant", and
Its value can be represented by type T, and the type of the conditional expression is T.
(3) otherwise, the type of the operand will be upgraded, and the type of the conditional expression is the type after the second and third operations are upgraded.
The preceding rule is used to solve the above problem. The first expression complies with the second rule: the type of one operand is Char, And the other type is literal
If the input value is 0 in int type, but 0 can be expressed as char, the final return type is indicated by char type. The second expression complies with the third rule: Because I is int
Type variable, and X is char type variable, so it will be upgraded to int type first, so the final result type is int type, but if I is defined as final,
If the returned result type is Char, the second rule is followed because the final type variable replaces the Three-element table with "literal constant 0" during compilation.
Reached:
Java code
- Final
Int
I = 0
;
- System. Out. println (false
? I: X); // X
Final int I = 0; system. Out. println (false? I: X); // X
In jdk1.4 or earlier, what are the conditional operators? :, When the second and continuation three operands are reference types, the conditional operators require one of them
It must be another child type, even if they have the same parent class:
Java code
- Public
Class
T {
- Public
Static
Void
Main (string [] ARGs ){
- System. Out. println (f ());
- }
- Public
Static
T f (){
- //!! 1.4 cannot be compiled, but 1.5 can
- //!! Return true? New T1 (): New T2 ();
- Return
True
? (T) New
T1 (): New
T2 (); // T1
- }
- }
- Class
T1 extends
T {
- Public
String tostring (){
- Return
"T1"
;
- }
- }
- Class
T2 extends
T {
- Public
String tostring (){
- Return
"T2"
;
- }
- }
Public class t {public static void main (string [] ARGs) {system. Out. println (f ();} public static t f (){//!! 1.4 cannot be compiled, but 1.5 can //!! Return true? New T1 (): New T2 (); Return true? (T) New T1 (): New T2 (); // T1} class T1 extends t {Public String tostring () {return "T1 ";}} class T2 extends t {Public String tostring () {return "T2 ";}}
In version 5.0 or later, conditional operators are always valid when two or three operands are allowed to be referenced. The result type is the most
Small public superclass. Common superclasses always exist, because objects are super types of each object type, and the minimum public superclasses above are T, so they can be compiled.