How do pointers work with primitive types in Java?

I read What is a NullPointerException and how to fix it? , and in the accepted answer, I read what I did not quite understand:

int x; x = 10; 

In this example, the variable x is int, and Java initializes it to 0 for you. When you assign it 10 in the second line, your value of 10 is written to the memory location that x points to.

I was thinking of primitive types, the variable was the memory address of the actual value; where, as for complex types, the variable was simply the memory address of the pointer to the actual value. But the answer above tells me that I'm wrong. It indicates "the location of the memory that x points to."

So, if x points to the memory address where the actual value is stored, how does a primitive type differ from a complex type? I did not know that primitive types even have pointers. How do pointers work with primitive types?

+6
source share
1 answer

The primitive type and complex type differ from each other mainly in how the data is stored. Are you really looking at the differences between a primitive type and a class type

1. Each variable is stored as a place in the computer's memory.

The above statement applies to both primitive types and class types.

Differences:

2. For the primitive type: the value of the variable is stored in the memory cell assigned to the variable .

This means that if we set int x = 10 , the value of x will be stored in where the value of 10 is stored, that is, in the memory cell. This means when we "look" at x, "10" is stored there. Maybe this will help, think that this is more like a “job” where you indicate that x is equal to 10.

3. For the class type: it only stores the memory address of the object that stores the value. He is not the object itself.

The integer x = 10 will have a memory address pointing to an object of type int, which will then hold the value 10. This is called a reference . Think of it as a directory that says go to the shelf in order to really get the value.

AND

Class types are also known as reference types or object types, that is, they all mean a class object (whether it be an Integer class or a MyPerson class).

Primitive types are not reference types because they do not contain a reference (memory address).

This difference is the reason for “wrapper classes” in everyday use, and types such as Integer are considered as wrapper classes for int , which allows you to manipulate data, such as storing integers in a data structure, such as an ArrayList. Since ints primitive data type, it is not an object , but Integer is. Since primitive types are not objects , we must put them in a class so that we can add them to Lists, Dictionaries, etc. Thus, we have a List of objects (which indicate primitive types), but they are not a bare primitive data type in and of themselves. See this SO question for further details.

Additional reading about the difference between primitive and non-primitive (aka Class / reference / object type) is described in detail here . They have a good diagram illustrating this too.

+9
source

Source: https://habr.com/ru/post/985903/


All Articles