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:

Java Program to Implement Bresenham Line Algorithm
Guide to the Volatile Keyword in Java
Documenting a Spring REST API Using OpenAPI 3.0
Working With Maps Using Streams
Comparing Objects in Java
The Java 8 Stream API Tutorial
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Implement Skip List
Java Program to Represent Graph Using Incidence List
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Simple Single Sign-On with Spring Security OAuth2
Uploading MultipartFile with Spring RestTemplate
Java Program to Generate a Sequence of N Characters for a Given Specific Case
SOAP Web service: Authentication trong JAX-WS
New Features in Java 11
Java Program to Solve the Fractional Knapsack Problem
Convert Hex to ASCII in Java
Java – Try with Resources
Introduction to Spring Cloud CLI
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Implement Hash Tables Chaining with Binary Trees
Java Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Implement Ternary Search Tree
Java Program to Implement Unrolled Linked List
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Check Cycle in a Graph using Graph traversal
Các kiểu dữ liệu trong java
Guide to PriorityBlockingQueue in Java
Java – Reader to Byte Array
Java Program to Represent Graph Using Incidence Matrix