4
  1. I assigned a variable map to null:

    public static void main(String[] args){
        Map map = null;
        createMap(map);
        System.out.println("map is:" + map);
    }
    
  2. and I pass the variable map to the method createMap and re-assigned it:

    public static void createMap(Map map){
        if(map == null){
            map = new HashMap();
            map.put("key", "value");
        }
    }
    

And the Variable map still is "null", could someone tell me why?

reto
  • 9,995
  • 5
  • 53
  • 52
Marks
  • 327
  • 3
  • 14

6 Answers6

4

For this you need to understand the concept of pass by value and pass by reference.

Pass by value When you use pass-by-value, the compiler copies the value of passed argument to the calling function to a corresponding non-pointer or non-reference parameter of the called function. The parameter in the called function is initialized with the value of the passed argument. As long as the parameter has not been declared as constant, the value of the parameter can be changed, but the changes are only performed within the scope of the called function only; they have no effect on the value of the argument in the calling function.
In simple words if there is int i = 5; and this is passed to function void manipulate(int i) then the value 5 is copied to new location and a pointer (address) is passed to called function and now if you make any changes to it they will not be reflected.

In your case you have an object of type Map and when you pass it then a new pointer is created pointing to a that object which is null and address of that is passed and when you assign it to a new object the changes are not reflected as you are assigning a new pointer, which is not the same as pointed by the map object that was passed.

If you want the changes to get reflected back use pass by reference but the Java is always Pass by value for this you can use AtomicReference

public static void createMap(AtomicReference<Map> atomicmap)
{
    Map map = atomicmap.get();
    if(map == null)
    {
        map = new HashMap();
        map.put("key", "value");
    }
    atomicmap.set(map);
}

public static void main(String[] args) 
{
        Map map = null;
        AtomicReference<Map> atomicmap = new AtomicReference<Map>();
        atomicmap.set(map);
        createMap(atomicmap);
        map = atomicmap.get();
       System.out.println("map is:" + map); 
 } 
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
3
public static void createMap(Map map){

In the code above, map reference variable points to the same object which is passes by the caller method. But when you do this :

map = new HashMap();

A new object is created and map is pointing to it now. But the reference in the caller method will continue pointing to the passed object. Hence if you make any changes to the map after creating new object will not get reflected in the caller reference variable.

Here it is:

Map map = null; // Caller reference variable
createMap(map); 

Here map is still pointing to the old object (unfortunately NULL in your case)

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

You are only manipulating the local variable l in your method createMap. The variable map in your first method is still pointing to null.

To understand how this works, consider this:

In your main method, you are creating a varialbe map, and assigning it null. You now have this variable on your stack, but still no corresponding object on the heap.

You then pass this variable as a parameter to createMap. Java does this call by value, i.e. it makes a copy of the variable and pushes it to stack in conjunction with the method call. You now have two variables on the stack, and still no object on the heap.

Finally, inside your createMap method, you create a new Map object and assign it to l, your copy of the first variable. Now, you have two variables on the stack, and one object on the heap, of which one points to the Map object:

map ---> null l ---> Map

When the createMap method returns, the stack is popped and your variable l is deallocated. map is still not pointing to the Map object - it never did, it will still point to null. Further, since the Map object now has no live references, it will be swept up by the next garbage collection cycle.

So, what you have in the end is again a single variable on the stack, which still points to null.

csvan
  • 8,782
  • 12
  • 48
  • 91
0

Yes. Because the reference isn't visible outside of the local stack (the value of a null reference was passed in). You could very easily do this though -

public static Map createMap(Map map){
  if(map == null){
    map = new HashMap();
  }
  map.put("key", "value"); // Really?
  // More stuff.
  return map;
}

And then change your calling method like so

Map map = createMap(null); // This would then work
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You pass null to createMap. Java passes all parameters by value, not by reference. Your createMap method assigns a new HashMap to the local variable map, not to the instance you tried to pass. Here is an article about parameter passing.

Tobias
  • 7,723
  • 1
  • 27
  • 44
0

Java passes references by value.

So you can't change the reference that gets passed in. So you are passing

createMap(map);

So map will be always pointing to null.

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70