【Java】官网中方法的值传递和引用传递
2016-02-23 23:13
615 查看
博主网站:http://andyheart.me
For example, consider a method in an arbitrary class that moves Circle objects:
Let the method be invoked with these arguments:
Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.
Passing Primitive Data Type Arguments(值传递)
Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:public class PassPrimitiveByValue { public static void main(String[] args) { int x = 3; // invoke passMethod() with // x as argument passMethod(x); // print x to see if its // value has changed System.out.println("After invoking passMethod, x = " + x); } // change parameter in passMethod() public static void passMethod(int p) { p = 10; } }
Passing Reference Data Type Arguments(引用传递)
Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object’s fields can be changed in the method, if they have the proper access level.For example, consider a method in an arbitrary class that moves Circle objects:
public void moveCircle(Circle circle, int deltaX, int deltaY) { // code to move origin of circle to x+deltaX, y+deltaY circle.setX(circle.getX() + deltaX); circle.setY(circle.getY() + deltaY); // code to assign a new reference to circle circle = new Circle(0, 0); }
Let the method be invoked with these arguments:
moveCircle(myCircle, 23, 56)
Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.
相关文章推荐
- JAVA JVM 原理
- JAVA JVM 原理
- Spring MVC 框架搭建及详解
- Java多线程Lock对象之公平锁和非公平锁
- Java Reflect 反射 .invoke 示例
- Struts2之拦截器
- 深入浅出Java并发包—CAS机制
- 深入浅出Java并发包—指令重排序
- Java_Ant详解
- 深入浅出Java并发包—原子类操作
- Spring面试题和答案
- Spring+hibernate基本配置
- JAVA_构造函数重载 OverloadingConstructors.java
- JAVA_构造函数重载 OverloadingConstructors.java
- Java EE 工程师系列 之 从后台到页面【最佳实践】
- Java基础I/O流总结
- JAVA正则表达式语法大全
- java正则表达式相关内容整理
- java的拆箱与装箱
- win10 配置 java8