在Java中值类型和引用类型是如何进行传递的,这个是我今天要谈论话题。
关于这个话题,我的一个好友的推荐下看了一篇文章,这才使我有了写这篇文章的想法。这篇文章是《 Pass-by-Value Please 》 (http://www.javaranch.com/campfire/StoryPassBy.jsp) ,顺便再向大家推荐一个网站,就是 http://www.javaranch.com ,一个很好的 Java 乐园。
好了,回到正题吧~免得一下又跑远了~呵呵·
关于值类型,我想大家都很清楚了,总的说来就是 Copy the value and give it to you 。
请看如下代码:
package blog.nichlas.article.j2se;
import static java.lang.System.out;
public class PassByValue {
public static void testPassValue(){
int a = 2 ;
int b = a ;
out.println("a= " + a ) ;
out.println("b= " + b ) ;
out.println("=============== the value has changed ===============");
a = 3 ; // has change the value of a, but tht value of b is never be changed
out.println("a= " + a ) ;
out.println("b= " + b ) ;
}
public static void main(String [] args){
out.println();
PassByValue.testPassValue();
}
}
结果如下:
a= 2
b= 2
=============== the value has changed ===============
a= 3
b= 2
这个例子很明显,不用过多的解释, a 和 b 已经是两个独立的,互补相干的值。
下面进入我们今天的重点,关于引用类型。
我这里想借鉴一下我开篇提到的那篇文章,里面提到的杯子, I like the cap, do you?
在这里,我想打一个比方,把我们杯子里的引用 Reference 作一个类似,在操作系统里,有一个很重要的概念,那就是进程控制块,他是进程实体的一部分,是操作系统中最重要的记录型数据结构,他记录了操作系统所需的,用于描述进程的当前情况以及控制进程运行的全部信息。同样,引用也是如此,只是没有这么复杂罢了。引用里存放了 java object 的相关信息,以便让 JVM (Java Virtual Machine) 在堆中找到对象 (Java 对象全部在堆中 ) 。
我们可以把这种情况想象成在一个杯子里放了一个类似于进程控制块的 reference ,下面来看如下代码:
package blog.nichlas.article.j2se;
import static java.lang.System.out;
public class PassByValue {
public static void testPassValue(){
int a = 2 ;
int b = a ;
out.println("a= " + a ) ;
out.println("b= " + b ) ;
out.println("=============== the value has changed ===============");
a = 3 ; // has change the value of a, but tht value of b is never be changed
out.println("a= " + a ) ;
out.println("b= " + b ) ;
}
public static void testPassReference(){
ObjectForTest o1 = new ObjectForTest("Object one");
ObjectForTest o2 = o1;
out.println(o1.getInfo());
out.println(o2.getInfo());
out.println("=============== the value has changed ===============");
o1 = new ObjectForTest("Object two");
out.println(o1.getInfo());
out.println(o2.getInfo());
}
public static void main(String [] args){
out.println();
PassByValue.testPassValue();
out.println();
PassByValue.testPassReference();
}
}
class ObjectForTest{
String info ;
public ObjectForTest(){
info = "default";
}
public ObjectForTest(String initInfo){
info = initInfo ;
}
public String getInfo(){
return info;
}
public void setInfo(String changeInfo){
info = changeInfo;
}
}
运行结果是:
a= 2
b= 2
=============== the value has changed ===============
a= 3
b= 2
Object one
Object one
=============== the value has changed ===============
Object two
Object one
这里可以很明显的表示出其实所谓的 Reference 其实就跟值类型是很相似的,所以我们可以给 Reference 取一个名字“引用值”。
这里只是把 o1 的引用值拷贝了一份给 o2 。
不过这里要注意的就是虽然是引用值的拷贝,但他们却引用了同一个对象,任何一个对对象的改变都会,在另外一个引用上都可以显示的出来。
看如下的例子:
package blog.nichlas.article.j2se;
import static java.lang.System.out;
public class PassByValue {
public static void testPassValue(){
int a = 2 ;
int b = a ;
out.println("a= " + a ) ;
out.println("b= " + b ) ;
out.println("=============== the value has changed ===============");
a = 3 ; // has change the value of a, but tht value of b is never be changed
out.println("a= " + a ) ;
out.println("b= " + b ) ;
}
public static void testPassReference(){
ObjectForTest o1 = new ObjectForTest("Object one");
ObjectForTest o2 = o1;
out.println(o1.getInfo());
out.println(o2.getInfo());
out.println("=============== the value has changed ===============");
o1 = new ObjectForTest("Object two");
out.println(o1.getInfo());
out.println(o2.getInfo());
}
public static void testChangeObject(){
ObjectForTest o1 = new ObjectForTest("Object one");
ObjectForTest o2 = o1;
out.println(o1.getInfo());
out.println(o2.getInfo());
out.println("=============== the value has changed ===============");
o1.setInfo("Object Value changed!");
out.println(o1.getInfo());
out.println(o2.getInfo());
}
public static void main(String [] args){
out.println();
PassByValue.testPassValue();
out.println();
PassByValue.testPassReference();
out.println();
PassByValue.testChangeObject();
}
}
class ObjectForTest{
String info ;
public ObjectForTest(){
info = "default";
}
public ObjectForTest(String initInfo){
info = initInfo ;
}
public String getInfo(){
return info;
}
public void setInfo(String changeInfo){
info = changeInfo;
}
}
结果如下:
a= 2
b= 2
=============== the value has changed ===============
a= 3
b= 2
Object one
Object one
=============== the value has changed ===============
Object two
Object one
Object one
Object one
=============== the value has changed ===============
Object Value changed
Object Value changed
从这里可以看到, o1 对对象的改变体现在了 o2 上了,通过对 o2 的调用可以看出。
以上是我对这个问题的看法,希望大家可以给一点帮助。