-1

I am a newbee.
I have read that the scope of local variables will be within a block (correct me if I am wrong). Here, the main method's local variables (the lists li and li1, and the StringBuffer y) are behaving like instance variables, and the variables (String y1 and int x) behave like local variables. Why ?

public class Test {
    public static void addValues(ArrayList<String> list, StringBuffer sb, int x){
        list.add("3");
        list.add("4");
        list.add("5");

        sb.append("String Buffer Appended !");
        x=x+10;
    }

    public static void addValues(ArrayList<String> list, String sb, int x){
        list.add("3");
        list.add("4");
        list.add("5");

        sb = sb + "is Appended !";
        x=x+10;
    }

    public static void main(String[] args) {
        ArrayList<String> li = new ArrayList<>();
        ArrayList<String> li1 = new ArrayList<>();
        StringBuffer y=new StringBuffer("ab");
        int x=10;
        String y1=new String("ab");
        li.add("1");
        li.add("2");
        li1.add("1");
        li1.add("2");
        System.out.println("b4 : "+li+" , y = "+y+" , y1 = "+y1+" x= "+x);
        addValues(li,y,x);
        System.out.println("Af : "+li+" , y = "+y+" x= "+x);
        addValues(li1,y1,x);
        System.out.println("Af : "+li1+" , y1 = "+y1+" x= "+x);
    }
}

Output :

b4 : [1, 2] , y = ab , y1 = ab x= 10
Af : [1, 2, 3, 4, 5] , y = abString Buffer Appended ! x= 10
Af : [1, 2, 3, 4, 5] , y1 = ab x= 10
Little Helper
  • 1,870
  • 3
  • 12
  • 20
LMK
  • 2,882
  • 5
  • 28
  • 52
  • 1
    Also see http://stackoverflow.com/questions/4658453/difference-between-mutable-objects-and-immutable-objects – ug_ Jun 30 '14 at 10:03
  • Sorry for posting this as an answer, since my reputation does not allow me to comment to your question. But this question was asked multiple times. Here, check [this](https://stackoverflow.com/questions/15871825/why-is-an-arraylist-parameter-modified-but-not-a-string-parameter), [this](https://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning) and [this](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – epipav Jun 30 '14 at 10:09

2 Answers2

3

In the case of the ArrayList and StringBuffer, you are passing a reference to an mutable object which reflects any modification on any variable that holds the same reference.

In the case of the String you are too passing a reference BUT its immutable so when you concatenate something to it, its actually creating a new String object and assigning a new reference to the variable within the method, you are not actually modifying the object itself.

And in the case of the int, all primitives are assigned and passed by value, so its actually making a copy of its value and passing it to the method, so anything you do to the variable within the method doesnt affect the variable outside of it.

Ed Morales
  • 1,027
  • 5
  • 9
0

Java always pass by value, means it will always pass the copy of the variables (primitive variable and reference variable) to the method's block of code.

When we pass an Object reference variables (in this case ArrayList and StringBuffer), actually we are copying the bit pattern of there reference variable, not the real object it self, the new copy in your addValues method refer to EXACTLY the same object as the the one declared in main method, so whenever we modify the value of the object's member the copy and actuall object reference variable will see the changes.

the String by default are immutable and int, they by default are immutable. so whenever we pass them to the methods as a paramater, the methods will only have the copy of the value from the String and int.

Your question related to "Stack and Heap" in java . you could watch this video as a reference

Sultan
  • 71
  • 3