(the following contents are purely my personal understanding. If there is any error, thank you for correcting it)
We all know that java is an object-oriented programming language, but in the practical sense, java is not purely object-oriented, because the meaning of object-oriented is that everything is an object. If the variable of int type is also an object, then we should be able to use "." This thing accesses its properties or methods, for example:
import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.get(0); } }
For example, we regard the list object as a variable. If it is an object, we should be able to access its properties and methods. However, the basic data type in java has no properties and methods, so we can say that just click "." The variables that can't be found are all basic data types (hence the wrapper class).
On the method passing of value type and reference type in java
Let's first look at how these data are stored in Java
I don't know if my brothers can understand it. I'm describing it in words
After two basic types of variables are declared in a function, these two variables are passed as parameters to another method. This method will copy two copies, that is, the values of the two copies are exchanged in the swap(int, int) method, and their real values are not exchanged. Then someone will think, wrap the class, Can't data of reference type be exchanged? Let's take a look:
public class Test { public static void main(String[] args) { Integer n1 = 1, n2 = 2; System.out.println("Before exchange: num1: " + n1 + "\tnum2: " + n2); swap(n1, n2); System.out.println("After exchange: num1: " + n1 + "\tnum2: " + n2); } public static void swap(Integer num1, Integer num2) { Integer temp = num1; num1 = num2; num2 = temp; } }
This result is not exchanged, so someone will think, what's going on?
In fact, although it is a reference type, it is passed in the way of copy in the process of passing parameters. It is just that the address stored in the stack area is exchanged as a copy, but the data is not exchanged in the heap.
Oracle officials also said that java is not passed by reference, only by value of value type and by value of reference type. To put it bluntly, it is only passed by value, and there is no other way to pass references. However, there are reference types in C + +. Let's look at a piece of code:
#include <iostream> using namespace std; int main(int agv, char** args) { int num1 = 1, num2 = 2; cout << "Before exchange " << "num1: " << num1 << "\tnum2: " << num2 << endl; swap(num1, num2); cout << "After exchange " << "num1: " << num1 << "\tnum2: " << num2 << endl; } void swap(int& num1, int& num2) { int temp = num1; num2 = num1; num2 = temp; }
The results are displayed as:
This is a reference type transfer in C + +, and the parameters passed are indeed references to data
The value exchange operation in the address can also be carried out in C + + or by using the pointer, but it is relatively troublesome (in fact, this is the operation in C). The output result is the same as that above
#include <iostream> using namespace std; int main(int agv, char** args) { int num1 = 1, num2 = 2; cout << "Before exchange " << "num1: " << num1 << "\tnum2: " << num2 << endl; swap(num1, num2); cout << "After exchange " << "num1: " << num1 << "\tnum2: " << num2 << endl; } void swap(int* num1, int* num2) { int temp = *num1; *num1 = *num2; *num2 = temp;
There is also reference passing in C# and the keyword ref is required. The code is as follows
static void Main(string[] args) { int num1 = 1, num2 = 2; Console.WriteLine("Before exchange: num1: {0}\tnum2: {1}", num1, num2); swap(ref num1, ref num2); Console.WriteLine("After exchange: num1: {0}\tnum2: {1}", num1, num2); Console.ReadKey(); } static void swap(ref int num1, ref int num2) { int temp = num1; num1 = num2; num2 = temp; }
The output result is:
Indeed, there was an exchange
After talking so much, I'm going to run to the question. At this time, someone may think that the title is the title of Java. Why do you say so many things in other languages? Don't worry
But what if Java doesn't have these two functions?
We can use the array method. Anyway, we try to exchange the data in the heap or use the return value method, but if we return, we can only return one data (unlike C # which can return multiple data, some people will think, how can we do anything like java), but the fact is that, what can we do...
public static void main(String[] args) { int[] n1 = {1}, n2 = {2}; System.out.println("Before exchange: num1: " + n1[0] + "\tnum2: " + n2[0]); swap(n1, n2); System.out.println("After exchange: num1: " + n1[0] + "\tnum2: " + n2[0]); } public static void swap(int[] num1, int[] num2) { int temp = num1[0]; num1[0] = num2[0]; num2[0] = temp; }
So, java can only exchange like this