简体   繁体   English

在Java中将对象和原始数据作为参数传递有什么区别?

[英]what is the difference between passing an object and a primitive data as the parameter in Java?

I'm a little bit confused about the difference between passing an object and a primitive data as the parameter in Java. 对于将对象和原始数据作为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. 我还阅读了该讨论,解释了Java中没有传递引用。 So what is the real difference between above two passes and why Java deals with them differently? 那么,上述两次通过之间的真正区别是什么?为什么Java处理它们的方式有所不同? Thanks in advance. 提前致谢。

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. 在第二种情况下,将复制参考值。

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. 不复制Object ,将对它的引用传递给函数。 This means that you can change the Object within the function. 这意味着您可以在函数内更改Object

However, the reference is passed by value. 但是,引用按值传递。 When that posting creates a new Dog from within the function, the value of the reference changes. 当该发布从函数中创建新的Dog时,引用的值将更改。 It is no longer a reference to the Dog that was passed to the function, but to a new one. 它不再是对传递给函数的Dog的引用,而是对新的Dog的引用。

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. 在这种情况下,您不会传递副本,所做的更改将反映在原始副本中。

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中的long对象和基本类型long之间有什么区别? - What is difference between Long object and primitive type long in Java? 原始类和原始数据类型之间有什么区别? - What is the difference between a primitive class and primitive data type? 在Java中将基元或对象放入对象映射之间的区别 - Difference between putting primitive or object to an object map in Java Java作为参数,泛型方法和方法对象之间有什么区别? - Java What is difference between generic method and a method object as a parameter? Primitive array和Reference Array有什么区别? - What is the difference Between Primitive array and Array of Reference . 基本类型和原子类型有什么区别? - What is the difference between a primitive type and an atomic type? 原始类型和引用类型有什么区别? - What's the difference between primitive and reference types? 在Java中通过引用传递和在C中传递指针有什么区别? - What is the difference between passing by reference in Java and passing a pointer in C? 创建新模型和视图或将模型作为方法参数传递之间有什么区别 - What is the difference between creating a new modelandview or passing in a model as method parameter 构造函数中的 this.setMethod(parameter) 和 setMethod(parameter) 有什么区别? [爪哇] - What is the difference between this.setMethod(parameter) and setMethod(parameter) in a constructor? [Java]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM