0
public class ClassNameHere {
   public static void main(String[] args) {
      String[] str = new String[2];
      str[0] = "Hello";
      str[1] = "World";
      String test = "God!";
      str[1] = test;
      test = "Guy!";
      System.out.println(str[1]);
   }
}

I thought the result should be "Guy!" but actually is "God!". Do the elements in the String[] contain references to Strings? Or just contain the content of Strings directly?

Ray
  • 1
  • 3
    I think you have answered your own question. – Scary Wombat Feb 15 '22 at 06:27
  • 2
    See https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Scary Wombat Feb 15 '22 at 06:27
  • 1
    No contents of reference types are ever stored "directly" in other objects in the sense that you mean. – Stephen C Feb 15 '22 at 06:27
  • You should look at `str[1]` the same way you look at `test`, in terms of their relationship to the actual objects they are about. – ernest_k Feb 15 '22 at 06:29
  • The "pass by reference" question is not an answer to this. That is about the semantics of method argument passing. This is about the semantics of objects and references and ... assignment. – Stephen C Feb 15 '22 at 06:30
  • Thanks, guys! Strings are immutable may be the reason for it! The statment " test = "Guy!" " make the test pointing to a new instance. – Ray Feb 16 '22 at 07:13

1 Answers1

0

First, String really falls under category of complex objects in Java, which are passed by reference. However instances of String are immutable, and when using concatenation with it (like test = test + "abc") you in fact create a new object, which is no longer linked to the original test instance. Second, when assigning a value to a reference of any complex object like this obj = new Object() you rewrite the reference, no matter if it's a String or any other mutable object, thus obj no longer has any connection to the instance it originally held.

If you want to actually get an array of mutable strings, you may want to use StringBuffer or any other container that holds a String reference / char array underneath:

StringBuffer[] str = new StringBuffer[2];
StringBuffer buff = new StringBuffer("test");
str[0] = buff;
buff.append("value");
System.out.println(str[0].toString());
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • Sorry, I think it is not my point. Because in my opinion, after I assign test to str[1], both of them should point to the same instance. After I change the content of that instance through reference test, str[i] should equal to the new stuff, but it doesn't. – Ray Feb 16 '22 at 06:59
  • Thanks, guy! I finally get your points. Strings are immutable may be the reason for it. – Ray Feb 16 '22 at 07:15
  • @Ray you can think of a reference like of a number variable, where number represents address the reference points to. When you create an array of complex object, each element is a reference, i.e. a number variable, which can have whatever value you assign. If you then make a reference `test` and do assignment `str[1] = test` you just copy the number (address) of `test` to `str[1]`. If you later assign new number (address) to `test` it doesn't change whatever `str[1]` assigned to – The Dreams Wind Feb 16 '22 at 22:58
  • @TheDreamsWind I got it, thanks for your answer with patience. – Ray Feb 19 '22 at 16:52