First off, This is not a duplicate question. Read on and you will know why.
An answer here, which is also a part of the wiki, explains in great detail how parameters are passed in method arguments.I am going to copy/paste it here so readers don't have to go back and forth to a new browser tab:
I feel like arguing about "pass-by-reference vs pass-by-value" is not super-helpful.
If you say, "Java is pass by whatever (reference/value)", but in either case you're not provide a complete answer. Here's some additional information that will hopefully aid in understanding what's happening in memory.
Crash course on stack/heap before we get to the Java implementation: Values go on and off the stack in a nice orderly fashion, like a stack of plates at a cafeteria. Memory in the heap (also known as dynamic memory) is haphazard and disorganized. The JVM just finds space wherever it can, and frees it up as the variables that use it are no longer needed.
Okay. First off, local primitives go on the stack. So this code:
int x = 3; float y = 101.1f; boolean amIAwesome = true;
results in this:
When you declare and instantiate an object. The actual object goes on the heap. What goes on the stack? The address of the object on the heap. C++ programmers would call this a pointer, but some Java developers are racist against the word "pointer". Whatever. Just know that the address of the object goes in the stack.
Like so:
int problems = 99; String name = "Jay-Z";
An array is an object, so it goes on the heap as well. And what about the objects in the array? They get their own heap space, and the address of each object goes inside the array.
JButton[] marxBros = new JButton[3]; marxBros[0] = new JButton("Groucho"); marxBros[1] = new JButton("Zeppo"); marxBros[2] = new JButton("Harpo");
So, what gets passed in when you call a method? If you pass in an object, what you're actually passing in is the address of the object. Some might say the "value" of the address, and some say it's just a reference to the object. This is the genesis of the holy war between "reference" and "value" proponents. What you call it isn't as important as that you understand that what's getting passed in is the address to the object.
private static void shout(String name){ System.out.println("There goes " + name + "!"); } public static void main(String[] args){ String hisName = "John J. Jingleheimerschmitz"; String myName = hisName; shout(myName); }
One String gets created and space for it is allocated in the heap, and the address to the string is stored on the stack and given the identifier
hisName
, since the address of the second String is the same as the first, no new String is created and no new heap space is allocated, but a new identifier is created on the stack. Then we callshout()
: a new stack frame is created and a new identifier,name
is created and assigned the address of the already-existing String.
So, value, reference? You say "potato".
So, according to the same logic, the swap function should work as well. But it doesn't. Let me show you:
... main(Str..) {
String one = "one";
String two = "two";
swap (one,two);
System.out.println("outside method " + one);
System.out.println("outside method " + two);
}
swap(String o, String t) {
String temp = o;
o = t;
t = temp;
System.out.println("inside method " + o);
System.out.println("inside method " + t);
}
This doesn't change the values of variables one
and two
. Why not? 2nd question: How do we change the values of one
and two
then in a (swap
) method?
This is how it will look in memory: