Math class: Math tool class, do some math calculation, square, logarithm, trigonometric function, etc.
All methods are static methods. They do not need to create objects. They can be invoked directly with class names.
Example:
Here are some of the daily development will be used, such as trigonometric functions and other ordinary will not be used, you can understand.
package demo; public class MathDemo { public static void main(String[] args) { function1(); function2(); function3(); function4(); function5(); } public static void function1() { int i = Math.abs(-9); System.out.println(i); // Output: 9, for absolute value } public static void function2() { double d1 = Math.floor(6.5); double d2 = Math.ceil(6.5); System.out.println(d1);// 6.0 System.out.println(d2);// 7.0 } public static void function3() { double d1 = Math.pow(2.0, 3.0); System.out.println(d1); // 2 Third power, output: 8.0 double d2 = Math.sqrt(4.0); System.out.println(d2); // 4 Development, Output 2.0 } public static void function4() { for (int i = 0; i < 10; i++) { double d = Math.random(); System.out.println(d); //Print random numbers, ranging from 0 to 1 } } public static void function5() { double d = Math.round(1.4234); System.out.println(d); //Output 1.0,Get rounded values } }
Array class: Array tool class, which helps us to do some operations on Arrays:
Some methods can save us a lot of code in daily development.
Example:
package demo; import java.util.Arrays; public class ArraysDemo { public static void main(String[] args) { function1(); function2(); function3(); } public static void function1(){ //For array ascending ordering, there is a much better fast ranking than bubble ordering. int[] arr = {5,4,8,7,3,1,6}; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } //Ascending printing } public static void function2(){ //The binary search of arrays, which I introduced in my sixth article, is roughly the same as here. int[] arr = {11,4,5,7,9,13,1}; //The premise of dichotomy is ordered array Arrays.sort(arr); int index = Arrays.binarySearch(arr, 7); System.out.println(index);//All of 7 after sorting is 3 //Note here: If this element does not exist in the array //Then return a negative number:(-insertion point-1) } public static void function3(){ int[] arr = {1,2,3,4,5}; String string = Arrays.toString(arr); System.out.println(string); //Output:[1, 2, 3, 4, 5] Character string //Principles have been written in my previous articles. } }
Sometimes arrays are larger than long size and need to be computed. What should we do?
BigInteger Class: Large Number Operations
Example:
package demo; import java.math.BigInteger; public class BigIntegerDemo { public static void main(String[] args) { function(); } public static void function() { BigInteger b1 = new BigInteger("6666666666666666666666666666666"); BigInteger b2 = new BigInteger("1234567891234567891234567891231123123"); BigInteger b3 = b1.add(b2);// b1+b2 System.out.println(b3);// 1234574557901234557901234557897789789 BigInteger b4 = b1.subtract(b2);// b1-b2 System.out.println(b4);// -1234561224567901224567901224564456457 BigInteger b5 = b1.multiply(b2);// b1*b2 System.out.println(b5);// It's a long number. It's not duplicated here anymore. BigInteger b6 = b2.divide(b1);// b2/b1 System.out.println(b6);// 185185 } }
BigDecimal Class: Floating Point Large Number Running to Improve Floating Point Operating Accuracy
Example:
package demo; import java.math.BigDecimal; public class BigDecimalDemo { public static void main(String[] args) { System.out.println(0.09+0.01);//Output 0.99999 System.out.println(1.0-0.32);//Output 0.67999 //It was found that there would be inappropriate results. //Cause: In computer binary, it is caused by the imprecision of floating point number. //Solution: BigDecimal class function1(); function2(); } public static void function1(){ BigDecimal b1 = new BigDecimal("0.09"); BigDecimal b2 = new BigDecimal("0.01"); BigDecimal b3 = b1.add(b2);//b1+b2 System.out.println(b3);//Get 0.10,Correct BigDecimal b4 = new BigDecimal("1.0"); BigDecimal b5 = new BigDecimal("0.32"); BigDecimal b6 = b4.subtract(b5);//b4-b5 System.out.println(b6);//0.68,Correct BigDecimal b7 = b1.multiply(b2);//b1*b2 System.out.println(b7);//0.0009,Correct } public static void function2(){ //There are some differences in division. BigDecimal b1 = new BigDecimal("1.301"); BigDecimal b2 = new BigDecimal("101"); //BigDecimal b3 = b1.divide(b2);//If you don't divide it, there will be an exception. BigDecimal b3 = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP); //The second parameter is to retain the meaning of two decimal places. //The third parameter is the retention mode: the closest number rounding (i.e., rounding reservation mode), and many other modes. System.out.println(b3);//Keep two and get 0..02 Result } }