day08常用类的概念

常用类

1 Object

所有类的父类,超类

1,1 构造方法

Object object =new Object();

1,2 方法

equals方法
Object中的equals方法比较是两个对象的内存地址是否相同

1,2,1 ** equals和==的区别

== :可以判断基本数据类型和引用类型
equals方法:只能判断引用类型
1 = =判断的是基本数据类型的值是否相同,和引用类型是否是同一个引用,既内存地址是否相同
2 equals方法,如果没有被重写,既和= = 的比较是相同的,都是内存地址的比较
如果被重写了,比如String就重写了equals方法,则此时equals的比较结果和重写定义的逻辑是相同的

package com.object;
public class Student {
String name;
int score;
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
@Override
public boolean equals(Object obj) {
String name1 = this.name;
Student student = (Student) obj;
String name2 = student.name;
// if (name1.equals(name2)) {
// return true;
// } else {
// return false;
// }
return name1.equals(name2) && this.score == student.score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
Student stu1 = new Student("张三", 71);
Student stu2 = new Student("张三", 70);
System.out.println(stu1.equals(stu2));//false

finalize() 对象被垃圾回收时调用的方法

package com.object;
public class ObjectTest {
public static void main(String[] args) {
for (int i = 0; i < 1000000; i++) {
Demo demo = new Demo();
}
}
}
class Demo {
@Override
protected void finalize() throws Throwable {
System.out.println("被回收了");
}
}

stu1.hashCode():对象的内存地址。哈希值,int类型
toString() 返回该对象的字符串表示 默认对象的描述字符串是哈希值(类的全类名@哈希值)
–》即直接打印对象,相当于是调用对象的toString()方法
–》可以对toString方法进行重写,返回一个字符串

public class Student {
String name;
int score;
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
Student stu1 = new Student("张三", 71);
Student stu2 = new Student("张三", 70);
System.out.println(stu1.equals(stu2));
System.out.println(stu1);
System.out.println(stu1.toString());

Scanner

Scanner in = new Scanner(System.in);
// 字符串
String name = in.next();
// String id = in.nextLine();
System.out.println(name);
// int age = in.nextInt();
// double he = in.nextDouble();
// float we = in.nextFloat();
// long l = in.nextLong();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值