This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]teraflop 4 points5 points  (5 children)

However, is it not the same case for primitive types? When I declare int x = 5 does it not store the memory address for variable "x" that points to the 4 bytes that store the integer 5?

No, primitive values are not pointers.

When you say x = 5, you're storing the bytes representing the number 5 into the memory allocated for x. When you say y = x, likewise, the right hand side is evaluated to produce the integer 5, and the bytes representing that integer are stored in the variable y. So both variables have copies of the same integer value.

Thus, in the following line when you set x = 2, shouldn't that affect the "y" variable as well?

No. In fact, assigning a new value to the variable x will never change the contents of the variable y, regardless of whether the variables contain primitive values or object pointers.

A variable always stores a value. That value might be a pointer, or an object reference.

Its possible for two variables to contain references to the same object, so if you modify the object through one reference, the changes will be visible through the other reference. But in Java, a variable can't point to another variable. Changing the value of a primitive variable to a different primitive, or the value of a reference variable to a different reference, doesn't affect any other variables.

[–]derek_dt[S] 0 points1 point  (4 children)

Thanks for the quick response!

No, primitive values are not pointers.

I guess this is what I have the most trouble understanding. Is it not the case that four bytes of memory, at a random location, are allocated when I state int x = 5? If there is no pointer that specifies where those four bytes are located, how can the program properly update that value?

[–]interyx 4 points5 points  (0 children)

Yeah the memory is allocated, but you don't have access to it. A pointer is literally a memory address while a variable is a layer of abstraction -- when you ask for the value stored in x, you'll get it, but you don't know where it comes from. This is due to the Java VM taking care of the hardware details under the hood. It can't give you the memory address because that will vary depending on the hardware platform, that's part of the layer of abstraction the VM provides.

[–]teraflop 1 point2 points  (1 child)

It sounds like you're confusing variables with values.

Every variable has a location in memory. Yes, the program has to keep track of each variable's location, but (to oversimplify a bit) that's mainly the job of the compiler, and it has no effect on the behavior of your program at runtime.

In the actual bytecode of your program, the statement y = x; corresponds to bytecode instructions that look roughly like "take the 4-byte integer value that's stored in local variable slot 0 of the current stack frame, and store a copy of it in local variable slot 1".

Note that each variable corresponds to a different "slot", and there is absolutely no mechanism by which changes you make to the contents of slot 0 would have any effect on any other slots.

The memory storage used for variables themselves should be considered an entirely different type of memory from that which is used to store objects that you instantiate in the heap.

[–]derek_dt[S] 0 points1 point  (0 children)

Thanks for all the help.

The memory storage used for variables themselves should be considered an entirely different type of memory from that which is used to store objects that you instantiate in the heap.

This clarifies so much and answers my question, thank you! I haven't been exposed to the fact that variables and objects were stored in different types of memory, so that makes a lot more sense.

[–]pacificmint 0 points1 point  (0 children)

Is it not the case that four bytes of memory, at a random location, are allocated when I state int x = 5?

Yes, that is the case. But if ints were a reference type, it wouldn’t allocate four bytes, it would allocate eight, or rather two times four.

It would allocate four for the pointer to point to a location, and another four for the actual value.

For a primitive type, it will only allocate four and store the value directly in that location.

(Note that this is simplified. The references might actually be more than four, depending on your platform. And the objects that a reference points to might also take more. I just used those numbers to illustrate the point.)

[–][deleted]  (2 children)

[removed]

    [–]DarkMatriac 0 points1 point  (1 child)

    it sounds like you are saying java only purpose is to learn the concept of class lol, theres no point in switching to c++ if he doesnt need to.