0

I have class which has void methods.

My question is, how does these methods executing. As my stringBuilderAppend() is a void method but when i call this method its returning the values which is been appended inside that method and I m doing the same check with other two method (i.e, stringAppend() and addNumbers() ) which are not returning anything.

  public class VoidMethodChecker {
    public static void main(String... a){
        StringBuilder stringBuilder = new StringBuilder("first StringBuilder ");
        stringBuilderAppend(stringBuilder);

        String string = "first String ";
        stringAppend(string);

        int number = 0;
        addNumber(number);


        /* stringBuilderAppend method prints "first StringBuilder second StringBuilder"
        *  why does stringBuilder() returning the appended value. (my stringBuilder() is void method) 
        *  as other two method does not appending*/

        System.out.println(stringBuilder);     // it prints "first StringBuilder second StringBuilder"

        System.out.println(string);            // it prints "first String"
        System.out.println(number);            // it prints "0"
    }

    private static void stringBuilderAppend(StringBuilder stringBuilder) {
        stringBuilder.append("second StringBuilder");
    }

    private static void addNumber(int number){
        number = 120;
    }

    private static void stringAppend(String string){
        string += "second String";
    }
}

Thanks

ravikumar
  • 893
  • 1
  • 8
  • 12
  • Java is pass-by-value. Always. Two of your methods, you're reassigning the parameter, the other one you're calling a method on the reference. Guess which one will have noticeable effects. – awksp Jun 18 '14 at 07:59

4 Answers4

3

stringBuilderAppend: The method is not returning anything. You are passing an object and modify its state inside the methods body. As you keep a reference to the same object in the calling method, you see the changes made on that object. This is a fundamental concept of object orientation which you need to understand.

addNumber: You are passing a primitive integer which is assigned to the parameter number. That parameter is nothing more than a variable only visible in the methods body. If you assign a new value to that variable, which you do, the caller doesn't see anthing of that. The same would happen in stringBuilderAppend if you would assign a new StringBuilder to your variable (stringBuilder = new StringBuilder()) before modifying it.

stringAppend: Basically the same as addNumber. string += "second String" is the same as string = string + "second String" so you are assigning a new value to the variable.

André Stannek
  • 7,773
  • 31
  • 52
2

You are passing a reference by value to your function private static void stringBuilderAppend(StringBuilder stringBuilder).

That means that the function can modify the object being referred to by stringBuilder. Which is what you're doing.

What you can't do in your function is to set stringBuilder in main to refer to a different StringBuilder instance.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

Let's have a look at your examples:

stringBuilderAppend: your main-method creates a StringBuilder-object and passes a reference (to be exact: a copy of the original reference, since references are passed by value) to that object to stringBuilderAppend. There you call that object's append() method, thus modifying the original object.

addNumber: since number is an int and int is a primitive type you are not passing a reference but a copy of the original int. that is modified during the method, but the original variable remains unchanged

stringAppend: here things start to get interesting. You are passing a copy of the reference to the original String-object. But += doesn't modify the original object (Strings can't be modified, they are immutable) but instead creates the concatenated Strings as a new object. the assignment then replaces the copy of the reference. Both the original reference and the original object remain unchanged.

piet.t
  • 11,718
  • 21
  • 43
  • 52
0

Java strings are immutable i.e., you cannot change the value of the string. When you add a new string literal to the existing value, Java creates one more new string. Where as StringBuilder/StringBuffer are mutable.

Hence in case of String, it creates a new literal in memory containing value "first stringsecond string", where limiting the scope to that specific method. Where in case of StringBulder it appends the string to the existing string.

Nevertheless, when you pass int as a parameter, it is pass by value.

Thanks, JK

André Stannek
  • 7,773
  • 31
  • 52
HJK
  • 1,382
  • 2
  • 9
  • 19
  • Whether it is immutable or not is irrelevant. The same syntax will produce the same result in all cases. Assigning to a reference variable will always make it point to a new object, and will never change any objects. – newacct Jun 18 '14 at 20:50
  • Here what I want to say is, as String is a mutable object, when we do changes in "stringAppend", Java creates a new string and point it to the new value. The old reference would not be get effected. – HJK Jun 19 '14 at 11:12