Writing high-quality code: 151 tips for improving Java programs (21~30)

Recommendation 21: Use even judgments, not odd judgments

To judge whether a number is odd or even, use: i%2==0? "even": "odd"

Can't use i%2==1? "odd number": "even number", when java does the remainder operation, it judges whether the result is 1, if not, it is an even number, and an error occurs when judging a negative number.

Recommendation 22: Use Integer Types for Currency

Floating point numbers in computers may be inaccurate because of the storage rules for floating point numbers.

The usual practice is to expand the value of the operation by 100 times now, and reduce it by 100 times after the operation is completed.

Recommendation 23: Don't let types silently convert

Java performs the operation first and then performs the type conversion. When the value exceeds the value range, the value will become negative.

Recommendation XXIV: Boundaries

A number out of bounds invalidates the test condition. The number becomes negative when it crosses the bounds.

Recommendation 25: Don't let rounding lose you

According to different scenarios, the rounding method is selected, and rounding cannot be simply selected.

Recommendation 26: Beware of Null Values ​​for Wrapped Types

The wrapper object and the unboxed object can be converted to each other, but the null value cannot be converted to the basic type, so the null value must be checked when the wrapper type participates in the operation.

Recommendation 27: Size comparison of discreet packaging types

The size of the package type cannot be compared with "==", "<", ">", only with compareTo

Recommendation 28: Use the shaping pool first

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int ii = scanner.nextInt();
            System.out.println("\n====" + ii + " equality judgment====");
// Two Integer objects generated by new
            Integer i = new Integer(ii);
            Integer j = new Integer(ii);
            System.out.println("Object generated by new: " + (i==j));

            i = ii;
            j = ii;
            System.out.println("The object of the basic type conversion: " + (i==j));

            i = Integer.valueOf(ii);
            j = Integer.valueOf(ii);
            System.out.println("object generated by valueOf: " + (i==j));
        }
    }
}

The boxing action is implemented by the valueOf method. The numbers from -128 to 127 are directly obtained from the static array cache shaping pool in the IntegerCache when they are packaged into objects, and the addresses are the same.

Recommendation 29: Prefer basic types

Arguments take priority over primitive types, autoboxing or unboxing without knowing which method is executed.

Suggestion 30: Don't set random seeds casually

When the seed is the same, no matter how many times the same machine runs, the random number is the same. So when using random numbers, don't set the seed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855652&siteId=291194637