Java_ One of the common class APIs
Still dazzling 2021-06-04 10:36:30

Math class

Math Class contains some methods for mathematical operations on data , And the methods in this class are all static . A class like this is called a tool class .

 1 public static int abs(int a)
 2  Absolute value of a data
 3 public static double ceil(double n)
 4 Round up a decimal 4.3 --> 5.0
 5 public static double floor(double n)
 6 Round a decimal down 4.3 --> 4.0
 7 public static long round(double n)
 8 Round a decimal 4.3 --> 4 ; 4.5 --> 5
 9
10 public static int max(int a,int b)
11  Find the maximum of two numbers
12 public static int min(int a,int b)
13  Find the minimum of two numbers
14
15 public static double random()
16 Generate [0,1) Random numbers in the range 

 

System class

 1 public static void exit(int n)
 2 sign out Java virtual machine //System.exit(0);
 3
 4 public static long currentTimeMillis()
 5  Get the millisecond value of the current time , distance 1970 year 1 month 1 Japan 0 when 0 branch 0 The millisecond value between seconds
 6
 7 public static void arraycopy(Object src, int srcPos,Object dest,int destPos,int length)
 8  Copy elements from the source array to the target array .
 9  Parameters :
10 src - Source array .
11 srcPos - The starting position in the source array .
12 dest - Target array .
13 destPos - The starting position in the target data .
14 length - The number of array elements to copy .
15
16 int[] arrayA={1,2,3,4,5};
17 int[] arrayB=new int[2];
18 // hold arrayA Medium 3 and 4 These two elements , Assign values to the arrayB in 
19 System.arraycopy(arrayA,2,arrayB,0,2); 

 

Object class

Object Is the parent of all classes , Any subclass can call Object Methods .

 1 public String toString()
 2 Convert an object to a string . String format : Package name + Class name @ Address values .
 3  Print objects every time , The underlying layer will automatically call toString() Method .
 4  Be careful : I wrote my own class , If you don't want to print the address, you can copy it toString() Method
 5 Shortcut key :alt+inert-->toString()
 6
 7
 8 public boolean equals(Object obj)
 9  Used to compare whether two objects are “ equal “.
10 The default comparison is the address value , The bottom one is "=="
11  Be careful : I wrote my own class , If you don't want to compare address values, you can rewrite equals() Method , Compare your own content .
12 Shortcut key :alt+inert-->Equals and HashCode

 

Objects class

Objects Class is a tool class , It is often used to judge the empty of an object .JDK7 Only later

1 public static String toString(Object s,String defalutStr)
2  Convert an object to a string , If the object is null, Returns the default string
3 public static boolean isNull(Object s)
4 Judge whether an object is null Equivalent to s==null
5 public static boolean nonNull(Object s)
6 Judge that an object is not null Equivalent to s!=null

 

BigDegimal class

BigDegimal You can do some precise calculation of data , It can also do some big data operations .

Construction method

1 public BigDecimal(double val)
2  Use double Type of decimal , establish BigDecimal object
3 public BigDecimal(String val)
4 Use String Data of type ( It has to be pure numbers ), establish BigDecimal object 

Four methods of operation

 1 public BigDecimal add(BigDecimal augend)
 2  Add two numbers
 3 public BigDecimal subtract(BigDecimal subtrahend)
 4  Subtracting two numbers
 5 public BigDecimal multiply(BigDecimal multiplicand)
 6  Multiply two numbers
 7 public BigDecimal divide(BigDecimal divisor)
 8  Divide two numbers
 9
10 public BigDecimal divide(BigDecimal divisor,int num,int type)
11  Divide two numbers , You can keep decimals .
12  Parameters :
13  BigDecimal divisor: Divisor
14 int num: Keep the number of decimals
15 int type: The way to keep decimals
16  BigDecimal.ROUND_HALF_UP Round the last decimal place
17  BigDecimal.ROUND_UP Whether the last decimal is full or not 5 All in 1
18  BigDecimal.ROUND_FLOOR The last decimal is slow 5 All give up
19
20 Be careful : Two numbers that need to be calculated , You have to package the tail first BigDecimal object , And then we do the operation through the above method .

 

The basic data type wraps the class

Each basic data type has a corresponding wrapper class , And provides some methods for developers to use .

1  Basic data type Packaging
2 byte Byte
3 short Short
4 int Integer
5 long Long
6 float Float
7 double Double
8 boolean Boolean
9 char Character

obtain Integer object

1 Integer num1 = Integer.valueOf(100);
2 Integer num2 = Integer.valueOf("100");
3
4 // If the data is in -128~127 Within the scope of , The object is the same ; Otherwise, it's a recreated object .

Automatic packing and unpacking

In order to facilitate the basic data type and wrapper class can be directly calculated , all Java Provides automatic packing and automatic unpacking mechanism .

1 // Automatic boxing : Basic types (int) Automatic conversion to Packaging (Integer)
2 Integer num=100; // Equivalent to Integer num = Integer.valueOf(100);
3
4 // Automatic dismantling : Packaging (Integer) Automatic conversion to Basic types (int)
5 int num2=num; // Equivalent to int num2 = num.intValue();

Data transformation of packaging class

If a string is a pure numeric string ( Such as : "12345"), You can't do numerical math , You have to convert a string to a number , To do mathematical operations . Such a transformation method is already provided in the wrapper class

String to other basic data types

 1 【 formula 】: If you want to change the type, you can find the packaging type , call parseXxx Method
 2
 3 // demand 1:"12345" Convert to 12345
 4 int num1=Integer.parseInt("12345");
 5
 6 // demand 2: “3.14” Convert to 3.14
 7 double num2=Double.parseDouble("3.14");
 8
 9 // demand 3: "6.88" Convert to 6.88
10 float num3=Float.parseFloat("6.88");
11
12 ...

Other types are converted to strings

1 1. Direct sum "" Make a splice , Because any data and string splicing result is a string
2 String s=100+"";
3
4 2. have access to String Class valueOf Method
5 String s1=String.valueOf(100);
6 String s2=String.valueOf(3.14);

 

Arrays class

Arrays Is a utility class .Arrays Class provides some methods to operate on arrays ( such as : Search and sort )

1 public static int binarySearch(int[] array,int key)
2  Binary search for the elements of an array . Premise : Elements must be ordered by
3 public static void sort(int[] array)
4  Sort the array .
5 public static String toString(int[] array)
6 Convert an array to a string Format :"[ Elements 1, Elements 2, Elements 3]"

  

Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09