0

Consider line 2 and line 3 in the following code.....

class ModifyObjects {
    static void modifyString1(String s){
        s = "xyz";
    //Or any other operations
    }

    static String modifyString2(String s){
        s = "xyz";
        return s;
    //Or any other operations
    }

    static void modifyPrimitive1(int i){
        i=9;
    }

    static int modifyPrimitive2(int i){
        i=9;
        return i;
    }
}

public class Operations {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    String st1 = "abcd";
    String st2 = "qwerty";
    String st3;

    int i1=0, i2;

    st1 = "xyz";                          //line 1
    System.out.println("st1: " + st1);     

    ModifyObjects.modifyString1(st2);
    System.out.println("st2: " + st2);     //line 2

    st3 = ModifyObjects.modifyString2(st2);
    System.out.println("st3: " + st3);

    System.out.println("st2: " + st2);

    ModifyObjects.modifyPrimitive1(i1);
    System.out.println("i1: " + i1);            //line 3

    i2 = ModifyObjects.modifyPrimitive2(i1);
    System.out.println("i2: " + i2);    
    }
}

line 2 gives st2 as qwerty (does not modify. Should be xyz.) line 3 gives i1 = 0 (does not modify. Should be 9.)

This looks a bit odd. Here is the ouput:

 st1: xyz
 st2: qwerty
 st3: xyz
 st2: qwerty
 i1: 0
 i2: 9

Also at line 1 a new string object "xyz" is created right? I think "abcd" is just not being referenced from here.

Mitesh Pathak
  • 1,306
  • 10
  • 14

2 Answers2

3

in Java references to objects are passed by value...

    1.  st1: xyz

    Reason : you are not returning anything...

    2.  st2: qwerty

    Reason :You are not storing the returned value in st2. you should do,

        st2=ModifyObjects.modifyString1(st2);

    3.  st3: xyz

    reason : You are returning a String value and storing it in st3

    4.  st2: qwerty

    Reason : st2 is qwerty...

    5.  i1: 0  // Not reassigning value to anything

    6.  i2: 9  // returned value reassigned to i2.
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

I think you have some confusion on how Strings and parameter passing work in java.

1) Strings are by design immutable objects, and thus cannot be changed once built. Just for clarity, something like this:

String s = "a";
s += "bc";
System.out.println(s);

Is not modifying the value of the String s, but creating a new object. At the end of this code, you have created 2 String objects: one with value "a", in the first line, and one with value "abc" in the second line. (This is a voluntary simplification since java compilers may decide to optimize this code, but it is correct anyway)

2) If you need a mutable String object, java gives you the StringBuilder object (or StringBuffer if you have the need of a synchronized version). It is actually the recommended idiom if you need to append things to a String.

3) Parameter passing is always by value in java; this means that using a parameter in the left side of an assignation will NEVER have an effect outside the method call; instead, you just step on the value of the parameter you received. This is true doesn't matter if the parameter is a primitive type or an object type. If the parameter is an object type, the parameter passing is by value too, but what you copy to the method call stack is a copy of the reference to the object. So, in your modifyStringXX methods, the only thing you achieve is to step on the reference to the parameter, but you will never see an effect outside that methods.

Jorge_B
  • 9,712
  • 2
  • 17
  • 22