Skip to content
基础

一句话答案

核心方法:equals()(相等判断)、hashCode()(哈希值)、toString()(字符串表示)、clone()(浅拷贝)、wait/notify(线程通信)。

核心要点

关键方法:

  • equals():默认比较引用,重写比较内容
  • hashCode():重写equals必须重写hashCode
  • toString():打印对象信息
  • clone():浅拷贝(需实现Cloneable)
  • wait()/notify():线程间通信
  • getClass():获取运行时类型
追问与易错

追问方向:

  • clone() 是深拷贝还是浅拷贝?(默认浅拷贝,需实现 Cloneable)
  • finalize() 为什么不推荐用?(不确定执行时机/影响 GC 性能/JDK9 已废弃)
  • wait() 为什么必须在 synchronized 中?(防止 lost wake-up)

易错点:

  • ❌ "== 和 equals 对所有对象行为一样"——Object 默认 equals 就是 ==,需要重写
  • ❌ 忘记 getClass() 和 instanceof 的区别——getClass 精确匹配,instanceof 包含子类

💡 记忆锚点

Object六件套:equals/hashCode成对改、toString看信息、clone浅拷贝、wait/notify线程对话、getClass查身份证。口诀:等哈串克等通类。