Table of Contents
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:
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 add two large numbers using Linked List
Introduction to Spring Method Security
Java Program to Describe the Representation of Graph using Adjacency List
Hướng dẫn sử dụng Printing Service trong Java
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Adding a Newline Character to a String in Java
Spring Boot - Flyway Database
Introduction to Using FreeMarker in Spring MVC
Filtering a Stream of Optionals in Java
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Quản lý bộ nhớ trong Java với Heap Space vs Stack
“Stream has already been operated upon or closed” Exception in Java
Multipart Upload with HttpClient 4
Multi Dimensional ArrayList in Java
Updating your Password
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Generate N Number of Passwords of Length M Each
LinkedHashSet trong java
Giới thiệu java.io.tmpdir
Using JWT with Spring Security OAuth
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
A Guide to the finalize Method in Java
Comparing Long Values in Java
Spring REST API + OAuth2 + Angular
Hướng dẫn sử dụng Java Generics
Spring Security OAuth2 – Simple Token Revocation
Limiting Query Results with JPA and Spring Data JPA
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Guide to Spring @Autowired
Java Program to Find kth Smallest Element by the Method of Partitioning the Array