How can a single reference variable access to all object fields?

后端 未结 4 1762
一生所求
一生所求 2021-01-13 17:22
Animal myAnimal = new Animal();

I have this code above. As far as I know, it will do these things :

  1. An animal object will be created
4条回答
  •  一整个雨季
    2021-01-13 17:55

    Addresses are just long numbers to denote the memory location of your object. And they depends on the bit's of the machine. It is 64 bit long on 64 bit machine and 32 bit long on 32 bit machine.

    How does the memory address value look like?

    If you are really enthusiastic to see those big numbers in your console, you can actually explore the Unsafe API

    public native long getAddress(long address)
    

    Fetches a native pointer from a given memory address. If the address is zero, or does not point into a block obtained from #allocateMemory , the results are undefined.

    If the native pointer is less than 64 bits wide, it is extended as an unsigned number to a Java long. The pointer may be indexed by any given byte offset, simply by adding that offset (as a simple integer) to the long representing the pointer. The number of bytes actually read from the target address maybe determined by consulting #addressSize .


    Is that one address or numerous address value? If only one, how can myAnimal have accesses to all object fields of Animal object like myAnimal.name, myAnimal.height,...?

    Well it should be one on top level and that address location may contain address locations of others (I am not really sure)

    I have not really tried running this on my machine.

提交回复
热议问题