Directory Structure
- 1. Object class
- 2. String class
- 3. StringBuffer class
- 4. Array
- Array advanced bubble sort
- Array advanced selection sort
- Array Advanced Binary Search
- 5. Basic type wrapper class
- 6. Regular Expressions
- 7. Pattern and Matcher classes
- 8. Math class
- 9. Random class
- 10. System class
- 11. BigDecimal class
- 12. Date class
- 13.SimpleDateFormat类
- 14.Calendar class
2. String class
-
2.1 Some features of the String class
- The String class represents strings. All string literals (such as "abc" ) in Java programs are implemented as instances of this class.
- Strings are constants; their value cannot be changed after creation. String buffers support mutable strings. Because String objects are immutable, they can be shared.
- The Java language provides special support for the string concatenation symbol ("+") and for converting other objects to strings. String concatenation is achieved through the StringBuilder (or StringBuffer) class and its append method.
-
2.2 Once created it cannot be changed
- The difference between String s = new String("hello") and String s = "hello";
- The characteristics of String cannot be changed once created [content cannot be changed, reference can be changed]
-
Questions and Answers
A:String的特点: 一旦被创建就不能改变
B:案例演示
a:如何理解这句话
String s = "hello" ;
s = "world" + "java"; 问s的结果是多少?
下面这条语句一共创建了多少个对象:String s = “a”+“b”+”c”; 分别都是什么?
答案:5个对象
分别是 "a" , "b" , "c" , "ab" , "abc"
因为字符串的特点是一旦被创建就不能被改变,所有在使用常量进行相加的时候,都是在创建新的字符串对象
最后在把字符串"abc"这个常量值赋值给引用变量s
- Output result:
3. StringBuffer class
-
3.1 Overview of the StringBuffer class
- String buffer, StringBuffer is a container
- If we splicing strings, each splicing will build a new String object, which is time-consuming and wastes space. And StringBuffer can solve this problem
- Thread-safe variable character sequence, the efficiency corresponding to safety is relatively low
-
3.2 The difference between StringBuffer and String
- String is an immutable sequence of characters
- StringBuffer is a sequence of characters that can be
-
3.3 Mutual conversion between StringBuffer and String
-
A:String -- StringBuffer
- a: by constructor
- b: through the append() method
-
B:StringBuffer -- String
- a: use the substring method
- b: by constructor
- c: through the toString() method
4. Array
- 4.1 Schematic diagram of advanced bubble sorting of arrays
- The principle of bubble sort
- The adjacent elements are compared in pairs, the larger ones are placed later, the first time is completed, and the maximum value appears at the maximum index.
- bubble sort code
/**
* 冒泡排序
* @param arr
*/
private static void bubbleSort(int[] arr) {
for(int y = 0 ; y < arr.length - 1; y++) {
for(int x = 0 ; x < arr.length - 1 - y ; x++ ) {
if(arr[x] > arr[x+1]) {
int temp = arr[x] ;
arr[x] = arr[x+1] ;
arr[x+1] = temp ;
}
}
}
}
- 4.2 Schematic diagram of advanced selection sorting of arrays
- Selection sort principle
- Starting from the 0 index, compare with the following elements in turn, the small ones are placed forward, the first time is completed, the minimum value appears at the minimum index
- Array advanced selection sorting code implementation
- Selection sort principle
private static void selectSort(int[] arr) {
for(int index = 0 ; index < arr.length - 1 ; index++) {
for(int x = index + 1 ; x < arr.length ; x++) {
if(arr[index] > arr[x]) {
int temp = arr[index] ;
arr[index] = arr[x] ;
arr[x] = temp ;
}
}
}
}
- 4.3 Schematic diagram of advanced binary search of arrays
- Array advanced binary search idea
- Check the element in the middle every time, and you can reduce the element by half if it is larger or smaller.
- Array Advanced Binary Search Code
- Array advanced binary search idea
/**
* 二分查找
* @param arr
* @return
*/
private static int binarySearch2(int[] arr , int value) {
// 定义三个变量
int minIndex = 0 ;
int maxIndex = arr.length - 1 ;
while(minIndex <= maxIndex) {
int midIndex = (minIndex + maxIndex) >>> 1 ;
// 比较
if(arr[midIndex] == value) {
return midIndex ;
}else if(arr[midIndex] > value) {
maxIndex = midIndex - 1 ;
}else if(arr[midIndex] < value) {
minIndex = midIndex + 1 ;
}
}
// 返回
return -1;
}
5. Basic type wrapper class
-
5.1 Why are there basic type wrapper classes
- In order to perform more operations and more convenient operations on basic data types, Java provides corresponding class types for each basic data type.
- Common operations:
- One of the commonly used operations: for conversion between basic data types and strings
-
5.2 Correspondence between basic types and wrapper classes
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
6. Regular Expressions
- Regular expressions do not need to be memorized, just check them when you use them
- A single string used to describe or match a series of strings that conform to a syntactic rule. It's actually a rule. has its own special application.
- Specific use of direct online search
- For example, match mobile phone numbers, email addresses, ID numbers, etc.
7. Pattern and Matcher classes
* 正则的获取功能需要使用的类
8. Math class
* Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
* 成员变量
public static final double E : 自然底数
public static final double PI: 圆周率
* 成员方法
public static int abs(int a) 取绝对值
public static double ceil(double a) 向上取整
public static double floor(double a) 向下取整
public static int max(int a,int b) 获取最大值
public static int min(int a, int b) 获取最小值
public static double pow(double a,double b)获取a的b次幂
public static double random() 获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
public static int round(float a) 四舍五入
public static double sqrt(double a) 获取正平方根
9. Random class
- random class
- This class is used to generate random numbers If two instances of Random are created with the same seed, each with the same sequence of method calls, they will generate and return the same sequence of numbers
- Construction method
public Random() 没有给定种子,使用的是默认的(当前系统的毫秒值)
public Random(long seed) 给定一个long类型的种子,给定以后每一次生成的随机数是相同的
public int nextInt()
public int nextInt(int n)
10. System class
- system class
- The System class contains some useful class fields and methods. It cannot be instantiated.
- system level operations