What is the difference between a pointer and a reference variable in Java?

后端 未结 9 1629
南笙
南笙 2020-11-28 15:42

My Java book explains that to use objects, we can assign them to reference variables. How is that different from a pointer to an object? Does Java have pointers?

T

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 16:07

    In terms of Java syntax - it's true, that Java doesn't have pointers, but to clarify situation - Java Runtime has pointers. Every time GC occurs, there is a big chance, that your object stored in the heap will physically change it's memory address. This is achievable only by using what is really called Pointer. As you come from C++ world - you know reference restrictions. But it doesn't mean that JVM doesn't use references =).

    For further information take a look at Ordinary Object Pointer

    So, as you have brief understanding of core JVM memory management, you can consider Java Reference term as Pointer at runtime.

    No, there is no pass by reference at all. Only pass by value, since the real Java method argument is a copy of Java Reference, therefore - another Pointer.

    So, in syntax usage way - Java Reference is closer to C++ Reference, but in real Runtime world it's close to Pointer.

    P.s. for more clarification it's better to read more articles about Ordinary Object Pointer or take a loot at oop.hpp.

提交回复
热议问题