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

后端 未结 9 1644
南笙
南笙 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:10

    As Java does not support pointers. It may appear that references are special kind of pointers. But we must note the key difference: with a pointer, we can point any address (which is actually a number slot in a memory). So, it is quite possible that with a pointer, we can point an invalid address also and then we may face surprises issues during runtime. But reference types will always point to valid addresses or they will point to null.

提交回复
热议问题