深入了解Java核心类库??Objects类( 三 )


public static void main(String[] args) {Person p = new Person(1234,"张三");Person e = new Person(2345,"李四");System.out.println("e.getClass():"+e.getClass());System.out.println("e.hashCode():"+e.hashCode());System.out.println("e.toString():"+e.toString());}

输出结果:
e.getClass():class com.company.demo.Person
e.hashCode():114132791
e.toString():com.company.demo.Person@6cd8737
  • 建议重写Object中的toString方法

2.3 Object源码package java.lang;import jdk.internal.HotSpotIntrinsicCandidate;public class Object {private static native void registerNatives();static {registerNatives();}@HotSpotIntrinsicCandidatepublic Object() {}@HotSpotIntrinsicCandidatepublic final native Class<?> getClass();@HotSpotIntrinsicCandidatepublic native int hashCode();public boolean equals(Object obj) {return (this == obj);}@HotSpotIntrinsicCandidateprotected native Object clone() throws CloneNotSupportedException;public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());}@HotSpotIntrinsicCandidatepublic final native void notify();@HotSpotIntrinsicCandidatepublic final native void notifyAll();public final void wait() throws InterruptedException {wait(0L);}public final native void wait(long timeoutMillis) throws InterruptedException;public final void wait(long timeoutMillis, int nanos) throws InterruptedException {if (timeoutMillis < 0) {throw new IllegalArgumentException("timeoutMillis value is negative");}if (nanos < 0 || nanos > 999999) {throw new IllegalArgumentException("nanosecond timeout value out of range");}if (nanos > 0) {timeoutMillis++;}wait(timeoutMillis);}protected void finalize() throws Throwable { }}
总结【深入了解Java核心类库??Objects类】本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注趣讯吧的更多内容!