Java and java
When calling a function, the passed parameter may have been a value or an address. If it is a value transfer, the operation inside the function does not affect the value of the parameter; if it is an address transfer, the operation inside the function is to operate on the memory pointed to by the parameter, it will affect the value of the parameter.
Is Java A value or an address? Use the following example to perform an experiment:
package test;import java.util.ArrayList;import java.util.List;public class ValueAndAddress { public static void f1(int x){ x = x + 5; return; } public static void f2(String s){ s = new String("10000"); return; } public static void f3(List<Integer> list){ list.remove(0); list.remove(1); list.remove(2); return; } public static void main(String[] args) { int x = 5; String s = new String("abcdef"); List<Integer> list = new ArrayList<Integer>(); list.add(0); list.add(1); list.add(2); list.add(3); list.add(4); f1(x); f2(s); f3(list); System.out.println("x=" + x); System.out.println("s=" + s); for (int i=0; i<list.size(); i++){ System.out.println("list[" + i + "]=" + list.get(i)); } }}
Running result:
It can be seen that the parameter values of the int and String types are not changed, but the parameter values of the List type are changed.