2

I've been reading this topic: Is Java "pass-by-reference" or "pass-by-value"? , and I've tried to run example from this answer: https://stackoverflow.com/a/73021/2838739 . In fact value of myDog has been changed outside this method. Then I have tried to do the same with an Integer. I was surprised because its value has not been changed. Could someone explain me why?

My test code is:

   package testtest;

class Dog {

    String name;

    public Dog(String name) {
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog("Rover");
        System.out.println(myDog.getName());
        foo(myDog);
        System.out.println(myDog.getName());

        Integer i = 5;
        changeValue(i);
        System.out.println(i);
    }

    public static void changeValue(Integer i) {
        i = 50;
    }
    public static void foo(Dog someDog) {
        someDog.setName("Max");     // AAA
        someDog = new Dog("Fifi");  // BBB
        someDog.setName("Rowlf");   // CCC
    }

}

And output was:

Rover

Max

5

Thank you in advance.

Community
  • 1
  • 1
  • 1
    "Java is always pass-by-value." - from the most voted answer. However, it works ***slightly*** different with Objects (over primitive data types). That's why Objects work almost like pass-by-reference. The solution explains this – But I'm Not A Wrapper Class Feb 07 '14 at 13:41
  • Someone will probably be able to describe this better, but in Java, when assigning an `Integer` object with a primitive `int` value, it automatically creates a new `Integer` object whose value is that primitive `int`. So in this case, `i` is being given a new object reference inside `changeValue()`, and the `i` in `main()` is left untouched. – jonhopkins Feb 07 '14 at 13:43
  • What part of the linked question and the answers don't you understand? – Sotirios Delimanolis Feb 07 '14 at 13:44
  • 1
    The changeValue method changes the reference of i. If you were able to write i.intValue = 50, the value outside the method would also change. Since Integer is an imutable object this is not possible. I would say that java is always "pass reference by value" :) – Lars Juel Jensen Feb 07 '14 at 13:44
  • So is there any rule when the value will be changed outside called method? I thought that if I pass an Integer object, it should behave the same as Dog class. – Paweł Jabłoński Feb 07 '14 at 13:45
  • The rule for that is, if you directly change a property of the object, typically though the use of a method like `setName()` or `theObject.name = "a new name";`, then the object will be changed outside of the method, but if you do something like `theObject = somethingElse;`, then it will not be changed outside the method because the local reference to that object will now point to a completely different object. – jonhopkins Feb 07 '14 at 13:46
  • The reason this is a bit unclear when you set i = 50 directly, is that this is converted to i = new Integer(50) by the compiler due to autoboxing. – Lars Juel Jensen Feb 07 '14 at 13:49
  • Now it seems to be clear :) Thank you – Paweł Jabłoński Feb 07 '14 at 13:51

3 Answers3

4

Integer is not a mutable class. (There is no set method on it.) You can point an old variable to a different Integer, but that doesn't change the Integer that you passed to the method.

Joshua Goldberg
  • 5,059
  • 2
  • 34
  • 39
0

Integer is a primitive data type. You cannot change them inside methods.

If you pass an object to a method you can change its accessible member variables.

In Java there are special wrapper classes for primitive types like Integer for int, Character for char etc, and even though they are like objects you cannot change their values inside methods either. That's the only exception that I know.

wldchld
  • 146
  • 3
0

As pointed out already primitive types are passed by value. But if you really need to pass primitives by reference you can use an array.

public class PassByRefMain {

    public static void main(String[] args) {
        PassByRefMain m = new PassByRefMain();
        int[] number = new int[]{5};
        System.out.println(number[0]);
        m.changevalue(number);
        System.out.println(number[0]);  
    }

    public void changevalue(int [] number){
        number[0] = 9;
    }
}

The output will be

5
9
FuryFart
  • 2,304
  • 4
  • 27
  • 43