Java疑点

Java只有值传递

// m变量的值:指向的堆对象的地址
public static void main(String[] args) {
    
    
    Integer m = 10;
    Integer n = 20;
    swap(m,n);
    System.out.println("m = " + m + ",n = " + n);
}
// Java只有值传递,对引用数据类型,值就是指向的堆对象的地址
// a和b交换了指向的堆对象,不影响m和n
static void swap(Integer a,Integer b){
    
    
    int temp = a;
    a = b;
    b = temp;
}

关于hashcode

对象的hashcode值并不是对象的地址

创建完对象之后,对象是没有hashcode的
第一次调用hashcode方法时,会产生hashcode值,hashcode值会存在对象的markwod里面
以后再调用hashcode方法得到的是同样的值

重写了Object的hashcode方法则不遵循上述,即不会存到对象的markword中

在这里插入图片描述

IDEA中debug时,这里@后面的数字并不是hashcode值.

That is objectId reported by the JVM, for details please see the JDWP specification.

Uniquely identifies an object in the target VM.
A particular object will be identified by exactly one objectID in JDWP commands and replies throughout its
lifetime (or until the objectID is explicitly disposed).
An ObjectID is not reused to identify a different object unless it has been explicitly disposed, regardless of whether
the referenced object has been garbage collected.
An objectID of 0 represents a null object.
Note that the existence of an object ID does not prevent the garbage collection of the object.
Any attempt to access a a garbage collected object with its object ID will result in the INVALID_OBJECT error code.
Garbage collection can be disabled with the DisableCollection command, but it is not usually necessary to do so.

stackoverflow
Debug时Object@xxx表示什么

猜你喜欢

转载自blog.csdn.net/qq_53318060/article/details/130900500