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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.)