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

后端 未结 9 1636
南笙
南笙 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条回答
  •  情书的邮戳
    2020-11-28 16:17

    A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.

    Java doesn't have pointers as such (unlike, say, C# which has references and pointers - the latter being used in "unsafe" code).

提交回复
热议问题