先执行gc方法,10S后打印数组的地址,发现没变,是否是说明复制算法不改变对象的内存地址 java
toString打印的值 现有的匿名回答是正解:题主作的实验根本没有涉及对象地址。java.lang.Object默认的toString()实现,返回的是“类名@hashcode”这样的格式的字符串。其中hashcode部分的值默认返回的是对象的“身份哈希值”(identity hash code)。jdk8u/jdk8u/jdk: 3dc438e0c8e1 src/share/classes/java/lang/Object.java算法
做者:RednaxelaFX 连接:https://www.zhihu.com/question/49631727/answer/120113928 来源:知乎 著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。 /** * Returns a string representation of the object. In general, the * {@code toString} method returns a string that * "textually represents" this object. The result should * be a concise but informative representation that is easy for a * person to read. * It is recommended that all subclasses override this method. * <p> * The {@code toString} method for class {@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{@code @}', and * the unsigned hexadecimal representation of the hash code of the * object. In other words, this method returns a string equal to the * value of: * <blockquote> * <pre> * getClass().getName() + '@' + Integer.toHexString(hashCode()) * </pre></blockquote> * * @return a string representation of the object. */ public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
jdk8u/jdk8u/jdk: 3dc438e0c8e1 src/share/classes/java/lang/System.java数组
/** * Returns the same hash code for the given object as * would be returned by the default method hashCode(), * whether or not the given object's class overrides * hashCode(). * The hash code for the null reference is zero. * * @param x object for which the hashCode is to be calculated * @return the hashCode * @since JDK1.1 */ public static native int identityHashCode(Object x);
hashcode:对象的初始地址的整数表示ide
Java中的对象是JVM在管理,JVM会在她认为合适的时候对对象进行移动,好比,在某些须要整理内存碎片的GC算法下发生的GC。此时,对象的地址会变更,但hashcode不会改变。性能
1.hashCode是为了提升在散列结构存储中查找的效率,在线性表中没有做用。 2.通常一个类的对象若是会存储在HashTable,HashSet,HashMap等散列存储结构中,那么重写equals后最好也重写hashCode,不然会致使存储数据的不惟一性(存储了两个equals相等的数据)。而若是肯定不会存储在这些散列结构中,则能够不重写hashCode。 3.若两个对象equals返回true,则hashCode有必要也返回相同的int数。 4.若两个对象equals返回false,则hashCode不必定返回不一样的int数,但为不相等的对象生成不一样hashCode值能够提升哈希表的性能。 5.若两个对象hashCode返回相同int数,则equals不必定返回true。 6.若两个对象hashCode返回不一样int数,则equals必定返回false。 7.同一对象在执行期间若已经存储在集合中,则不能修改影响hashCode值的相关信息,不然会致使内存泄露问题。 8.通常来讲涉及到对象之间的比较大小就须要重写equals方法。
内存地址改变,hashcode不会改变this
hashCode 的常规协定是: 在 Java 应用程序执行期间,在同一对象上屡次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另外一次执行,该整数无需保持一致。 若是根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每一个对象上调用 hashCode 方法都必须生成相同的整数结果。.net