简体   繁体   English

指针如何与Java中的原始类型一起使用?

[英]How do pointers work with primitive types in Java?

I was reading What is a NullPointerException, and how do I fix it? 我正在阅读什么是NullPointerException,我该如何解决? , and in the accepted answer, I read something that I did not quite understand: 在接受的答案中,我读到了一些我不太了解的内容:

 int x; x = 10; 

In this example the variable x is an int and Java will initialize it to 0 for you. 在此示例中,变量x是一个int,Java会将它初始化为0。 When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x. 当您在第二行中将其分配给10时,您的值10将被写入x指向的内存位置。

I thought for primitive types, the variable was the memory address of the actual value; 我想原始类型,变量是实际值的内存地址; where as for complex types, the variable was merely the memory address of a pointer to the actual value. 对于复杂类型,变量只是指向实际值的指针的内存地址。 But the quoted answer above tells me I am wrong. 但上面引用的答案告诉我,我错了。 It says "the memory location pointed to by x." 它说“x指向的内存位置”。

So if x is pointing to a memory address which stores the actual value, how is a primitive type different from a complex type? 因此,如果x指向存储实际值的内存地址,那么原始类型与复杂类型有何不同? I did not know primitive types even had pointers. 我不知道原始类型甚至有指针。 How do pointers work with primitive types? 指针如何与原始类型一起使用?

A primitive type and complex type are different from each other primarily in the way data is stored. 原始类型和复杂类型主要在数据存储方式上彼此不同。 You're actually looking at the differences between a primitive type and a class type 您实际上在查看基本类型和类类型之间的差异

1. Every variable is stored as a location in the computer memory. 1.每个变量都存储在计算机内存中的一个位置。

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 . 2.对于基本类型:变量的值存储在分配给变量的存储器位置中

That means if we assigned int x = 10 , the value of x is stored in where the value of 10 is stored, ie the memory location. 这意味着如果我们分配int x = 10 ,则x的值存储在存储值10的位置,即存储位置。 That means when we "look" at x, '10' is stored there. 这意味着当我们“看”x时,'10'存储在那里。 Maybe it would help to think of it more like an "assignment" where you command that x be equal to 10. 也许它会更像是一个“赋值”,你命令x等于10。

3. For a class type: It only stores the memory address of the object that stores the value. 3.对于类类型:它仅存储存储该值的对象的内存地址。 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 . 整数x = 10将有一个指向int类型对象的内存地址,然后它将保持值10.这称为引用 Think of it as directory that tells you to go to which shelf to actually retrieve the value. 可以把它想象成一个目录,它告诉你去哪个架子来实际检索这个值。

Also

Class types are also known as reference types, or object types, that is they all mean an Object of a class (be it an Integer class, or MyPerson class). 类类型也称为引用类型或对象类型,它们都表示类的Object(无论是Integer类还是MyPerson类)。

Primitive types are not reference types because they do not hold references (memory addresses). 原始类型不是引用类型,因为它们不包含引用 (内存地址)。

This distinction is the reason for "wrapper classes" in daily use, and types such as Integer are seen as a wrapper class to an int , to allow for data manipulation such as storing integers in a data structure such as an ArrayList. 这种区别是日常使用中“包装类”的原因,并且诸如Integer类的类型被视为int的包装类,以允许数据操作,例如在数据结构(例如ArrayList)中存储整数。 Because ints a primitive data type, is not an object , while Integer is. 因为ints是原始数据类型,所以不是object ,而Integer是。 Since primitive types are not objects , we have to put them into a class in order for us to add them to Lists, Dictionaries etc. This way we have a List of Objects (which, point to the primitive types) but they are not a naked primitive datatype by itself. 由于原始类型不是对象 ,我们必须将它们放入一个类中,以便我们将它们添加到Lists,Dictionaries等。这样我们就有了一个对象列表(指向原始类型)但它们不是裸原始数据类型本身。 See this SO question for further info 有关详细信息,请参阅此SO问题

Additional reading on the difference between a primitive and non-primitive (aka Class/reference/object type) is detailed here . 此处详细介绍了原语和非原语(aka类/引用/对象类型)之间的差异。 They have a nice diagram illustrating it too. 他们有一个漂亮的图表来说明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM