-2

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:

primitives on the stack

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";

a b*7ch aint one!

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");

marx brothers

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 call shout(): a new stack frame is created and a new identifier, name is created and assigned the address of the already-existing String.

la da di da da da da

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:

enter image description here

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Faraz
  • 6,025
  • 5
  • 31
  • 88
  • You pasted an entire other question with an existing answer and the whole thing is trivially googleble. This is a terrible question and you should delete it.. – pvg Mar 25 '17 at 23:54
  • 1
    I read on, and I still think it's a duplicate. If you think it's not, you need to explain your own thought process about why you don't agree with or understand the answers to the linked question. "So, according to the same logic, the swap function should work as well." No, it means you're not understanding the explanation you pasted. To be fair, it's a rather convoluted explanation with unusual terminology. – John Kugelman Mar 25 '17 at 23:57
  • "How do we change the values of one and two then in a (swap) method?" You can't. It's not possible to write a swap method in Java. – John Kugelman Mar 26 '17 at 00:00
  • Worthwhile to note that an equivalent to your swap function wouldn't work in C++ either. The Java reference is just like a C++ pointer except you can never dereference it directly (like `*one = *two` as you would need to do to write a swap function), only dereference with a member access like `->`. http://ideone.com/UsNWem – Radiodef Mar 26 '17 at 00:03
  • @pvg No I have not copied pasted the WHOLE question. I have copied ONLY the answer. Any only because so people don't have to go to new page and come back and forth to understand my question. And I don't understand a concept, so I posted a question. So no I am not deleting it. – Faraz Mar 26 '17 at 00:05
  • @JohnKugelman ok. Can you explain how to change the values of Object one and object two when passing them in method parameters? Is that possible? – Faraz Mar 26 '17 at 00:09
  • Like I said, no, it's not possible – John Kugelman Mar 26 '17 at 00:09
  • @JohnKugelman I am making another object and will try to manipulate it. I will update my question then. – Faraz Mar 26 '17 at 00:19
  • @FarazDurrani I have no idea what you're talking about. – pvg Mar 26 '17 at 00:21
  • @JohnKugelman okay I made a class with 2 fields and created two objects out of it. And tried to swap the fields in a swap method. Actually now I know what is going on in memory. I did it by creating sketches. And now I know exactly how values are changing in memory. Can you delete this question plz? – Faraz Mar 26 '17 at 01:12

2 Answers2

2

That swap doesn't work because Java passes method arguments by value, not by reference. This is no "holy war"; such a claim is only made by someone who doesn't understand the facts. In fact, pass-by-reference and pass-by-value are objectively verifiable, factual phenomena. Your swap example is the canonical proof that Java does not pass by reference. All the excess verbiage in the article you quoted is just blown smoke.

Bear in mind that what Java calls a "reference" is actually a pointer. Even in C++, which supports pass-by-reference, pointers are normally passed by value. You have to explicitly ask for them to be passed by reference.

Java doesn't give you that option. You may only pass by value.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
0

Your swap() method wouldn't work for a few reasons. When swap() is called, the parameters are new references to the value, so when I call swap(String1, String2), on runtime, the variable String1 would be passed to a new reference in the parameter.

So what this does is that even though we called swap(one,two), parameters are new variables in the scope which correspond to the values of the original one & two respectively.

This means when I call swap(), the parameters are copies of the variables, not the same variables. As Strings are immutable and cannot be changed (only the variable reassigned), this means that even though I call one = "SomethingElse", that means the parameter copy of the reference is equal to SomethingElse, not the original variable which still has its value of one = "something" as I am NOT reassigning the original reference of one, just reassigning the parameter variable which happens to have pointed to the same value.

To fix this, you would need to do your methods code in the original statement without the method call so the variables are what you want them to be.

Alex Pawelko
  • 414
  • 1
  • 4
  • 14