I have the following code
class sample
{
public void update(List l)
{
l = null;
}
public static void main (String[] args)
{
List m = new ArrayList();
m.add("suresh");
m.add("sankar");
new sample().update(m);
System.out.println(m);
}
}
The answer will be {[“suresh,”sankar”]}. The m is a pointer to the arraylist object, it contains an memory address value (for ex consider 0xf34 ). when we pass m to update method ,the local variable l will be set to 0xf34 that points to arraylist object in memory .when we set null to this variable l , the memory address replaces the arraylist with null ,hence the variable m should also refer null.am i right.please help.
No, the compiler has it right. 🙂
The parameter
lcontains a reference to the ArrayList object assigned tom.lgets updated to null, and indeed any later use oflwithin theupdate()method would see it as null. Butlis a separate variable that has scope only within that method — it’s not linked tomin any way (other than the fact that they originally contained references to the same object).