设为首页 加入收藏

TOP

如何理解java的参数传递
2014-11-24 00:35:39 来源: 作者: 【 】 浏览:4
Tags:如何 理解 java 参数 传递

Java的参数传递方式就只有一种:值传递。


以前在Java的面试题中,就见过这样一个问题:Java的参数传递是值传递还是引用传递?其实这题是问那些从C++转向Java的程序员的,因为在C++中,有一种叫指针的东西,而在Java的世界中,屏蔽了容易让程序员犯错的指针(其实内部只是封装了指针,很多功能的实现其实还是通过指针的),也因此让问题变得更加简单,在Java参数传递中,只存在值的传递,不存在引用传递。


先来看一个例子:


public class Test {


public static void main(String[] args) {


int i = 1 ;


Test test = new Test() ;


test.change(i) ;


System.out.println(i) ;


}


public void change(int i) {


i += 2 ;


System.out.println(i) ;


}
}


这个程序会依次输出3和1。这是已经说明了Java的参数传递是值传递。


值传递的意思就是说,参数在传递过程中,传递的是参数的一个副本,而不是引用本身。


上面的例子可能不足以说明问题,因为上面的例子传递的是基本类型参数,要是引用类型呢?再看下面的例子:


public class Test {


public static void main(String[] args) {


Apple apple = new Apple() ;


apple.setColor(“red”) ;


apple.setSize(“10″) ;


Test test = new Test() ;


test.change(apple) ;


System.out.println(apple) ;


}


public void change(Apple apple) {


apple.setColor(“green”) ;


apple.setSize(“20″) ;


System.out.println(apple);


}


}


class Apple {


private String color ;


private String size ;


public void setColor(String color) {


this.color = color ;


}


public void setSize(String size) {


this.size = size ;


}


public String getColor() {


return this.color ;


}


public String getSize() {


return this.size ;


}


public String toString() {


return “color = ” + this.color + “, size = ” + this.size ;


}


}


上面这段程序依次输出依次输出: color = green, size = 20 和 color = green, size = 20 。


有人会说,你看change()方法这不明明是改变了之前的apple对象吗?这传递的难道不是引用?这是初学者最难理解的地方。


这传递的还的确是引用,也就是传递的apple对象的内存地址;如果不传递引用的话,难道还传递对象吗?那效率低得都海枯石烂了!


虽然传递的是引用,但是传递的是引用的一个副本!这才是关键。再看下面的例子:


public class Test {


public static void main(String[] args) {


Apple apple = new Apple() ;


apple.setColor(“red”) ;


apple.setSize(“10″) ;


Test test = new Test() ;


test.change(apple) ;


System.out.println(apple) ;


}


public void change(Apple apple) {


Apple apple2 = new Apple() ;


apple2.setColor(“green”) ;


apple2.setSize(“20″) ;


apple = apple2 ;


System.out.println(apple);


}


}


class Apple {


private String color ;


private String size ;


public void setColor(String color) {


this.color = color ;


}


public void setSize(String size) {


this.size = size ;


}


public String getColor() {


return this.color ;


}


public String getSize() {


return this.size ;


}


public String toString() {


return “color = ” + this.color + “, size = ” + this.size ;


}


}


上面的程序依次输出: color = green, size = 20 和 color = red, size = 10 。


看出什么问题了吗?就是无论怎么样都无法改变引用。这充分说明了一个问题,在参数传递过程中,传递的是引用的值,也就是引用的副本。没有任何方法能改变引用本身!这就是Java参数传递方式是值传递的含义。深刻理解Java的参数传递对于排除一些难以发现的bug非常有好处。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇介绍一下Web测试中的链接测试? 下一篇广州-卧龙在线最新题目

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)