-2

In Pass By value when arguments are passed by value to a method, it means that a copy of the original variable is being sent to the method and not the original one, so any changes applied inside the method are actually affecting only the copy version and not the original one.

for example,

public class Test {



    private void squareNumber(int number){
        number=number*number;
    }

    public static void main(String[] args) {

       int x=2;


        System.out.println(x);  //output    =   2

        new Test().squareNumber(x);

        System.out.println(x);//output    =   2

    }
}

But in case of Arrays and List, this doesn't work like this and below is the example of ArrayList

public class Test {


    private void squareOfList(List<Integer> integerList){
        for (int i=0;i<integerList.size();i++) {
            integerList.set(i,integerList.get(i)*integerList.get(i));

        }
    }


    public static void main(String[] args) {

        List<Integer> nums= new ArrayList<>();
        nums.add(2);
        nums.add(3);
        nums.add(4);
        nums.add(5);


        System.out.println(nums); // output  =  [2, 3, 4, 5]

        new Test().squareOfList(nums);

        System.out.println(nums);  // output  =  [4, 9, 16, 25]

    }

}

Since we have passed nums as a parameter, only the value inside the method should change but it is changing the value of the original list. Does this "STRICTLY Pass by value" term hold good?

Mukul Bhardwaj
  • 562
  • 5
  • 16
  • No, this topic is different from Is Java "pass-by-reference" or "pass-by-value". – Mukul Bhardwaj Jul 11 '19 at 08:21
  • 1
    It really isn't. If you read the answers on that question you'll understand exactly why your code works the way it does. – JonK Jul 11 '19 at 08:22
  • 1
    For non-primitives, Java copies the reference – GalAbra Jul 11 '19 at 08:24
  • 1
    Because `int` is primitive type in java and `List` is reference type. When `List` is copied the reference is copied – Ashwani Jul 11 '19 at 08:26
  • then why Java is considered as strictly pass by value? – Mukul Bhardwaj Jul 11 '19 at 08:29
  • when you pass primitive datatype like int in a function the value of that variable is passed which has no link with its reference(address in heap) so operation in that value does not change anything in its reference but when u pass a object the whole reference(address) of the variable is passed and operation is directly done on that address – rahul Jul 11 '19 at 08:31
  • 1
    @Mukul Bhardwaj Java is *always* pass by value; for non primitives, Java passes the value of the reference to the `Object`, not the `Object` itself. – Austin Schaefer Jul 11 '19 at 08:40
  • Your variable holds a *reference* to the list or array. When you call a method, you are correct: a copy of that reference is sent to the method. It’s still a reference to the same list or array. Hope you’ll be able to get it straight. – Ole V.V. Jul 11 '19 at 08:40
  • @Ashwani Dausodia, so that means we only have one list in memory and different variables are referring to that list (correct me if I am wrong). Then why java is termed as strictly pass by value (since STRICTLY means that this law is applicable to all the things put in the same basket regardless whether it is primitive data type or list)? – Mukul Bhardwaj Jul 11 '19 at 09:39
  • @Ole V.V., so that means we only have one list in memory and different variables are referring to that list (correct me if I am wrong). Then why java is termed as strictly pass by value (since STRICTLY means that this law is applicable to all the things put in the same basket)? Why shouldn't we use different word than "STRICTLY pass by value"? – Mukul Bhardwaj Jul 11 '19 at 09:39
  • 1
    Your understanding is correct. Whether that deserves the term “strictly by value” is a fight about words that I am not taking part in. – Ole V.V. Jul 11 '19 at 09:53

1 Answers1

1

int is primitive type and List is Reference type. According to JLS

The reference types are implemented by dynamically created objects that are either instances of classes or arrays. Many references to each object can exist.

When you pass a value of any reference type in a function only the reference is copied. Now both the references (caller, callee) are pointing to the same dynamically allocated memory. If you change that (dynamically allocated memory) in callee it is going to reflect in caller.

A variable of a primitive type holds a value of that exact primitive type

int is primitive type and it holds the value. when passed to a function it copies the value and there is no dynamically allocated memory which is shared. therefore all the changes you make in function remain in the function.

This example may clarify it further

public class Test {
    private void squareOfList(List<Integer> integerList){
        integerList = new ArrayList<>(); // Refering to a different dynamically allocated memory
    }

    public static void main(String[] args) {
        List<Integer> nums= new ArrayList<>();
        nums.add(2);
        System.out.println(nums); // output  =  [2]
        new Test().squareOfList(nums);
        System.out.println(nums);  // output  =  [2]
    }

}

For further information read chapter 4 of java language specification.

Ashwani
  • 1,938
  • 11
  • 15