-1

I am a beginner in Java, recently faced such an interview question on Java String concept:

public class Test1 {

    public static void changeStr(String str){
        str="welcome";
    }

    public static void main(String[] args) {
        String str="1234";
        changeStr(str);
        System.out.println(str);
     }
}

I think the output should be "welcome", however, I tested it in Eclipse, it showed "1234", isn't Java string is a reference, so the Java string "str" references gets changed to "welcome" in method changeStr?

Pardon me for the beginner question!

Kei Minagawa
  • 4,395
  • 3
  • 25
  • 43
Kevin
  • 6,711
  • 16
  • 60
  • 107

5 Answers5

5

The object's reference is passed to the method and assigned to the parameter, which is a kind of local variable.

Assigning a different object reference to the parameter does nothing to the variable outside the method that held a reference to the original object.

Also String is immutable, so there's no way to change its value (for example a setValue() method).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

The line str = "welcome"; doesn't change the value of any String - Strings can never change their values. What it does is it makes one reference point to a different String. But the reference that it reassigns is the one that's local to changeStr, not the one that is declared in main.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • The **String str** and **String str="1234"** are not same one, they are two local variables live in stack. Both of those refer to string "1234" in String Constant Pool. But after assigning str="welcome", the **String str** reref to "welcome" and the **String str="1234"** still refer to "1234". The key in here, when you modifier a string means you create a new String not modifier itseft because String is immutable. – qmn1711 Jun 28 '14 at 08:39
  • @qmn1711 it seems to me that you should post that as a separate answer, since it's so completely different from anything I said. – Dawood ibn Kareem Jun 28 '14 at 09:30
  • but I only understood this question after reading your comment. I think it has a connection :) – qmn1711 Jun 29 '14 at 07:42
1

String is a reference, but the key is that String str inside changeStr is a different reference than str in main. Add that to the fact that strings are immutable in Java (meaning that when you change a String, the reference points to a different location in memory) and that explains why main will print 1234

geoand
  • 60,071
  • 24
  • 172
  • 190
1

Consider the following example:

public static void main(final String[] args) throws Exception {
    final StringHolder holder = new StringHolder("old value");
    System.out.println(holder);
    reassignHolder(holder);
    System.out.println(holder);
    changeVal(holder);
    System.out.println(holder);
}

static void reassignHolder(StringHolder holder) {
    holder = new StringHolder("new value");
}

static void changeVal(StringHolder holder) {
    holder.setVal("new value");
}

static class StringHolder {

    private String val;

    public StringHolder(String val) {
        this.val = val;
    }

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return "StringHolder{" + "val=" + val + '}';
    }

}

In Java references are passed by value and (almost) everything is an object reference.

Here we have a mutable object that holds a String - a StringHolder.

When we call reassignHolder what we actually do is copy our object reference and pass it to the method. When the method reassigns the reference nothing happens to our original reference as we are passing a copy.

When we call reassignHolder we also pass a copy of our reference, but the method uses this reference to call a method on our object to change its val variable. This will have an effect.

So the output is:

StringHolder{val=old value}
StringHolder{val=old value}
StringHolder{val=new value}

As String is immutable, you can only carry out the first example rather than the second.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

The logic here is that the str value that is passed is passed as a copy of reference of str. This implies that any change in str will not be reflected in the original str. This situation is similar to pass by value (but of references!)

However in your code if you change the code to str = new String("welcome"); It will change the original str string by making it reference a new copy that was created.

Hope this clears things for you.

Crystal Meth
  • 589
  • 1
  • 4
  • 16