Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with pass by value in java [duplicate]

Java supports pass by value (always works on a copy) but when you pass a user defined object then it changes the actual object (kind of pass by reference but no pointer changes), which I understand but why the changeObject2CLEAR method below is actually changing the value of the object ? Instead it has to work on the copy?


import java.util.HashMap;
import java.util.Map;

public class PassBy {

    class CustomBean {
        public CustomBean() {

        }

        private int id;
        private String name;

        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }   

        @Override
        public String toString() {
            return id + ", " + name;
        }
    }

    public Map<Integer, String> changeObject2NULL (Map<Integer, String> m) {
        m = null;
        return m;
    }

    public Map<Integer, String> changeObject2CLEAR (Map<Integer, String> m) {
        m.clear();
        return m;
    }

    public CustomBean changeCustomObject (CustomBean _e) {

        _e.setId(_e.getId() + 1);
        return _e;
    }

    public static void main(String[] args) {

    PassBy passby = new PassBy();

    Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(1, "value");

    CustomBean bean = passby.new CustomBean();
    bean.setId(1);
    bean.setName("arun");

    // Initial Value
    System.out.println(map.toString());
    System.out.println(bean.toString());
    System.out.println("-------------------------");

    // Pass by value works fine
    passby.changeObject2NULL(map);
    passby.changeCustomObject(bean);

    // Custom object value changes since we pass as the object reference but Map remains with actual value
    System.out.println(map.toString());
    System.out.println(bean.toString());
    System.out.println("-------------------------");

    // Testing Pass by value - CANT UNDERSTAND why it changed the object since it has to be pass-by-value and not by ref in this case ??
    // Why setting to null not chainging the value but clear does ?
    passby.changeObject2CLEAR(map);
    System.out.println(map.toString());

    }
}
like image 964
AKS Avatar asked Sep 27 '22 18:09

AKS


1 Answers

So let me try to help you understand, Java always does pass by value, but I am sure you know that all object instances are actually pointers to those objects. Now when you send an object then you are passing a value of the address of the object. If you do any changes to the object itself (like m.clear()) then it goes to that address, type casts the object and does the operation on it. But if you change the pointer itself, like m = null, then only the copy of the address you are holding is changed.

like image 55
vishalv2050 Avatar answered Sep 30 '22 08:09

vishalv2050