1

I have these two situations:

String s = "aa";
s = s + " aa";
    System.out.println(s);
    //......

that work fine! It prints aa aa. But there is a problem:

String s = "aa";
this.addStringToStatement(s, " aa");
System.out.println(s);
//...
private void addStringToStatement(String statement, Object value) {
    statement += value;
}

It prints: aa. What is the reason?? Thanks!

1
  • 2
    Apart from the actual problem/question (Java is not pass-by-reference as you seem to expect), in real world you'd normally use the builtin String#concat() method or maybe a StringBuilder for this.
    – BalusC
    Mar 4, 2010 at 19:44

8 Answers 8

5

There are two issues to understand here.

  1. The String append operator will create a new immutable String object. The expression s + value is a different object in memory from s.

  2. In your function addStringToStatement when you execute statement += value; you aren't changing the value of the variable s, rather you are reassigning the local pointer statement to a new String instance.

EDIT: Fixed usual noob mistake: In Java, object references are passed, not the objects themselves.

1
  • 2
    You were doing so well, right until the last sentence. All Java parameters are passed by value. Mar 4, 2010 at 20:07
4

This is because in Java a reference is passed by value. Here when you passed s and " aa" as two parameters.

In the method statement variable (which has reference of s) is altered to point to something else ie "aa aa". Note only the statement reference is passed, s is still pointing to "aa".

So when you print s you get what is expected :)

3

this is because of how passing by value works in Java, you need to do something like this:

String s = "aa";
s = this.addStringToStatement(s, " aa");
System.out.println(s);
//...
private string addStringToStatement(String statement, Object value) {
    statement += value;
    return statement;
}
3
  • No such thing as passing by reference in Java. Mar 4, 2010 at 20:07
  • whoops I meant value. big typo there. Thanks for pointing that out @Michael
    – GSto
    Mar 4, 2010 at 20:27
  • This would be correct. Partially the reason for the slowness of Java at times. Mar 4, 2010 at 21:54
1

Strings in Java are immutable. If you have a String s="aa", you only have a reference to the String "aa" inside the JVM. If you want the String "aa aa" assigned to s, you assign only the reference (address) to "aa aa" (another String inside the JVM) to s, "aa" lurks still somewhere in the JVM.

The statement "Java references are passed by value" is somewhat confusing (if true). If you use a StringBuilder sb and give this StringBuilder to a function, the reference is "copied" but still points to the same object as sb:

public static void main(String[] args) {
    final StringBuilder sb = new StringBuilder();
    f(sb);
    System.out.println(sb);
}

private static void f(final StringBuilder sb) {
    sb.append("aa aa");
}
1

You have to return the "statement" variable. Without the "this.*" part.

String s = "aa";
s = addStringToStatement(s, " aa");
System.out.println(s);
//...
 private String addStringToStatement(String statement, Object value) {
    statement += value;
    return statement;
}
1
  • should be private String, not private void
    – GoingTharn
    Mar 4, 2010 at 21:06
1

@Frank Meißner (new to this, can't reply to answers yet)

Just to clarify the difference between String and StringBuilder (in case anyone reading this is confused), while both store the CharSequence they hold as a char[], the char[] within String cannot be changed, thus a new String has to be created for every alteration. In the case of a StringBuilder, there are methods like StringBuilder.append(String) that can change the internal char[] of the StringBuilder, so if StringBuilder.append(String) is called, no new object will have to be created for the content of the StringBuilder to change.

As per Frank Meißner's example, System.out.println(sb); will print "aa aa", since the method append(String) was called on the StringBuilder sb. final doesn't hurt here since the identity of sb isn't changed, only its state.

0

+= in case of string doesn't modify string itself but produce new string. in case of your method you set this new string reference to local variable (parameter)

0

Java references are passed by value. When you pass a String to a method and it changes inside it points to different String. You can either return the String after appending or use StringBuffer as argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.