10

I was reading What is a NullPointerException, and how do I fix it?, 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. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by 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."

So if x is pointing to a memory address which stores the actual value, how is a primitive type different from a complex type? I did not know primitive types even had pointers. How do pointers work with primitive types?

7
  • is not it a reference into the memory location? Commented Apr 24, 2015 at 18:38
  • @KickButtowski For complex types, yes. What I don't get is that something is pointed to by x, which is a primitive type.
    – Evorlor
    Commented Apr 24, 2015 at 18:58
  • when you say point to, it reminds me pointer in c and c++ which we do not have such a thing in Java. Commented Apr 24, 2015 at 19:00
  • 1
    I think you should take a look at difference between reference and pointer . google it Commented Apr 24, 2015 at 19:05
  • 2
    @KickButtowski Thanks! I will try and wade my way thru all the C++ reference and pointer material and look for something Java specific.
    – Evorlor
    Commented Apr 24, 2015 at 19:09

1 Answer 1

11

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.

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.

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).

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. Because ints a primitive data type, is not an object, while Integer is. 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. See this SO question for further info

Additional reading on the difference between a primitive and non-primitive (aka Class/reference/object type) is detailed here. They have a nice diagram illustrating it too.

4
  • I changed your format a little , so It can be easy to read. hope it is ok with you. I have already plus one u too Commented Apr 24, 2015 at 18:54
  • So is it just that I got confused with the wording of "the memory location pointed to by x."? They did not mean to imply that the memory location of x pointed anywhere...they just meant x?
    – Evorlor
    Commented Apr 24, 2015 at 18:55
  • @KickButtowski just saw that. definitely okay with me. thank you! I added some info so I mirrored your edits. Please edit accordingly! Commented Apr 24, 2015 at 18:56
  • 1
    @Evorlor precisely. If you think of it in terms of programming languages that use pointers (C based ones for example), the difference is that a primitive type is just int x, while a class type will be a class that has a pointer (mem address) to int x. If that makes sense... the link I gave has a more thorough write up, and explains it in more detail than I attempted to, i think. Commented Apr 24, 2015 at 18:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.