public void otherMethod(){
    List<String> list = new ArrayList<String>();
    list.add("abc");
    list.add("abc");
    list.add("abc");
    someOtherMethod(list);
    System.out.println(list.size());
}

public void someOtherMethod(List<String> list){
    list.add("abc");
}

Invoking otherMethod prints 4.

where as

public void otherMethod(){
    int a = 10
    someOtherMethod(a);
    System.out.println(a);
}

public void someOtherMethod(int a){
    a = 11;
}

prints 10;

How are the two different. Aren't both local variables? Does sending list reference work in a different way? Please help me understand how the two scenario differ?

Before downvoting please make me understand why this below one also prints 10?

public void otherMethod(){
    Long a = new Long(10);
    someOtherMethod(a);
    System.out.println(a);
}

public void someOtherMethod(Long a){
    a = 11;
    //or a= new Long(11);
}
有帮助吗?

解决方案

In fact conceptually they are not different, in both cases a copy of the original value is passed in as an argument to the method. The thing is that in the first case you have a complex/composite structure of some sort so you pass in a reference copy which is different from the original reference but which still points to the original structure/object.

In the second case you pass in a copy of an int and so the method just operates on the copied int. Unlike that in the first case the method operates directly on the original structure through that copy of the reference (which it receiver from the caller).

Now, in the case of Integer and Long, you work on the original objects through a copied reference. The thing is that you cannot do much on the original object as these classes are immutable e.g. they don't have methods Integer.increment(int n) or Long.increment(long n) which change the original objects by incrementing their values. It would be same if we talk about String (which is immutable) but not the same if we talk about StringBuilder (as the latter is mutable).

public void someOtherMethod(Long a){
    a = 11;
    //or a= new Long(11);
}

In this example you direct the copied reference to a new object (11)
but the caller still has the original reference which still points
to the same old object (10). You have no way of changing the original
object (from within the called method) just because Long is immutable.
If it wasn't, you would be able e.g. to increment it e.g. by calling
a.increment(1). If that was possible, this is different: you're not
directing the copied reference to a new object, you're using it to call
a method on the original object.

Remember, Java is pass-by-value (always) but certain
differences like these make people confused sometimes.

其他提示

int are not Objects in Java, so no a is not a reference.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top