Error with passing by value in java

Java supports passing by value (always working on a copy), but when you pass a user-defined object, then it changes the actual object (the type of transmission by reference, but the pointer does not change), which I understand, but why the changeObject2CLEAR method below actually changes the value of the object ? Should he work on a copy instead?


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()); } } 
+5
source share
3 answers

When you call changeObject2CLEAR

 passby.changeObject2CLEAR(map); 

you are passing a map instance.

in changeObject2CLEAR method

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

you execute .clear() on the same map instance, although in the method it is called m .

As an exercise in understanding, notice that the next method will do the same.

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

Note that you do not need to return Map<Integer, String> m , since the map that you have access to is the same instance of the object that is passed wherever the method is called.

EDIT: why m = null; behaves like pass-by-value, but m.clear() behave like passing by reference?

When you assign a null value to m , you change the link from the previous instance of the map object to a new null memory location.

When you call the .clear() method of an .clear() object m , you call the method on the same object that is in the memory location referenced by map , therefore, you modify the map object.

+1
source

So let me try to help you understand, Java always goes by value, but I'm sure you know that all instances of objects actually point to these objects. Now when you send the object, you pass the value of the address of the object. If you make any changes to the object (for example, m.clear ()), then it will go to this address, enter cast and the object on it. But if you change the pointer itself, for example m = null, only a copy of your address will be changed.

+3
source

AFAIK Java only passes the value, but the values ​​are actually references

0
source

All Articles