Use of JDK Chinese API documents
JDK11 Chinese API document download link
Before learning common classes, I will first introduce the use of the following JDK API documents. If you don't have one, you can search online documents or download resources, as shown above
Using API documents, we can quickly query the functions and usage methods of the methods contained in the classes we need.
Generally, we first open the API document and then use the index to search the class name, as shown in the figure below
String
A string is a class. A string variable is a reference data type
The content of "" is a string. As long as it is a string, you can call the method in the string
System.out.println("zhangsan".equals("ddd"));
The common methods in the string are as follows
Declare the following objects first
String strA = "zhangsan"; String strB = "LISI"; String strC = " Wang Wu";
Length returns the string length length(). Note that the length at this time is after the method rather than the variable length
System.out.println("Length of returned string:"+strA.length());
String concat enation or+
System.out.println("String splicing:"+strA.concat("Wang Wu")); System.out.println("String splicing:"+strB.concat("Li Si"));
String judgment
Equality judgment
System.out.println("Compare the contents of two strings and return true Otherwise return false: "+strA.equals("zhangsana"));
Judge whether it is empty
System.out.println("Judge whether the string is empty and return true Otherwise return salse: "+strA.isEmpty());
Equality judgment ignores case
System.out.println("Comparing the contents of two strings, ignoring equal case, returns true Otherwise return false: "+strA.equalsIgnoreCase("ZHANGSAN"));
The integer 0 returned by dictionary order comparison represents the same row. It is a negative number before it, otherwise it is a positive number
System.out.println("The integer 0 returned by dictionary order comparison represents the same row. It is a negative number before it, otherwise it is a positive number: "+strA.compareTo(strB));
Compare in dictionary order, ignoring case
System.out.println("Compare in dictionary order, ignoring case:"+strA.compareToIgnoreCase("ZHANGSAN"));
Judge whether to start with a specific character, return true, otherwise return false
System.out.println("Determines whether to start with a specific string:"+strA.startsWith("zh"));
Determines whether to end with a specific string
System.out.println("Determines whether to end with a specific string: "+strA.endsWith("a"));
String conversion
Convert all strings to uppercase
System.out.println("Convert all strings to uppercase:"+strA.toUpperCase());
Convert all strings to lowercase
System.out.println("Convert all strings to lowercase:"+strB.toLowerCase());
Convert string to byte array
System.out.println("Convert string to byte array:"+strA.getBytes());
Converts the basic data type to a string
System.out.println("Converts the basic data type to a string:"+strA.valueOf(123));
Remove the front and back blanks
System.out.println("Remove the previous blank:"+strC.trim());
Returns the index (subscript) of a string
System.out.println("Returns a string according to the index. The contents of the index:"+strA.charAt(3)); System.out.println("Returns its index value based on a string:"+strA.indexOf("s"));
String interception
System.out.println("Intercept from index value to end:"+strA.substring(2)); System.out.println("From the beginning of the index value to the end of the index value, it is closed on the left and opened on the right"+strA.substring(2, 4));
Character string segmentation is based on specific characters, and the split string is received as a character array
String strD = "The shade of green leaves is thick, and the pools, pavilions and water pavilions are all over. It's much cooler"; String[] strE = new String[3]; strE= strD.split(","); for(String str:strE) { System.out.println(str); }
Complete code
public class StringDemo { public static void main(String[] args) { //A string is a class. A string variable is a reference data type //The content of "" is a string. As long as it is a string, you can call the method in the string System.out.println("zhangsan".equals("ddd")); //The common methods in the string are as follows String strA = "zhangsan"; String strB = "LISI"; String strC = " Wang Wu"; //Length returns the string length length(). Note that the length at this time is after the method rather than the variable length System.out.println("Length of returned string:"+strA.length()); //String concat enation or+ System.out.println("String splicing:"+strA.concat("Wang Wu")); System.out.println("String splicing:"+strB.concat("Li Si")); //String judgment //Equality judgment System.out.println("Compare the contents of two strings and return true Otherwise return false: "+strA.equals("zhangsana")); //Judge whether it is empty System.out.println("Judge whether the string is empty and return true Otherwise return salse: "+strA.isEmpty()); //Equality judgment ignores case System.out.println("Comparing the contents of two strings, ignoring equal case, returns true Otherwise return false: "+strA.equalsIgnoreCase("ZHANGSAN")); //The integer 0 returned by dictionary order comparison represents the same row. It is a negative number before it, otherwise it is a positive number System.out.println("The integer 0 returned by dictionary order comparison represents the same row. It is a negative number before it, otherwise it is a positive number: "+strA.compareTo(strB)); //Compare in dictionary order, ignoring case System.out.println("Compare in dictionary order, ignoring case:"+strA.compareToIgnoreCase("ZHANGSAN")); //Judge whether to start with a specific character, return true, otherwise return false System.out.println("Determines whether to start with a specific string:"+strA.startsWith("zh")); //Determines whether to end with a specific string System.out.println("Determines whether to end with a specific string: "+strA.endsWith("a")); System.out.println(""); System.out.println(""); //String conversion //Convert all strings to uppercase System.out.println("Convert all strings to uppercase:"+strA.toUpperCase()); //Convert all strings to lowercase System.out.println("Convert all strings to lowercase:"+strB.toLowerCase()); //Convert string to byte array System.out.println("Convert string to byte array:"+strA.getBytes()); //Converts the basic data type to a string System.out.println("Converts the basic data type to a string:"+strA.valueOf(123)); //Remove the front and back blanks System.out.println("Remove the previous blank:"+strC.trim()); System.out.println(""); System.out.println(""); //Returns the index (subscript) of a string System.out.println("Returns a string according to the index. The contents of the index:"+strA.charAt(3)); System.out.println("Returns its index value based on a string:"+strA.indexOf("s")); System.out.println(""); System.out.println(""); //String interception System.out.println("Intercept from index value to end:"+strA.substring(2)); System.out.println("From the beginning of the index value to the end of the index value, it is closed on the left and opened on the right"+strA.substring(2, 4)); //Character string segmentation is based on specific characters, and the split string is received as a character array String strD = "The shade of green leaves is thick, and the pools, pavilions and water pavilions are all over. It's much cooler"; String[] strE = new String[3]; strE= strD.split(","); for(String str:strE) { System.out.println(str); } } }
Schedule
For more useful methods, please refer to the JDK API documentation
StringBuffer and StringBuilder
StringBuffer and StringBuilder are variable length strings. We can append the strings directly
String is an immutable length string. Each new assignment is equal to redefining it, which is very inefficient
The common methods of StringBuffer and StringBuilder are the same, generally including append, delete, insert and modify
The usage is as follows
public class StringBufferDemo { public static void main(String[] args) { //Variable length string //statement StringBuffer sb=new StringBuffer(); //Add sb.append("one"); sb.append("two"); sb.append("three"); sb.append("four"); System.out.println(sb); //delete System.out.println("Start delete"); System.out.println("From the beginning of index deletion to the end, the index is left closed and right open:"+sb.delete(1, 3)); //insert System.out.println("Insert the index value of the first parameter into the index position and the content of the second parameter:"+sb.insert(1, "two or three")); //modify System.out.println("Change from start index to end index: "+sb.replace(1, 3, "seven or eight")); //Convert to String System.out.println("convert to String:"+sb.toString()); } }
The difference between StringBuffer and StringBuilder
There is little difference between the usage of StringBuffer and StringBuilder
The main difference is that StringBuffer is thread safe and StringBuilder is non thread safe
The use of both is generally divided into cases (single thread and multi thread)
The specific characteristics of the two are as follows
String class
➥ strings of immutable length
➥ when splicing, a new string is actually created to replace the original string, so the efficiency of string addition is very low
StringBuffer class
➥ thread safe, variable length strings
➥ it is used more in multithreading operation, and it is more efficient to append strings
StringBuilder class
➥ thread unsafe, variable length strings
➥ string appending is most efficient in a single threaded operation
Date time class
We usually use the Date class to handle time in milliseconds
Generally, it includes obtaining the current time and customizing the time, and passing in a long integer data
Use as follows
Date date = new Date(); //Gets the time when the data was created System.out.println(date); //Incoming milliseconds custom time Date dateA = new Date(600000000000l); System.out.println(dateA); //Gets the millisecond value of the time System.out.println("Time millisecond value:"+date.getTime());
However, the output time format is not in line with our aesthetics
We usually use the SimpleDateFormat class to convert the time format
The usage is as follows. Note that M represents month and M represents minute
Date date = new Date(); //Gets the time when the data was created System.out.println(date); //Incoming milliseconds custom time Date dateA = new Date(600000000000l); System.out.println(dateA); //Gets the millisecond value of the time System.out.println("Time millisecond value:"+date.getTime()); //The format looks uncomfortable. We can convert it to conform to Chinese aesthetics //Time format conversion class SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println(simpleDateFormat.format(date));
Math operation class
This method has been used by everyone. Its methods are static methods, which can be accessed directly using the class name without instantiating the object
There are several common usages. You can see the use of random numbers
//use Π value System.out.println(Math.PI); //Maximum of two numbers System.out.println(Math.max(20, 22)); //Minimum of two numbers System.out.println(Math.min(50, 12)); //rounding System.out.println(Math.round(36.7)); //absolute value System.out.println(Math.abs(-11)); //Generate decimals between random numbers 0.0-1.0 System.out.println(Math.random()); //Between 0-10 System.out.println((int)(Math.random()*10)); //0-100 System.out.println((int)(Math.random()*100)); //Root seeking System.out.println(Math.sqrt(4)); //Seeking square System.out.println(Math.pow(2,10));
Packaging
In order to realize more complex data processing, Java arranges wrapper classes for 8 basic data types for complex data processing
The corresponding relationship is shown in the figure below
Packing and unpacking of packaging
Simply put, boxing is to package the basic data type into its packaging class instance for data processing. Disassembly is to get the data in the packaging class into the basic data type
Packing and unpacking can be done automatically
int i = 100; //Packing //Direct declaration auto packing common Integer k = i; Double l= 15.6; //Create manually Integer s = new Integer(12); //Unpacking method //Automatic unpacking i = s; //Manual unpacking int p = k.intValue();
Key role
The data types we receive from web pages are all string types, so we need to convert the data if we want to perform operations. Therefore, the data processing method (static method) of packaging class is particularly important
Automatic unpacking after sample data processing
//Why use wrapper classes? A function that has to be used to convert a string into data in any format, parseInt, parsefloat, etc String strA="1234"; String StrB = "true"; int testA = Integer.parseInt(strA); boolean testB = Boolean.parseBoolean(StrB); System.out.println(testA+12); if(testB) { System.out.println(testB); }
Arrays array class
Commonly used are basically ascending and searching
The specific usage is as follows
//Define an array int[] testA = {1,8,95,33,99,152}; //Arrays sort //In ascending order, notice that the array itself has changed Arrays.sort(testA); for(int i:testA) { System.out.println(i); } //Descending order //The value specified in the Arrays search must be an ordinal return index value System.out.println(Arrays.binarySearch(testA, 95));
System class
The commonly used methods to get the current time of the system and exit the millisecond error output are as follows
//Error output System.err.println("Error output"); //Returns the millisecond value of the current time System.out.println(System.currentTimeMillis()); //System exit normal exit 0 abnormal exit System.exit(0); System.out.println("The system has exited");
Timed task
The specific method is not detailed
The details are as follows
import java.util.Timer; import java.util.TimerTask; public class MyTask extends TimerTask{ @Override public void run() { // Method stub automatically generated by TODO //Tasks requiring timing System.out.println("Output every two seconds"); } public static void main(String[] args) { Timer timer = new Timer(); MyTask myTask = new MyTask(); //The first parameter is the task object. The second parameter is how many milliseconds to start. The third parameter is how many milliseconds to execute once. It is executed only once without writing timer.schedule(myTask, 1000,2000); } }