Java is in hand, I have the world! ! ! Java basics-Math class and Arrays class

Math class definition

The java.util.Math class is a tool class related to mathematics, which provides a large number of static methods to complete operations related to mathematical operations. Because a large number of static methods are provided in the Math class, and the construction method uses private modification, it is private, so when using it, you can directly use [class name. method name] instead of using a new object.
It should be noted that this class, like the Sting class, is modified by final, so it cannot have subclasses.

Common methods of Math class

1. public static double abs(double num): take the absolute value (support multiple overloads: double, long, float can be)
[overload] public static long abs(long a)
public static float abs(float a)

2.public static double ceil(double num):向上取整

3.public static double floor(double num): round down

4.public static long round(double num): rounding

5.public static int max(int ​​a, int b): take the maximum value and return (support multiple overloads: int, long, float, double are all available)
[overload] public static double max(double a, double b)
public static float max(float a, float b)
public static long max(long a, long b)

6. public static double min(double a, double b): take the minimum value and return (support multiple overloads: int, long, float, double are all available)
[overload] public static float min(float a, float b)
public static long min(long a, long b)
public static int min(int a, int b)

7.public static double random(): Random decimal generation, returns a double value between [0.0, 1.0)

8.Math.PI represents the approximate constant of pi (double) and
its value is: public static final double PI = 3.14159265358979323846;

9. Common methods related to trigonometric functions
public static double sin(double a): find the sine
public static double cos(double a): find the cosine
public static double tan(double a): find the tangent
public static double acos(double a) : Inverse cosine
public static double asin(double a): Inverse sine
public static double atan(double a): Inverse tangent

Math class code example

public class Demo01Math {
    
    
    public static void main(String[] args) {
    
    

         //获取绝对值
        System.out.println(Math.abs(3.14));     //3.14
        System.out.println(Math.abs(0));     //0
        System.out.println(Math.abs(-3.14));     //3.14
        System.out.println("****************************************");

        //向上取整
        System.out.println(Math.ceil(3.9));     //4.0
        System.out.println(Math.ceil(3.0001));     //4.0
        System.out.println(Math.ceil(3.0));     //3.0
        System.out.println("****************************************");

        //向下取整,抹零
        System.out.println(Math.floor(30.9));   //;30.0
        System.out.println(Math.floor(30.1));   //30.0
        System.out.println(Math.floor(31.0));   //31.0
        System.out.println("****************************************");

        //四舍五入,不带小数点
        System.out.println(Math.round(30.2));       //30
        System.out.println(Math.round(40.5));       //41

        //两数取最大值
        System.out.println(Math.max(2,8));      //8

        //两数取最小值
        System.out.println(Math.min(10.5,-7.8));        //-7.8

        //随机小数生成,返回[0.0,1.0)之间的double值
        for (int i = 0; i < 100; i++) {
    
    
            System.out.println(Math.random());
        }
    }
}

Arrays class definition

java.util.Arrays is a tool class related to arrays, which provides a large number of static methods to implement common operations on arrays. Like the String class and Math class, it can be used directly through [class name. method name].

Common methods of Arrays class

public static String toString (array): turn the parameter array into a string (according to the default format: [element 1, element 2, element 3...])
public static void sort (array): array according to the default ascending order (smallest to largest) Sort the elements

Remarks:
1. If it is a number, sort is sorted in ascending order by default
. 2. If it is a string, sort is sorted in alphabetical order by default
. 3. If it is a custom type, then this custom class needs to be supported by the Comparable or Comparator interface.

Arrays class code example

public class Demo01Arrays {
    
    
    public static void main(String[] args) {
    
    
        int[] arrayInt=new int[]{
    
    5,9,8,};
        //将int[]数组按照默认格式变成字符串
        String strInt= Arrays.toString(arrayInt);
        System.out.println(strInt);     //[5, 9, 8]

        int[] arrayInt1={
    
    6,56,7,45,89,5};
        Arrays.sort(arrayInt1);
        System.out.println(Arrays.toString(arrayInt1));     //[5, 6, 7, 45, 56, 89]

        String[] arrayStr={
    
    "bbb","aaa","ccc"};
        Arrays.sort(arrayStr);
        System.out.println(Arrays.toString(arrayStr));     //[aaa, bbb, ccc]
    }
}

Guess you like

Origin blog.csdn.net/wtt15100/article/details/108063420