I give the value of three reference types (Integer,String,List) to a function to change it.
But only the value of List changed, but Integer and String are reference types too.
Why can't I change their values too?
public static void main(String[] args) {
Integer i = new Integer(1);
String s = "apple";
List<Integer> l = new ArrayList<Integer>();
l.add(new Integer(1));
System.out.println("Integer: " + i);
System.out.println("String: " + s);
System.out.println("List: " + l);
System.out.println("");
setInteger(i);
setString(s);
setList(l);
System.out.println("Integer: " + i);
System.out.println("String: " + s);
System.out.println("List: " + l);
}
public static void setInteger(Integer i)
{
i++;
}
public static void setString(String s)
{
s = "banana";
}
public static void setList(List l)
{
l.set(0,2);
}
Output:
Integer: 1
String: apple
List: [1]
Integer: 1
String: apple
List: [2]