0

Conside the below program

class A{
    public void foo0(){
        ArrayList<Integer> tmp = new ArrayList<>();
        tmp.add(1);
        System.out.println(tmp.size());
        
        foo1(tmp);
        
        System.out.println(tmp.size());
        
        foo2(tmp);
        
        System.out.println(tmp.size());
    }
    
    public void foo1(ArrayList<Integer> tmp){
        tmp = new ArrayList<>();
    }
    
    public void foo2(ArrayList<Integer> tmp){
        tmp.add(4);
    }
}
public class Main
{
    public static void main(String[] args) {
        A obj = new A();
        obj.foo0();
        
    }
}

My expected answer was 1 0 1 as by considering pass by reference scenario. But the actual output is different Output :- 1 1 2

0 Answers0