0

Basically, when I passed arguments in Java, I knew it was passing only value.

However, the following code shows that the add method executed on SubClass's SubMethod affects ArrayList of MainClass.

MainClass.java

public class MainClass{
    public satatic void main(String[] args){
        List list = new ArrayList<>();
        SubClass subClass = new SubClass(list);
        subClass.subMethod();
        System.out.println(list) // Why added value???
    }
} 

SubClass.java

public class SubClass{
    private List list;
    public SubClass(List list){
        this.list = list;
        
    }
    public void subMethod(){
       list.add(1);
       list.add(2);
    }
} 

When I did the same thing with a HashMap's put, there was no effect on the HashMap of the MainClass.

I would like to know why only ArrayList is causing these results and what is happening inside Java.

Update

The code for the hashmap version is as follows: MainClass.java

public class MainClass{
    public satatic void main(String[] args){
        Map map = new HashMap<>();
        SubClass subClass = new SubClass(map );
        subClass.subMethod();
        System.out.println(map) // Not putting value
    }
} 

SubClass.java

public class SubClass{
    private Map map;
    public SubClass(Map map){
        this.map= map;
        
    }
    public void subMethod(){
       map = someGenerationHashMap(arg1, arg2);
    }
} 
Community
  • 1
  • 1
srsok
  • 29
  • 1
  • 4
  • Can you please share the code for the `HashMap` version? Because they should behave the same way. – Federico klez Culloca Oct 22 '19 at 08:00
  • 5
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Eduard Dubilyer Oct 22 '19 at 08:00
  • Java passes objects by references. It means Your `SubClass` and `MainClass` share the same list. It is different when You pas a cardinal values like int, long, boolean, etc (note the lowercase). Version with `HashMap` - It will also affect the `HashMap`. Can You please share the code with `HashMap`, there has to be some difference. – zolv Oct 22 '19 at 08:00
  • Update My code. –  srsok Oct 22 '19 at 08:09
  • System.out.println (map) is just simplified code. The problem is that in that situation HashMap.isEmpty is true. –  srsok Oct 22 '19 at 08:14
  • Forget what I said, it was wrong. `HashMap`'s `toString` method returns the content as well, so it should print. See Andres' answer for the actual reason (i.e., you're not adding to the map, you're creating a new one and losing the reference immediately after that) – Federico klez Culloca Oct 22 '19 at 08:15
  • `someGenerationHashMap` reassigns new map to `map`. It means that maps in `MainClass` and `SubClass` are not referrring the same one. – zolv Oct 22 '19 at 09:41

1 Answers1

2

It's not about ArrayList. Any object you pass as an argument can be modified. What is passed by value is the address of the object, not the object itself.

In the Map version, you are not making any operation that could modify it. In the list version instead, you are making an add.

Make sure not to confuse objects with primitives. For example, make sure not to confuse int with Integer.

Andres
  • 10,561
  • 4
  • 45
  • 63