Java programming must be careful with some traps

Source: Internet
Author: User

Java puzzlers (translated in Chinese), a good book, interesting, short and concise text, mainly introduces some traps that must be careful in Java programming ). Chapter 1: expression puzzles. I only want to write down some interesting examples. many interesting things about hexadecimal are not mentioned. Try not to use hexadecimal anyway.

1. Do not use float or double for exact answers. For currency calculations, use int, long, And bigdecimal, and never use bigdecimal (double) constructor ., This constructor uses the exact value of its parameters to construct an instance.

For example:

Public class Change {
Public static void main (string ARGs []) {
System. Out. println (2.00-1.10 );
}
}

We want to print 0.9, but the printing is 0.899999999999999. It is best to use bigdecimal for currency calculation:

Public class Change {
Public static void main (string ARGs []) {
System. Out. println (New bigdecimal ("2.00"). Subtract (New bigdecimal ("1.10 ")));
}
}

2. Long division: when you operate a large number, always be careful with overflow:

Public class Longdivision {
Public static void main (string [] ARGs ){
Final long micros_per_day = 24*60*60*1000*1000;
Final long millis_per_day = 24*60*60*1000;

System. Out. println (micros_per_day/millis_per_day );
}
}

What do you guess to print? 1000? No, 5 is printed. Because the calculation of the first micro_per_day is based on the int type, although the calculation result can be put into long, it has exceeded before it is put. In this case, convert the display location to long:

Final long micros_per_day = 24l * 60*60*1000*1000;

3. Basic Types of transformation operations:

Public class multicast {
Public static void main (string [] ARGs ){
System. Out. println (INT) (char) (byte)-1 );
}
}

The output may surprise you: 65535. The key to the problem is that Java does not explicitly distinguish between the number of signed symbols and the number of unsigned symbols. When performing a char conversion, a rule must be noted that if the initial numeric type is signed, the symbol extension will be executed; if it is Char, then no extension is executed no matter what type it is converted. Because byte is a signed type, symbol extension occurs when-1 is converted to Char, and the 16-bit char as the result is set, therefore, the result is the 16 power of 2 plus-1.

4. It is best to use the second and third operands of the same type to use conditional operators. Guess the output result of the following example:

Public class dosequis {
Public static void main (string [] ARGs ){
Char x = 'X ';
Int I = 0;
System. Out. Print (true? X: 0 );
System. Out. Print (false? I: X );
}
}
You may think that xx should be printed, but this is not the case. The result is x88, why? This is related to the condition operator rules:

A. If the type of the 2nd and third operands is the same, it is also the type of the result of the conditional operator.

B. If the type of an operand is T (t includes byte, short, char), the type of another operand is the constant expression of INT (note that it is a constant) and can be represented by the T type, the result of the conditional expression is T.

C. Otherwise, the operands of the expression are upgraded in binary format, and the result is the upgraded type.

Therefore, the 2nd output statements convert X to the int type, that is, 88. Complicated, right? Oh, so we 'd better use the same type !!

5. Another important thing to note is the composite operator, such as + =, * =,/+. The composite value assignment expression automatically converts the calculated result to the type of the variable on the left. Therefore, narrow transformation should be prevented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.