I was reading the Cathy Sierra Bert Bates 5.0 certification book, and in the Assignment chapter, the first example that talks about heap and stack seems to ignore this. Hope this is a typo.. or is it not ??
Rama, In this case i think that a local variable args will be on stack but it may or may not point to any String Object as the argument may be furnished at run time or it may not. I think more clarification can be given by Bert or Kathy themselves...
Not specifically knowing the answer, but experience giving me this gut feeling, my response would be which stack?
Of course, we're talking about the stack referred to in K&B, but there's (probably) some behind the scenes action going on as well.
I believe the JVM maintains its own stack and heap for what's discussed in the book. However, there are more stacks behind the scenes. Our stack doesn't come into context until main() starts. True, args is probably an array on the heap, but pass by value says that the value of args refers to the heap, and is not on the heap itself. When you access args, it is pulled off the stack to determine where the heap location is. The JVM creates an object on the heap automatically between when the JVM fires up and when main() starts.
That said, now a different stack... Take a mental step back and think of an operating system or cpu stack that is separate from the JVM stack. Think of it like this: When a method is called, the JVM pushes the arguments of the calling method onto the os/cpu stack. When the called method is invoked, it pops those parameters from that stack and places them on the JVM stack to keep the world spinning . After that, the parameters are at the mercy of you - the programmer.
Similarly, when you return a value from a method, I believe it is pushed onto the os/cpu stack and popped off by the calling function. It is then either assigned to a variable or sent to GC.
That's a traditional method of how I know how it works. If those of the knowhow can shed a different light on this, I'd be curious (and humbly corrected) to know myself.