Java 中一切皆对象,对象即 Object:除了8种基本类型「byte、short、int、float、double、long、char、boolean」
那么作为一个 Javaer ,你有对象吗?
/**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*
* @author unascribed
* @see java.lang.Class
* @since JDK1.0
*/
public class Object {
Object 类是类层次结构的根
是 Java 中所有类的父类
所有方法
在 IDEA 中进入一个类,按 Ctrl + F12,即可展示此类的全部方法:
分析比较常用的方法
我写我能所写的,所以有些方法我自己暂时用的很少的,就没有总结上来,不过我会后续添加
– 2019/12/4
toString()
这个可是超级常用的了:
Object 中 toString()方法
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
package testAll.ThinkingInJava;
public class TestObject {
public static void main(String[] args) {
User user = new User("NCharming", 23);
System.out.println(user);
}
}
自己写了一个测试类 User,没有复写 toString() 方法,打印如下:
testAll.ThinkingInJava.User@eed1f14
这就是我们刚开始写的时候,很容易忘记去复写 toString() 方法,让后又想打印查看结果,这就导致直接调用 Object 的 toString() 方法。(这就是开头我们说的,Object 类是所有类的父类,自己创建的类也不例外哦)
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
现在在 User 类中复写 toString() 方法(IDEA 中 alt + insert 快捷键可以生成呢)
User{name='NCharming', age=23}
你看,这就很清晰了。
hashCode()
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code hashCode} method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hash tables.
* </ul>
public native int hashCode();
- 对同一个对象不管取多少次 hashCode 都应该返回同一个值(integer)
- 如果两个对象使用 equals() 返回 true,那么这两个对象的 hashCode 一定相同
- 如果两个对象使用 equals() 返回 false,那么这两个对象的 hashCode 也可以相同
equals()
这可也超级常用呢,还经常和 == 来做比较哦;
这里我先总结出 == 与 equals() 的区别:
==:比较地址,所以如果没有重写 equals() 可是直接比较地址的哦
equals():比较内容
对于基本类型「都不是对象」,== 当然是比较内容的啊
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
public boolean equals(Object obj) {
return (this == obj);
}
假设 x,y,z 都不为 null:
- 自反性:x.equals(x) is true;
- 对称性:x.equals(y) is true 当且仅当 y.equals(x) is true;
- 传递性:假如,x.equals(y) is true;y.equals(z) is true;x.equals(z) is also true;
- 一致性:假如,x.equals(y) is true,那么再次调用应该也为 true;
- x.equals(null) is always false;
note:
一般情况下都是很有必要在重写 equals 的同时重写 hashCode ;且都是用内容去进行 hashCode 值的计算
这是用 IDEA 生成的 User 类下的 equals 和 hashCode 的重写
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age && name.equals(user.name);
}
// equals 重写,如果是自己重写,需要注意:
public boolean equals(Object o) :Object 不能换成自己当前所写的 User 类:这只是一个重载,而非重写
@Override
public int hashCode() {
return Objects.hash(name, age);
//求此 hashCode 是用内容(name,age)去求的,所以只要内容相同,那么 hashCode 就必然相同
}
– 2019/12/4
欢迎刚好读到这篇文章的你提个建议!