常用类
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 { static class Body implements Cloneable { public Head head;
public Body() {}
public Body(Head head) { this.head = head; }
@Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
static class Head { public Head() {} }
public static void main(String[] args) throws CloneNotSupportedException { Body body = new Body(new Head());
Body body1 = (Body) body.clone();
System.out.println("body == body1 : " + (body == body1));
System.out.println("body.head == body1.head : " + (body.head == body1.head)); } }
out: body == body1 : false body.head == body1.head : true
|
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);
System.out.println(person.toString()); } }
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(); Class<?> cls = vehicle.getClass();
System.out.println("Class name: " + cls.getName()); } }
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)); System.out.println(str1.equals(str3));
out: true true
|
1.5 wait()方法
pass
1.6 notify()方法
pass
2.包装类
基本类型的数据不具备“对象”的特性——不携带属性、没有方法可调用,故Java为每种基本数据类型分别设计了对应的类,称之为包装类

自动拆箱和装箱
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) { int m = 500; Integer obj = m; int n = obj; System.out.println("n = " + n);
Integer obj1 = 500; System.out.println("obj 等价于 obj1?" + obj.equals(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";
System.out.println(str1==str2); System.out.println(str1==str3); System.out.println(str3==str2); System.out.println(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)) { }
|
4.StringBuilder 和 StringBuffer
pass
5.File类
pass