Common API s
ava API is the classes provided to us in JDK. These classes encapsulate the underlying code implementation. We don't need to care about how these classes are implemented, we just need to learn how to use these classes.
There is a Src in the JDK installation directory Zip file. The contents of this file after decompression are the source files of all Java classes. You can view the source code of the corresponding class.
Math math
java.lang.Math class is a program function class of mathematical calculation provided in the whole Java. Using this Zeng Xu function class, you can easily perform some basic mathematical calculations: logarithm, trigonometric function, square root, module, etc. This class is in jdk1 0 is provided to users, and there is no constructor in this class.
1. Take absolute value
System.out.println(Math.abs(-123)); 123
2. Round down
System.out.println(Math.floor(4.3)); 4.0
3. Round up
System.out.println(Math.ceil(4.2)); 5.0
4. Rounding
System.out.println(Math.round(4.5)); 5
5. Square root
System.out.println(Math.sqrt(16,0)); 4.0
6. Power operation
System.out.println(Math.pow(2.0,3.0)); 8.0
7. Maximum
System.out.println(Math.max(3,5,7)); 7
8. Minimum value
System.out.println(Math.min(3,5,7)); 3
9. Random number
System.out.println(Math.random()*100); 0~100 between System.out.println((int)(Math.random()*100)); 0~100 Integer between
Math.rangdon() produces non repeating decimals between 0 and 1, but there will be repeated numbers after expansion and coercion.
System.out.println(UUID.randomUUID().toString());
The second way to generate random numbers: through UUID Randomuuid() produces a random number in hexadecimal and will not be repeated
Date class
java. util. The date class represents a specific moment, accurate to milliseconds.
Continue to refer to the description of the Date class. It is found that Date has multiple constructors, but some of them are outdated, but some of them can convert the millisecond value into a Date object.
Date date = new Date(); System.out.println(date); date.getYear(); // getYear will be crossed out by a black line, which means it is out of date. It is not recommended to use in the future, but it can be used
Date formatting
SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr=dFormat.format(date); System.out.println(dateStr)
String class
java.lang.String class is the implementation of some methods for storing strings.
String str = "IaskldfjakDFAS"; System.out.println(str.length());//Print the length of the string System.out.println(str.charAt(2));//Print characters with subscript 2 System.out.println(str.toCharArray());//Converts a string to an array of character types System.out.println(str.startsWith("i");//Determines whether to start with a specific string (characters in parentheses) System.out.println(str.endsWith("t"));//Determines whether to end with a specific string (characters in parentheses) System.out.println(str.indexOf("a"));//indexOf() looks for the first occurrence of a character or substring System.out.println(str.lastIndexOf("a")); //lastIndesOf() looks for the last occurrence of a string or string System.out.println(str.substring(2));//Intercept from the position marked 2 to the end System.out.println(str.substring(2,5)); //Intercept from the position marked with 2 to the position of 5, but do not include the position of 5 System.out.println(str.replace("a","bj")); //Replace with a character and then call all places in the string where a character appears System.out.println(str.trim());//Remove the spaces at the beginning and end String a="asdf,asdf,asdf,gasd,wer"; String [] b=a.split(",");//When characters in parentheses are read, string segmentation is performed System.out.println(str.toUpperCase());//Convert characters to uppercase System.out.println(str.toLowerCase());//Convert characters to lowercase
object
String s1 = "Hello"; String s2 = new String(s1); System.out.println(s1.equals(s2));//The equals () method compares characters in a string object System.out.println(s1 == s2); // ==Operator to compare whether two objects refer to an instance and whether two objects point to a memory
Accuracy calibration
You can calibrate some numbers with missing accuracy
double a=3.14*2.0*5; System.out.println(a); Result: 31.400000000000002 DecimalFormat dFormat = new DecimalFormat("0.000"); // Decimal table 6 two decimal places String result = dFormat.format(a); System.out.println(result);