Java pass by value query

49 Views Asked by At

Since java is pass by value.In below code we are passing a value to appendStringMethod not a reference, then why in main method we get HelloWorld not just Hello after calling appendStringMethod() in main.

public class test {
    public static void main(String args[]) {

        StringBuilder str = new StringBuilder("Hello");
        appendStringMethod(str);
        System.out.println(str);
    }

    static void appendStringMethod(StringBuilder s) {
        s.append("World");
    }
}

However in below code, values are not swapped.

public class Mock {
  public static void main(String args[]) {
    StringBuilder str1 = new StringBuilder("Hello");
    StringBuilder str2 = new StringBuilder("World");

   swap(str1, str2);
   System.out.println(str1);
   System.out.println(str2);

   }
   static void swap(StringBuilder s1, StringBuilder s2) {
   StringBuilder s= s1;
   s1=s2;
    s2=s1;
   }
}
3

There are 3 best solutions below

2
On

While Java is technically pass by value for everything, as spoken about here, It's best not to think of it like that.

When passing an instance of a class into a method, you're really passing the reference of the object by value.


StringBuilder str = new StringBuilder("Hello");
appendStringMethod(str);

In this code, you are passing a reference to the StringBuilder instance into the appendStringMethod by value.

As a result, str will become "HelloWorld" once the method has been called.


Note: This doesn't apply to primitives such as int and char. Since they are not actual objects, they won't have a reference. This means they will be passed by value in the "expected" way.

1
On

It's because the reference to the StringBuilder is passed by value. You can add characters and they will be in the instance after the method returns. This in the end acts like a pass by reference. It works similarly with the Collection classes (List, Map,...), as well as your own classes.

In the case of primitive types (int,...), Java behaviour is simple: The value is copied in another instance of the primitive type.

In case of Objects, this is the same: Object variables are pointers that holds the Object’s address so the references are copied. The only exception I can think of are String Objects as the characters are stored in an array declared final so that it cannot be modified.

1
On

In java we pass by value of reference. Have a look at a following example:

public void foo(Integer i) {
    i = new Integer(1000);
}

public void bar(Integer i) {
    i++;
}

Integer n = new Integer(2000);
foo(n);
bar(n);
System.out.println(n.toString());

I believe that much of the confusion on this issue has to do with the fact that different people have different definitions of the term "reference". People coming from a C++ background assume that "reference" must mean what it meant in C++, people from a C background assume "reference" must be the same as "pointer" in their language, and so on. Whether it's correct to say that Java passes by reference really depends on what's meant by "reference".

--first comment