1, Data type
Data types in Java are divided into two categories: basic types and object types. Accordingly, there are two types of variables: basic type and reference type.
The basic type of variable saves the original value, that is, the value it represents is the value itself;
A variable of reference type holds a reference value. The "reference value" points to the address of the memory space and represents the reference of an object, not the object itself. The object itself is stored at the address represented by the reference value.
Basic types include: byte,short,int,long,char,float,double,Boolean,returnAddress,
Reference types include: class type, interface type and array.
2, Value transfer
When a method is called, the actual parameter passes its value to the corresponding formal parameter. The function receives a copy of the original value. At this time, there are two equal basic types in memory, namely, the actual parameter and the formal parameter. The operations in the following methods modify the value of the formal parameter without affecting the value of the actual parameter.
public class Swap { public static void main(String[] args) { int x = 10; int y = 20; swap(x, y); System.out.println("x(2) = " + x); System.out.println("y(2) = " + y); } public static void swap(int x, int y) { int temp = x; x = y; y = temp; System.out.println("x(1) = " + x); System.out.println("y(1) = " + y); } } /*output x(1) = 20 y(1) = 10 x(2) = 10 y(2) = 20 */
The main function calls the swap function to exchange the values of x and y. however, after calling the function, it is found that the values of x and y in main are not exchanged.
Because X and Y in the main function are placed in different areas from X and Y in swap, only the values of X and Y in main will be assigned to the swap function when calling the swap function. When swap function exchanges X and y, it only exchanges the values in swap and does not change the value of main
3, Reference passing
It is also called a forwarding address. When a method is called, the reference of the actual parameter (address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and the function receives the memory address of the original value;
During method execution, the contents of formal parameters and arguments are the same and point to the same memory address. The operation on references during method execution will affect the actual object.
public static void main(String[] args) { int []a={10,20}; System.out.println("a[0] :"a[0]+"a[1] : "+a[1]);//a[0]=10,a[1]=20; swap(a, 0, 1); System.out.println("a[0] :"a[0]+"a[1] : "+a[1]);//a[0]=20,a[1]=10; } public static void swap(int []a,int i,int j){ int temp=a[i]; a[i]=a[j]; a[j]=temp; System.out.println("a[0] :"a[0]+"a[1] : "+a[1]);//a[0]=20,a[1]=10; } //output /*a[0]=10 a[1]=20 a[0]=20 a[1]=10 a[0]=20 a[1]=10 */
It can be seen from the figure that in swap, only the address of the array is obtained, and the elements of the array are not copied. The operation on the array in swap is a direct operation on the array in the main function. Therefore, after the swap function returns, the values of a [0] and a [1] in the main function are exchanged.
Interview questions
Here, special consideration should be given to String, Integer, Double and other basic type packaging classes. Because there is no function to modify itself, each operation generates a new object. Therefore, special treatment should be given. It can be considered as a value transfer operation similar to the basic data type.
public class Demo { public static void main(String[] args) { //demo1 String str=new String("hello"); char []chs={'w','o','r','l','d'}; change(str, chs); System.out.println(str+" "+new String(chs)); //------------------------------------------------- //demo2 StringBuffer sb=new StringBuffer("hello"); change(sb); System.out.println(sb); } public static void change(StringBuffer sb) { sb.append(" world"); } public static void change(String str,char[]chs) { str.replace('h', 'H'); chs[0]='W'; } } //output /* hello World hello world */