1

I'm a little bit confused about the difference between passing an object and a primitive data as the parameter in Java. I read a post here explaining that when you pass a primitive data, you copy that data and pass it, but if you pass an object then you pass by the object reference. And I also read this discussion explaining that there's no pass-by-reference in Java. So what is the real difference between above two passes and why Java deals with them differently? Thanks in advance.

Community
  • 1
  • 1
Wen Zhu
  • 127
  • 2
  • 13

4 Answers4

6

There's no difference between passing a primitive and an object reference. Both are passed by value. In the first case, the primitive value is copied; in the second case, the reference value is copied.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • But how come a setter method changes the value of the instance variable? In a setter method too it should be passed by value. – underdog May 02 '15 at 03:36
  • @underdog *"In a setter method too it should be passed by value"* Yes, the parameters to the setter method are passed by value, for sure. The object on which you call the method is not passed as a method parameter. – m0skit0 May 02 '15 at 03:40
2

When you pass a primitive, you actually pass a copy of value of that variable. This means changes done in the called method will not reflect in original variable.

When you pass an object you don't pass a copy, you pass a copy of 'handle' of that object by which you can access it and can change it. This 'handle' is a 'reference'. In this changes will be reflected in original.

Now one thing, what would happen when you pass array variable of a primitive type. In that case you do not pass a copy and the changes made will be reflected in original one.

Paramvir Singh Karwal
  • 597
  • 1
  • 10
  • 24
2

To be clear, primitive types are passed by value, a copy of the type is what exists inside a function.

An Object is not copied, a reference to it is passed to the function. This means that you can change the Object within the function.

However, the reference is passed by value. When that posting creates a new Dog from within the function, the value of the reference changes. It is no longer a reference to the Dog that was passed to the function, but to a new one.

Carl
  • 993
  • 8
  • 14
1

Just to clarify the responses so far

public class Car {

    private String type;

    public Car(String type) {
        this.type = type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public static void foo(Car someCar) {
        Car newCar = new Car("Sedan");//new object created
        someCar = newCar; //passed reference also points to newly created object
        someCar.setType("Coupe");//Referred object changes
        System.out.println(newCar.type);//Coupe
        System.out.println(someCar.type);//Coupe
    }

    public static void main(String[] args) {
        Car myCar = new Car("Sports");//new object created
        foo(myCar);//reference is passed by value
        System.out.println(myCar.type);//Sports
    }
}
kaykay
  • 556
  • 2
  • 7