• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiler handling of value types  RSS feed

 
Cliff DeRose
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A variable that gets declared stores its value on the stack...my question is this: How is a variable associated with a stack entry? Is a variable just an alias for an address of a stack entry?
If so, when "pass by value" is used to send a copy of a value type to a method, does the actual block of memory on the stack get copied and the new address that marks that block get assigned to the method's variable?
The compiler just knows whenever it sees a value type variable to go to the address and retrieve the value?
Cliff
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing you're normally a C programmer, right?
Luckily for us, we don't need to worry about how a valriable is associated with a stack entry. The Virtual machine takes care of all that. In fact, you can look at the Java Virtual Machine spec if your interested in those kind of details. Specifically I think this section (3.2 DataTypes), seems promising, but it still doesn't go down to the stack level, I believe that's left up to the author of the VM.
On a more abstract level. "Pass by Value" only works with primitive types. Where you actually pass the value of the primitive from one method to the other. For all other variables (objects, arrays, etc) you "Pass by Reference" where you simply pass the reference to the object, rather than the object itself.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be really picky, "pass by reference" is really "pass a pointer by value". That is, the JVM passes a COPY of the reference (pointer) to the called method. The called method can change the object the pointer points to, but cannot change the pointer to point to another object. That's pretty serious double-talk and may take a few tries to parse, but it makes sense if you cross your eyes just right.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java passes all types (primitive types and object reference types) STRICTLY by value.
I have tried to demonstrate this fact at:
http://www.xdweb.net/~dibblego/java/faq/answers.html#q21
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!