[Solved] How are these two situation different? [duplicate]


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.

solved How are these two situation different? [duplicate]