指针如何与 Java 中的原始类型一起使用?
How do pointers work with primitive types in Java?
我正在阅读What is a NullPointerException, and how do I fix it?,在接受的答案中,我阅读了一些我不太理解的内容:
int x;
x = 10;
In this example the variable x is an int and Java will initialize it
to 0 for you. When you assign it to 10 in the second line your value
10 is written into the memory location pointed to by x.
我想对于原始类型,变量是实际值的内存地址;至于复杂类型,变量只是指向实际值的指针的内存地址。但是上面引用的答案告诉我我错了。它说 "the memory location pointed to by x."
那么如果 x 指向存储实际值的内存地址,原始类型与复杂类型有何不同?我什至不知道原始类型有指针。指针如何与原始类型一起工作?
基本类型和复杂类型的主要区别在于数据的存储方式。您实际上是在查看原始类型和 class 类型
之间的区别
1. Every variable is stored as a location in the computer memory.
The above statement applies to both primitive types and also class
types.
The differences:
2. For a primitive type: the value of the variable is stored in the memory location assigned to the variable.
That means if we assigned int x = 10
, the value of x
is stored in
where the value of 10
is stored, i.e the memory location. That means
when we "look" at x, '10' is stored there. Maybe it would help to
think of it more like an "assignment" where you command that x be
equal to 10.
3. For a class type: It only stores the memory address of the object that stores the value. It does not directly hold the
object itself.
Integer x = 10 will have a memory address that points to object of
type int, which will then hold the value of 10. This is known as a
reference. Think of it as directory that tells you to go to which shelf to actually retrieve the value.
还有
Class 类型也称为引用类型或对象类型,也就是说它们都表示 class 的对象(无论是 Integer class 还是 MyPerson class).
基本类型不是引用类型,因为它们不保存引用(内存地址)。
这种区别是日常使用 "wrapper classes" 的原因,Integer
等类型被视为 class 到 int
的包装器,以允许数据操作,例如将整数存储在 ArrayList 等数据结构中。因为 ints
是原始数据类型,不是 object
,而 Integer
是。由于基本类型不是对象,我们必须将它们放入class以便我们将它们添加到列表、字典等。这样我们就有了一个对象列表(指向原始类型)但它们本身并不是裸原始数据类型。参见 this SO question for further info
有关原始类型和非原始类型(又名 Class/reference/object 类型)之间差异的额外阅读内容详尽 here。他们也有一个很好的图表来说明它。
我正在阅读What is a NullPointerException, and how do I fix it?,在接受的答案中,我阅读了一些我不太理解的内容:
int x; x = 10;
In this example the variable x is an int and Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x.
我想对于原始类型,变量是实际值的内存地址;至于复杂类型,变量只是指向实际值的指针的内存地址。但是上面引用的答案告诉我我错了。它说 "the memory location pointed to by x."
那么如果 x 指向存储实际值的内存地址,原始类型与复杂类型有何不同?我什至不知道原始类型有指针。指针如何与原始类型一起工作?
基本类型和复杂类型的主要区别在于数据的存储方式。您实际上是在查看原始类型和 class 类型
之间的区别1. Every variable is stored as a location in the computer memory.
The above statement applies to both primitive types and also class types.
The differences:
2. For a primitive type: the value of the variable is stored in the memory location assigned to the variable.
That means if we assigned
int x = 10
, the value ofx
is stored in where the value of10
is stored, i.e the memory location. That means when we "look" at x, '10' is stored there. Maybe it would help to think of it more like an "assignment" where you command that x be equal to 10.3. For a class type: It only stores the memory address of the object that stores the value. It does not directly hold the object itself.
Integer x = 10 will have a memory address that points to object of type int, which will then hold the value of 10. This is known as a reference. Think of it as directory that tells you to go to which shelf to actually retrieve the value.
还有
Class 类型也称为引用类型或对象类型,也就是说它们都表示 class 的对象(无论是 Integer class 还是 MyPerson class).
基本类型不是引用类型,因为它们不保存引用(内存地址)。
这种区别是日常使用 "wrapper classes" 的原因,Integer
等类型被视为 class 到 int
的包装器,以允许数据操作,例如将整数存储在 ArrayList 等数据结构中。因为 ints
是原始数据类型,不是 object
,而 Integer
是。由于基本类型不是对象,我们必须将它们放入class以便我们将它们添加到列表、字典等。这样我们就有了一个对象列表(指向原始类型)但它们本身并不是裸原始数据类型。参见 this SO question for further info
有关原始类型和非原始类型(又名 Class/reference/object 类型)之间差异的额外阅读内容详尽 here。他们也有一个很好的图表来说明它。