常用类

1.Object类

1.1 clone方法

clone方法:clone方法执行的是浅拷贝

注:clone方法创建了一个新对象,但这个新对象和原始对象的引用变量相同(浅拷贝)。

验证clone方法是浅拷贝的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Main {
// 声明静态内部类 Body,实现 Cloneable 接口以支持克隆
static class Body implements Cloneable {
public Head head;

public Body() {}

public Body(Head head) {
this.head = head;
}

@Override
protected Object clone() throws CloneNotSupportedException {
// 调用父类的 clone 方法以执行浅拷贝
return super.clone();
}
}

// 声明静态内部类 Head,这个类不需要克隆功能
static class Head {
public Head() {}
}

public static void main(String[] args) throws CloneNotSupportedException {
// 创建一个 Body 对象,并将其关联的 Head 对象初始化
Body body = new Body(new Head());

// 使用克隆方法复制 Body 对象,得到一个新的 Body 对象 body1
Body body1 = (Body) body.clone();

// 检查两个对象是否相同
System.out.println("body == body1 : " + (body == body1));

// 检查两个对象的 head 属性是否相同(因为是浅拷贝,所以 head 属性相同)
System.out.println("body.head == body1.head : " + (body.head == body1.head));
}
}

out:
body == body1 : false
body.head == body1.head : true//新对象和原对象共享相同的引用对象,故clone方法执行的是浅拷贝

1.2 toString()方法

Object 类的 toString 方法返回一个字符串,该字符串由类名、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。

注:通常情况下,应重写 toString() 方法,以便返回一个更有意义的、描述对象状态的字符串。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}

public class ToStringExample {
public static void main(String[] args) {
Person person = new Person("Alice", 30);

// 使用 toString() 方法输出对象的字符串表示形式
System.out.println(person.toString()); // 输出:Person{name='Alice', age=30}
}
}

out:
Person{name='Alice', age=30}

1.3 getClass()方法

getClass()方法:返回对象的运行时类,所有 Java 类都继承了这个方法,不可重写,要调用的话,一般和getName()联合使用,如getClass().getName()。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Vehicle {
// ...
}

class Car extends Vehicle {
// ...
}
public class GetClassExample {
public static void main(String[] args) {
Vehicle vehicle = new Car();
// 使用 getClass() 方法获取对象的运行时类
Class<?> cls = vehicle.getClass();

// 输出类的名称
System.out.println("Class name: " + cls.getName()); // 输出:Class name: Car
}
}

out:
Class name: Car

1.4 equals()方法

Object中的equals方法是直接判断this和obj本身的值是否相等,如果this和obj指向的是同一块内存对象,则返回true,如果

this和obj指向的不是同一块内存,则返回false。

注:即便是内容完全相等的两块不同的内存对象,也返回false。

String类已经重写了object中的equals方法(比较内容是否相等)

1
2
3
4
5
6
7
8
9
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println(str1.equals(str2)); // 输出:true,内容相同
System.out.println(str1.equals(str3)); // 输出:true,内容相同

out:
true
true

1.5 wait()方法

pass

1.6 notify()方法

pass

2.包装类

基本类型的数据不具备“对象”的特性——不携带属性、没有方法可调用,故Java为每种基本数据类型分别设计了对应的类,称之为包装类

1

自动拆箱和装箱

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
int m = 500; // 创建一个基本数据类型变量 m 并赋值为 500
Integer obj = m; // 自动装箱:将基本数据类型 m 装箱为 Integer 对象 obj
int n = obj; // 自动拆箱:将 Integer 对象 obj 拆箱为基本数据类型 n
System.out.println("n = " + n); // 输出 n 的值

Integer obj1 = 500; // 创建一个 Integer 对象 obj1 并初始化为 500
System.out.println("obj 等价于 obj1?" + obj.equals(obj1)); // 比较 obj 和 obj1 是否相等
}

out:
n = 500
obj 等价于 obj1?true

注:所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。

3.String类

3.1 创建字符串对象方式

String类对象一旦声明则不可以改变,而改变的只是地址,原来的字符串还是存在的,并且产生垃圾。

直接赋值方式:创建对象是在方法区的常量池

String str="hello";//直接赋值的方式

构造方法:通过构造方法创建字符串对象是在堆内存

String str=new String("hello");//实例化的方式

两种实例化方式的比较:

1
2
3
4
5
6
7
8
9
10
String str1 = "Lance";
String str2 = new String("Lance");
String str3 = str2;
String str4 = "Lance";
//String类对象==比较,比较的是地址,而不是内容
System.out.println(str1==str2);//false
System.out.println(str1==str3);//false
System.out.println(str3==str2);//true
System.out.println(str1==str4);//true
//str1和str4引用的是字符串常量池中的同一个对象,因此它们的引用相等

3.2 字符串常量池

​ 在字符串中,如果采用直接赋值的方式(String str=”Lance”)进行对象的实例化,则会将匿名对象“Lance”放入对象池,每当下一次对不同的对象进行直接赋值的时候会直接利用池中原有的匿名对象

两种实例化方式的区别:

直接赋值(String str = “hello”):只开辟一块堆内存空间,并且会自动入池,不会产生垃圾。

构造方法(String str= new String(“hello”)):连续两次new一个String对象,会开辟两块堆内存空间,其中一块堆内存会变成垃圾被系统回收,而且不能够自动入池,需要通过public String intern();方法进行手工入池。

注:在开发的过程中不会采用构造方法进行字符串的实例化

3.3 空指向异常

equals比较的是字符串内容,在开发的过程中,equals()通过接受参数,可以避免空指向。

1
2
3
4
5
6
7
8
9
 String str = null;
if(str.equals("hello"))
{
//此时会出现空指向异常 ...
}
if("hello".equals(str))
{
//此时equals会处理null值,可以避免空指向异常 ...
}

4.StringBuilder 和 StringBuffer

pass

5.File类

pass