Skip to content

How are strings passed into methods in java (In terms of memory)

When we call methods, if you are passing in a primitive type, that value is put on the stack frame when calling the method. If you are passing in a reference type, if it’s not null, you would have already created it on the heap some where and what gets put on the stack is a reference to it.

But what happens on the stack, when I do something like


Does it first create a string object on the heap and then put a reference to that on the stack?


Given your code, "Jack" is a String constant so it resides in the run-time constant pool. This pool resides in an area of the JVM called the method area.

However, if the code is changed to:

Person.SetName(new String("Jack"));

then the passed reference points to an object on the heap, as it would normally for any reference type.