I have another absolute beginners question that I probably should know the answer to after 3 months of playing with Java but I have either not come across the information or have forgotten it.
What I would like to know is where certain things live in memory.
Now.... I "think" that all instance variables live on the heap along with any objects created using the 'new' keyword. That static variables have their own "special" storage area (does anything else live in this region or is it only statics?) And that variables created inside a method live on the stack. Am I right about these things?
What I am totally unaware of though is where any values passed in as arguments to a method live? And when passing the main() method of a console application any values upon execution from the windows command prompt, do these live in the same place as any other arguments passed to methods? I am guessing yes considering that main() is just another method but just want to confirm. Also the variable you use to iterate through in a for loop eg.
Where does that integer i live?
Also. When you give a method parameters, is the storage for the parameters created before any arguments are passed into it?
When is storage created for the int i being passed to the doIt() method?
Paul Clapham wrote:Values passed as parameters to a method (or constructor) are treated just the same as local variables inside that method. As for exactly when they are created, I have no idea and I don't think it's useful to find out.
So they live on the stack then? And the arguments passed in to the main() method when invoked at the command prompt are the same too?
And I agree that there's not much use in knowing when the storage is actually allocated to parameters or arguments ( I've seen people use these two terms interchangeably but I think there is a slight difference. Parameters are what your method expects to receive and arguments are what actually get passed to it - I don't know the "official" definitions and whether I am right or wrong but that's the way I look at it) but just for completeness and peace of mind I would like to know. I am the kind of person who gets caught up in minutiae, which I know can become quite a hindrance to learning some things but hey that's just me.
Miles Williams wrote:So they live on the stack then? And the arguments passed in to the main() method when invoked at the command prompt are the same too?
I would presume that all methods behave the same, but the simple fact is that it's just not important - and believe me, if Paul doesn't know, then it's probably not worth knowing.
I am the kind of person who gets caught up in minutiae, which I know can become quite a hindrance to learning some things but hey that's just me.
Then I suggest you change your habits, or maybe read the DontSweatIt page, which has exactly this question as a topic.
Java is a memory-managed language, and it was made so precisely so that you don't have to worry about things like this.
Unless you need it for the SCJP exam - in which case, you should probably read the study guide (which still seems to be the latest version) - you're almost certainly wasting your time. And we'd hate you to do that.
Winston Gutkowski wrote:Unless you need it for the SCJP exam - in which case, you should probably read the study guide (which still seems to be the latest version)
Come out from that cave, Oracle has owned Java for over four years
Ben Allen wrote:Come out from that cave, Oracle has owned Java for over four years
Yeah, more's the pity (I worked at Sun for a while). They've threatened a new guide for OCJP7 for 2014, but I don't see any sign of it as yet - not that I could afford the exams anyway now that those turkeys have taken over.