Home > Java > javaTutorial > What is the difference between object reference and value passing in Java?

What is the difference between object reference and value passing in Java?

WBOY
Release: 2024-04-11 18:48:01
Original
532 people have browsed it

The difference between object reference passing and value passing in Java: Value passing: Pass a copy of the basic data type, and modifying the copy will not affect the original variable. Passing by reference: Passing a reference to a reference type variable. Modifying the object pointed to by the reference will affect all variables holding the reference.

What is the difference between object reference and value passing in Java?

The difference between object reference and value passing in Java

Preface
Java programming language support There are two ways of passing by value and passing by reference. Understanding the differences between these two types of passing is crucial to writing efficient and correct Java programs.

Value passing
Value passing passes a copy of the variable to a method or other thread. The original variable and the copy are independent, and modifying the copy will not affect the original variable. Primitive data types (such as int, double, boolean, etc.) are always passed by value.

Example:

int a = 10;
int b = a; // 值传递,创建 a 的副本
b++;  // 修改副本
System.out.println(a); // 输出 10
Copy after login

Pass by reference
Pass by reference passes a reference to a variable to a method or other thread. The original variable and reference point to the same object, which means that any modifications to the object will be reflected in all variables holding that reference. Pass by reference is used to refer to object types (such as classes, interfaces, etc.).

Example:

Person person1 = new Person("John", 25);
Person person2 = person1; // 引用传递,person2 指向与 person1 相同的对象
person2.setName("Jane"); // 修改对象
System.out.println(person1.getName()); // 输出 "Jane"
Copy after login

Practical case

Value passing: passing basic data type

public void increment(int value) {
    value++;  // 仅修改局部副本
}
Copy after login

Pass by Reference: Passing Object Reference

public void changeName(Person person) {
    person.setName("New Name");  // 修改实际对象
}
Copy after login

Conclusion
Understanding the difference between object reference and passing by value is essential for writing robust and maintainable The Java code is crucial. Primitive data types are passed by value, while objects are passed by reference. By carefully considering the types of variables you pass, you can avoid unexpected behavior and write correct code.

The above is the detailed content of What is the difference between object reference and value passing in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template