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:

So sánh Array và ArrayList trong Java
Spring Security OAuth Login with WebFlux
Validations for Enum Types
How to Set TLS Version in Apache HttpClient
Introduction to Spring MVC HandlerInterceptor
Spring Boot - Securing Web Applications
Java Program to Find a Good Feedback Vertex Set
Java Program to Implement Graph Coloring Algorithm
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement ArrayDeque API
Spring Security with Maven
Mix plain text and HTML content in a mail
Case-Insensitive String Matching in Java
Java Program to Find Path Between Two Nodes in a Graph
The DAO with Spring and Hibernate
Java Program to Implement Pagoda
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
How to Get the Last Element of a Stream in Java?
Flattening Nested Collections in Java
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Find the GCD and LCM of two Numbers
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Tính đóng gói (Encapsulation) trong java
How to Delay Code Execution in Java
Lớp LinkedHashMap trong Java
Spring Security – Reset Your Password
Java Program for Topological Sorting in Graphs
Map Serialization and Deserialization with Jackson