Java Basics: Common APIs (Math, System, Runtime, Object, BigInteger, BigDecima)

1. Common APIs

1.1 Math class

Rounding up means going to the right of the number axis, and so is negative numbers. It can also be called rounding up, regardless of positive or negative numbers, it will go to the right by one.

Rounding down is to go to the left of the number axis. It can also be called the end method, regardless of whether the number is positive or negative, the number after the decimal point will be removed.

1.1.2 Exercises

// 判断有多少水仙花数
        int count = 0;
        for (int i = 100; i < 1000; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            if (i == Math.pow(ge, 3) + Math.pow(shi, 3) + Math.pow(bai, 3)){
                count++;
            }
        }
        System.out.println("有" + count + "个水仙花数");

1.2 System class

System.exit(), write 0 in it to indicate that the virtual machine stops normally, and write 1 to indicate that the virtual machine stops abnormally.

If the source array and the destination array are both basic data types, then the data types of the two must be consistent. Otherwise, an error will be reported.

The length of the source array and destination array must also be considered, and an error will be reported if it exceeds.

If the data source array and destination array are both reference data types, then the subclass type can be assigned to the superclass type.

1.3Runtime

Runtime indicates the running environment of the current virtual machine

1.4 Object

Object has only parameterless constructor

1.4.1 Common methods

(1) toString

The string returned by the method is: package name + class name + @ + address value.

detail:

System.out.println and toString can also print out the address value

system: class name out: static variable
system.out: get printed object println( ): method parameter: indicates the
content to be printed
string. Then it is printed on the console, and the newline processing is completed after printing.

If you don't want to print the address value, we can rewrite the toString method. (ptg plug-in can generate toString method when generating javabean)

When an object is passed in sout, the toString method will be used at the bottom

(2) equals()

You can only compare whether the address values ​​are the same. If you compare the attribute values, you can override the equals method. You can choose to generate automatically in alt+insert.

(3) clone

Completely copy the attribute value of the A object to the B object, also called object copy, object copy

Details: We can't use clone directly because it is protected, so we need to rewrite the clone method in the encapsulated class, and implement a cloneable interface at the beginning ( the interface does not contain abstract methods ). 

Cloneable
If there is no abstract method in an interface,
it means that the current interface is a marked interface
. Now Cloneable means that once it is implemented, the object of the current class can be cloned.
If it is not implemented, the object of the current class cannot be cloned.

Steps to implement clone:

1. Rewrite the clone method in 0bject
2. Let the javabean class implement the Cloneable interface
3. Create the original object and call clone.

shallow clone

Shallow cloning only clones the address value, the disadvantage is that when the value in the string or array of any one of the objects changes, the other will also change accordingly.

deep clone

Deep cloning is to directly create a new data, and assign the address value of the new array to the new object. The address value of the string is the same because the string pool is reused.

The clone method of object is a shallow clone.

To achieve deep cloning, you need to use third-party tools

1.5 objects

Objects is a tool class that provides some methods to complete some functions.

1.6 BigInteger

1.6.1 Construction method

(1) BigInteger(4, r), means a random number in [0, 15], 15 = 2^4 - 1.

(2) The parameter in the second construction method must be an integer

(3)  1. The numbers in the string must be integers 2. The numbers in the string must match the hexadecimal system. For example, in binary, only 0 and 1 can be written, and an error will be reported when writing others.

(4)  The static method obtains the object of BigInteger, which is internally optimized.
Details:
1. The range that can be represented is relatively small, and it can only be within the value range of 1ong. If it exceeds the range of 1ong, it will not work.
2. The commonly used numbers: -16~16 are optimized internally. Create a BigInteger object with -16 ~ 16 in advance, if it is acquired multiple times, it will not recreate a new one.

1.6.2 Basic usage

Biginteger is an object, and cannot be added, subtracted, multiplied or divided.

1.6.3 Storage method

Group the binary numbers corresponding to the large integers into 32-bit groups, then convert the binary values ​​of the three groups into their corresponding decimal values, and finally put them into the array in order.

signum indicates the sign of a large integer, -1 is a negative number, 0 is 0, and 1 is a positive number.

 1.6.4 Summary

1.7 BigDecima

1.7.1 Construction method

We know that the integer part of the binary is obtained by multiplying the binary number of each bit by the power of 2. The power decreases from right to left, and the same is true for decimals. The power decreases from right to left (-1, -2, -3...), so some specific fractional parts are difficult to express and need to occupy a lot of bytes. But the number of bytes of long and double is limited, so the calculation will be inaccurate.

Details:
1. If the number to be represented is not large and does not exceed the value range of double, it is recommended to use the static method
2. If the number to be represented is relatively large and exceeds the value range of double, it is recommended to use the construction method
3. If we An integer between 0 and 10 is passed, including 0 and 10, then the method will return the created object and will not renew

1.7.2 Basic use

1.7.3 Storage method

First split each symbol in the String, and then find the number corresponding to the corresponding ASCII code table and put it into the array.

1.7.4 Summary

Guess you like

Origin blog.csdn.net/Orange_sparkle/article/details/129242352