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:
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Exploring the Spring Boot TestRestTemplate
Collect a Java Stream to an Immutable Collection
Summing Numbers with Java Streams
Spring Boot - Sending Email
Hướng dẫn Java Design Pattern – Template Method
Hashing a Password in Java
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Find Basis and Dimension of a Matrix
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Unsatisfied Dependency in Spring
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Spring WebClient vs. RestTemplate
Spring Boot - Cloud Configuration Client
Receive email by java client
Java Program to Perform Left Rotation on a Binary Search Tree
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Generate Random Numbers Using Probability Distribution Function
Lập trình đa luồng với CompletableFuture trong Java 8
Removing all Nulls from a List in Java
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Debug a HttpURLConnection problem
A Guide to ConcurrentMap
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Implement Dijkstra’s Algorithm using Queue
Deque và ArrayDeque trong Java
Java Program to Implement Binary Tree
Java Program to Perform Uniform Binary Search
Hướng dẫn Java Design Pattern – Observer