Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an ArrayList parameter modified, but not a String parameter? [duplicate]

public class StackOverFlow {     public static void main(String[] args) {         ArrayList<String> al = new ArrayList<String>();         al.add("A");         al.add("B");         markAsNull(al);         System.out.println("ArrayList elements are "+al);          String str = "Hello";         markStringAsNull(str);         System.out.println("str "+ str);     }     private static void markAsNull(ArrayList<String> str){         str.add("C");         str= null;     }     private static void markStringAsNull(String str){         str = str + "Append me";         str = null;     } } 

This outputs:

ArrayList elements are [A, B, C] str Hello 

In the case of ArrayList, the added elements are getting retrieved. In case of String the method call has no effect on the String being passed. What exactly is the JVM doing? Can anyone explain in detail?

like image 471
Naresh Avatar asked Apr 08 '13 05:04

Naresh


People also ask

Are Arraylists modifiable?

You can modify an ArrayList elementarily (Only one element is added or removed or updated) or in bulk (More than one elements are added or removed or updated).

Can ArrayList store doubles?

ArrayList cannot hold primitive data types such as int, double, char, and long.

How do you pass an ArrayList as an argument?

Class one has an ArrayList as one of its attributes and it calls a void method from class two and passes that ArrayList as a parameter. Now that method initializes another ArrayList and makes it equal to the parameter passed by me and makes changes to that new ArrayList .


Video Answer


2 Answers

In the case of Arraylist string objects the added elements are getting retrived. In case of String the method call has no effect on the String being passed.

It happens cause Java is Pass-by-Value and Strings are immutable

When you call

markAsNull(ArrayList<String> str) 

The a new reference by name str is created for the same ArrayList pointed by al. When you add an element on str it gets added to same object. Later you put str to null but the object has the new values added and is pointed by a1.

When you call

markStringAsNull(String str) {     str = str + "Append me";     // ... } 

The line str = str + "Append me"; creates a new String object by appending the given string and assignes it to str. but again it is just reference to actual string which now pointing to newly created string. (due to immutablity) and the original string is not changed.

like image 153
Azodious Avatar answered Oct 11 '22 14:10

Azodious


The markXAsNull methods are setting the local references to be null. This has no effect on the actual value stored at that location. The main method still has its own references to the values, and can call println using those.

Also, when doing the string concatenation, toString() is being called on the Object, and that is why the ArrayList is outputted as a list of its values in brackets.

like image 39
Whymarrh Avatar answered Oct 11 '22 13:10

Whymarrh