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 the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Apache Commons Collections SetUtils
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Implement Circular Singly Linked List
Java Program to Implement Floyd Cycle Algorithm
A Guide to JPA with Spring
Java Program to Construct K-D Tree for 2 Dimensional Data
Comparing Dates in Java
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
How to Count Duplicate Elements in Arraylist
Spring Boot - Google OAuth2 Sign-In
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Removing all duplicates from a List in Java
Template Engines for Spring
Quick Guide to Spring MVC with Velocity
Tính kế thừa (Inheritance) trong java
Introduction to the Functional Web Framework in Spring 5
RegEx for matching Date Pattern in Java
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Implement Sieve Of Atkin
Convert Hex to ASCII in Java
Java Program to Implement Gauss Jordan Elimination
Spring Security – Reset Your Password
Spring REST API + OAuth2 + Angular
Extra Login Fields with Spring Security
Hướng dẫn Java Design Pattern – Singleton
Java Program to Implement TreeMap API
Convert String to int or Integer in Java
A Guide to System.exit()
Java Program to Implement Uniform-Cost Search
Using JWT with Spring Security OAuth
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm