Java Advanced---Practical Class

Java Advanced-Practical Class

1. Math:

double Pi = Math.PI;//常量PI
double e = Math.E;  //常量E
Math.random();      //随机数0~1
Math.round(34.3);   //四舍五入
Math.abs(-23.4);    //绝对值
Math.floor(23.4);   //向下取整
Math.ceil(345.6);   //向上取整
Math.sqrt(2);       //根号
Math.log(1);        //log函数
Math.pow(10, 3);    //次方
Math.max(1,4);      //求最大值
Math.min(3.2,43);   //求最小值

There's some left
Insert picture description here

2. Date

//1.Date
Date d = new Date();
System.out.println(d);

//2.SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//字符顺序不能变,中间的格式可以换
String time = format.format(d);
System.out.println(time);

//3.Calender
Calendar calendar = format.getCalendar();
System.out.println(calendar.get(Calendar.HOUR));//从0开始,星期从星期天开始
System.out.println(calendar.get(Calendar.MONTH));//从0开始
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//从1开始
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));//今天在一年中是第几周

Three, String

1. length(): get the length of the string
2. equals(): compare whether the contents of the two strings are the same.
Here to talk about: the difference between equals() and ==:
Insert picture description here
Insert picture description here
3. equalsIgnoreCase(): compare case regardless of case
4. toLowerCase(): convert to lowercase
5, toUpperCase(): convert to uppercase
6, charAt(): get the character at which position
7, toCharArray(): convert to a character array
8, split(): according to the given regularity Match the expression to split this string. Form a new String array.
9, startsWith(): Does the string start with xxx
10, endsWith(): Does the string end with xxx
11, contains(): Does the string contain the specified substring
12, replace(): Replace the string at the
Insert picture description here
end String splicing: s1+s2 [This should be familiar], but there is also a concat() method

The difference between the String class concat method and + in Java:

1. concat: concatenate the specified string to the end of this string. If the length of the parameter string is 0, this String object is returned. Otherwise, create a new String object to represent the character sequence formed by concatenating the character sequence represented by this String, the character sequence represented by the object and the character sequence represented by the parameter string.
Example:
“cares” .concat( “s”) returns “caress”
“to” .concat( “get” ).concat( “her”) returns “together”
Parameters: str-the String concatenated to the end of this String.
Returns: a string representing the character formed by concatenating the character string parameter after the character of this object.

2.+: You can connect any type of data with String type

四、String,StringBuffer,StringBuilder

1. String: immutable length, safe, slowest

Insert picture description here
final type
Through the source code, we can see that the bottom layer of String is actually a character array (char[]), and changing the length will create a new object

2. StringBuffer: variable length, safe and fastest

final type
When the length exceeds the specified length (16), the object will not be created, but the array length will be changed. When the length
is too long, it will automatically expand, similar to the ArrayList
append() method for string splicing

Why is it safe? It is because the methods inside have added the synchronized keyword to make thread synchronization safe
Insert picture description here

3. StringBuilder: variable length, unsafe

final type
The default length of 16 is the
same as StringBuffer, but it is not suitable for multi-threading and unsafe

Guess you like

Origin blog.csdn.net/qq_43288259/article/details/113247038