JAVA常用类(一)
文章目录
一. Object类
-
超类、基类,所有类的直接或间接父类,位于继承树的最顶层。
-
任何类,如没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承。
-
Object类中所定义的方法,是所有对象都具备的方法。
-
Object类型可以存储任何对象。
- 作为参数,可接受任何对象。
- 作为返回值,可返回任何对象。
1. getclass()方法
public final Class <?> getClass(){}
- 返回引用中存储的实际对象类型。
- 应用:通常用于判断两个引用中实际存储对象类型是否一致。
实例:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Application {
public static void main(String[] args) {
Student student1 = new Student("aaa",20);
Student student2 = new Student("bbb",18);
//getclass方法
Class class1 = student1.getClass();
Class class2 = student2.getClass();
if (class1==class2){
System.out.println("s1和s2属于同一个类型");
}else{
System.out.println("s1和s2不属于同一个类型");
}
}
}
-----------------结果----------------------
s1和s2属于同一个类型
2. hashcode()方法
public int hashCode(){}
- 返回该对象的哈希码值。
- 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值。
- 一般情况下相同对象返回相同哈希码。
实例:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Application {
public static void main(String[] args) {
Student student1 = new Student("aaa",20);
Student student2 = new Student("bbb",18);
//hashcode方法
System.out.println(student1.hashCode());
System.out.println(student2.hashCode());
Student student3=student1;
System.out.println(student3.hashCode());
}
}
-----------------结果----------------------
856419764
621009875
856419764
3. toString()方法
public String toString() {}
- 返回该对象的字符串表示(表现形式)。
- 可以根据程序需求覆盖该方法,如:展示对象各个属性值。
实例:
//未重写tostring方法
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
//重写tostring方法后
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name+","+age;
}
}
//主程序
public class Application {
public static void main(String[] args) {
Student student1 = new Student("aaa",20);
Student student2 = new Student("bbb",18);
//toString方法
System.out.println("————————hashCode————————");
System.out.println(student1.hashCode());
System.out.println(student2.hashCode());
System.out.println("————————toString————————");
System.out.println(student1.toString());
System.out.println(student2.toString());
}
}
----------------未重写结果---------------
————————hashCode————————
856419764
621009875
————————toString————————
com.mnm.obj.Student@330bedb4 //330bedb4(十六进制)==856419764(十进制)
com.mnm.obj.Student@2503dbd3
----------------重写后结果---------------
————————hashCode————————
856419764
621009875
————————toString————————
aaa,20
bbb,18
4. equals()方法
public boolean equals (Object obj) {}
- 默认实现为(this == obj), 比较两个对象地址是否相同。
- 可进行覆盖,比较两个对象的内容是否相同。
-
未重写
实例:
public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } }
public class Application { public static void main(String[] args) { Student student1 = new Student("aaa",20); Student student2 = new Student("bbb",18); Student student3 = student1; Student student4 = new Student("aaa",20); System.out.print("student1和student2相比:\t"); System.out.println(student1.equals(student2)); System.out.print("student1和student3相比:\t"); System.out.println(student1.equals(student3)); System.out.print("student1和student4相比:\t"); System.out.println(student1.equals(student4)); } } ----------------结果------------------- student1和student2相比: false student1和student3相比: true student1和student4相比: false
-
重写后
equals()方法重写步骤:
- 比较两个引用是否指向同一个对象。
- 判断obj是否为null。
- 判断两个引用指向的实际对象类型是否一致。
- 强制类型转换。
- 依次比较各个属性值是否相同。
实例:
public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //重写equals()方法 @Override public boolean equals(Object obj) { //1,比较两个引用是否指向同一个对象 if(this==obj){ return true; } //2.判断obj是否为null if(obj==null){ return false; } //3.判断两个引用指向的实际对象类型是否一致。 if(obj instanceof Student){ //4.强制类型转换 Student s = (Student) obj; //5.比较属性 if(this.name.equals(s.getName())&&this.age==s.getAge()){ return true; } } return false; } }
public class Application { public static void main(String[] args) { Student student1 = new Student("aaa",20); Student student2 = new Student("bbb",18); Student student3 = student1; Student student4 = new Student("aaa", 20); System.out.print("student1和student2相比:\t"); System.out.println(student1.equals(student2)); System.out.print("student1和student3相比:\t"); System.out.println(student1.equals(student3)); System.out.print("student1和student4相比:\t"); System.out.println(student1.equals(student4)); } } ----------------结果------------------- student1和student2相比: false student1和student3相比: true student1和student4相比: true
5. finalize()方法
- 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列。
- 垃圾对象:没有有效引用指向此对象时,为垃圾对象。
- 垃圾回收:由GC销毁垃圾对象,释放数据存储空间。
- 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象。
- 手动回收机制:使用System.gc();通知JVM执行垃圾回收。
实例:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//重写finalize()方法
@Override
protected void finalize() throws Throwable {
System.out.println(this.name+"对象被回收");
}
}
public class TeseStudent {
public static void main(String[] args) {
Student s1 = new Student("aaa", 20);
Student s2 = new Student("bbb", 19);
Student s3 = new Student("ccc", 18);
//回收垃圾
System.out.println("回收垃圾");
System.gc();
}
}
----------------结果-------------------
回收垃圾
这里可以看到垃圾并没有被回收,说明三个类都有用。
修改后
public class TeseStudent {
public static void main(String[] args) {
new Student("aaa", 20);
new Student("bbb", 19);
new Student("ccc", 18);
//回收垃圾
System.out.println("回收垃圾");
System.gc();
}
}
----------------结果-------------------
回收垃圾
ccc对象被回收
bbb对象被回收
aaa对象被回收
二. 包装类
- 基本数据类型所对应的引用数据类型。
- Object可统一所有数据,包装类的默认值是null。
1. 包装类对应
基本数据类型 | 包装类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
2. 类型转换与装箱/拆箱
- 8种包装类提供不同类型间的转换方式:
Number父类中提供的6个共性方法。
修饰符和类型 | 方法和说明 |
---|---|
byte | byteValue() 返回指定号码作为值 byte ,这可能涉及舍入或截断。 |
abstract double | doubleValue() 返回指定数字的值为 double ,可能涉及四舍五入。 |
abstract float | floatValue() 返回指定数字的值为 float ,可能涉及四舍五入。 |
abstract int | intValue() 返回指定号码作为值 int ,这可能涉及舍入或截断。 |
abstract long | longValue() 返回指定数字的值为 long ,可能涉及四舍五入或截断。 |
short | shortValue() 返回指定号码作为值 short ,这可能涉及舍入或截断。 |
以Integer为实例的装箱/拆箱:
public class Application {
public static void main(String[] args) {
//类型转换:装箱,基本类型转成引用类型的过程
//基本类型
int num1 = 18;
//使用Integer类创建对象
Integer integer1 = new Integer(num1);
Integer integer2 =Integer.valueOf(num1);
System.out.println("装箱"+integer1);
System.out.println("装箱"+integer2);
//类型转换:拆包,引用类型转成基本类型
int int1=integer1.intValue();
System.out.println("拆箱"+int1);
//JDK1.5之后,提供了自动装箱和拆箱
int age=30;
//自动装箱
Integer integer3=age;
System.out.println("自动装箱"+integer3);
//自动拆箱
int num2=integer3;
System.out.println("自动拆箱"+num2);
}
}
-------------------结果---------------------
装箱18
装箱18
拆箱18
自动装箱30
自动拆箱30
实例的class文件:
public class Application {
public Application() {
}
public static void main(String[] args) {
int num1 = 18;
Integer integer1 = new Integer(num1);
Integer integer2 = Integer.valueOf(num1);
System.out.println("装箱" + integer1);
System.out.println("装箱" + integer2);
int int1 = integer1;
System.out.println("拆箱" + int1);
int age = 30;
Integer integer3 = Integer.valueOf(age);
System.out.println("自动装箱" + integer3);
int num2 = integer3;
System.out.println("自动拆箱" + num2);
}
}
自动装箱的原理:编译器自动帮你调用方法
parseXXX()静态方法。
可以实现字符串和基本类型的转换
实例:
public class Application {
public static void main(String[] args) {
//基本类型和字符串之间转换
//1基本类型转字符串
int num1=10;
//1.1 使用+号
String s1=num1+"";
//1.2 使用Integer中的基本方法
String s2=Integer.toString(num1);
String s3=Integer.toString(num1,16);//重载方法
System.out.println("基本类型转字符串");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
//2字符串转基本类型
String s4="110";
int num2 = Integer.parseInt(s4);
System.out.println("字符串转基本类型");
System.out.println(num2);
//boolean字符串形式转成基本类型,"true"--->true 非"true"---->false
String s5="true";
String s6="tttt";
Boolean b1 = Boolean.parseBoolean(s5);
Boolean b2 = Boolean.parseBoolean(s6);
System.out.println("boolean字符串形式转成基本类型");
System.out.println(b1);
System.out.println(b2);
}
}
-------------结果-----------------
基本类型转字符串
10
10
a
字符串转基本类型
110
boolean字符串形式转成基本类型
true
false
注意:需保证类型兼容,否则抛出NumberFormatException异常。
3. 整数缓冲区
- Java预先创建了256个常用的整数包装类型对象。
- 在实际应用当中,对已创建的对象进行复用。
面试题:
public class Application {
public static void main(String[] args) {
//面试题
Integer integer1 = new Integer(100);
Integer integer2 = new Integer(100);
System.out.println(integer1==integer2);
//自动装箱
int num1=100;
Integer integer3=num1;
Integer integer4=num1;
System.out.println(integer3==integer4);
//自动装箱
int num2=200;
Integer integer5=num2;
Integer integer6=num2;
System.out.println(integer5==integer6);
}
}
------------结果--------------
false
true
false
代码段讲解1:
Integer integer1 = new Integer(100);
Integer integer2 = new Integer(100);
System.out.println(integer1==integer2);
------------结果--------------
false
两个对象指向堆中的两个引用,所以两个对象不相同
代码段讲解2:
int num1=100;
Integer integer3=num1;
Integer integer4=num1;
System.out.println(integer3==integer4);
------------结果--------------
true
原因:
编译器自动调动Integer.valueOf();方法
class文件代码段如下:
int num1 = 100;
Integer integer3 = Integer.valueOf(num1);
Integer integer4 = Integer.valueOf(num1);
System.out.println(integer3 == integer4);
Integer.valueOf()源码如下:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可以看到ValueOf定义了一个从[-128,127]的数组,当小于数组时从数组调用,大于数组的时候新建引用
代码段讲解3:
int num2=200;
Integer integer5=num2;
Integer integer6=num2;
System.out.println(integer5==integer6);
------------结果--------------
false
class文件代码段如下:
int num2 = 200;
Integer integer5 = Integer.valueOf(num2);
Integer integer6 = Integer.valueOf(num2);
System.out.println(integer5 == integer6);
num2的大小超出了cache[]数组的大小,新建了两个引用