This article brings you a detailed introduction to Java’s call by value and call by reference. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. Helps.
The topic is as follows:
public class TestJavaCallBy { // 测试值传递 public static void testCallByValue(int value) { value *= 10; } // 测试引用传递 public static void testCallByReference(StringBuilder sb) { sb.append(" - " + ZonedDateTime.now()); } public static void main(String[] args) { // 测试值变量 int param = 1; // 测试引用对象 StringBuilder sb = new StringBuilder("666"); // 测试 System.out.println(param); testCallByValue(param); System.out.println(param); System.out.println(sb.toString()); testCallByReference(sb); System.out.println(sb.toString()); } } 控制台输出结果 1 // 结果仍为1,因为参数传递的是实参的一份拷贝,对实参不造成影响 1 666 // 传递的是引用的拷贝,新旧两个引用指向同一个对象(地址),因此append操作是作用在该对象上 666 - 2019-03-16T01:25:57.933038500+08:00[Asia/Shanghai]
The above is the detailed content of Detailed introduction to Java's call by value and call by reference. For more information, please follow other related articles on the PHP Chinese website!