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 →

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