Truyền giá trị và tham chiếu trong java

1. Truyền giá trị (pass by value) trong Java

Nếu chúng ta gọi một phương thức và truyền một giá trị cho phương thức đó được gọi là truyền giá trị. Việc thay đổi giá trị chỉ có hiệu lực trong phương thức được gọi, không có hiệu lực bên ngoài phương thức.

Ví dụ giá trị data không bị thay đổi sau khi gọi phương thức change():

class Operation {
    int data = 50;
 
    void change(int data) {
        data = data + 100; // changes will be in the local variable only
    }
 
    public static void main(String args[]) {
        Operation op = new Operation();
 
        System.out.println("before change " + op.data);
        op.change(500); // passing value
        System.out.println("after change " + op.data);
 
    }
}


Kết quả:

before change 50
after change 50

2. Truyền tham chiếu (pass by reference) trong Java

Khi chúng ta gọi một phương thức và truyền một tham chiếu cho phương thức đó được gọi là truyền tham chiếu. Việc thay đổi giá trị của biến tham chiếu bên trong phương thức làm thay đổi giá trị gốc của nó.

Ví dụ giá trị của biến data của đối tượng op bị thay đổi sau khi gọi phương thức change():

class Operation {
    int data = 50;
 
    void change(Operation op) {
        op.data = op.data + 100;// changes will be in the instance variable
    }
 
    public static void main(String args[]) {
        Operation op = new Operation();
 
        System.out.println("before change " + op.data);
        op.change(op); // passing object
        System.out.println("after change " + op.data);
 
    }
 
}


Kết quả:

before change: 50
after change: 150

Related posts:

Updating your Password
Hướng dẫn Java Design Pattern – Intercepting Filter
Servlet 3 Async Support with Spring MVC and Spring Security
Introduction to Using Thymeleaf in Spring
Convert String to Byte Array and Reverse in Java
Serialize Only Fields that meet a Custom Criteria with Jackson
A Guide to Spring Boot Admin
“Stream has already been operated upon or closed” Exception in Java
Configuring a DataSource Programmatically in Spring Boot
Spring Boot - Tomcat Port Number
Spring Boot - Quick Start
Java Program to Perform String Matching Using String Library
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java List UnsupportedOperationException
A Guide to HashSet in Java
Spring Cloud – Tracing Services with Zipkin
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Spring Boot - Twilio
Java Program to Implement Hash Tables with Linear Probing
Spring Security – security none, filters none, access permitAll
OAuth2 Remember Me with Refresh Token
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Guide to UUID in Java
Java Program to Implement Threaded Binary Tree
Java Scanner hasNext() vs. hasNextLine()
ETL with Spring Cloud Data Flow
Java Program to Implement Fermat Factorization Algorithm
Hướng dẫn Java Design Pattern – Command
A Custom Media Type for a Spring REST API
Spring Boot Configuration with Jasypt
Getting the Size of an Iterable in Java