在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 的调用可以看出。
以上是我对这个问题的看法,希望大家可以给一点帮助。
分享到:
相关推荐
So, where can all these people go to learn what they need to know in order to pass the exam? Now they can go to Software Testing Foundations 2nd Edition, from Rocky Nook's new Computing division. ...
on what I’ve learned so that you can quickly become an effective R programmer. Reading it will help you avoid the mistakes I’ve made and dead ends I’ve gone down, and will teach you useful tools, ...
” 人们可能会给你提供如下的指引:“Turn right after you pass McDonalds.” 或者 “When you see a church on your left-hand side, turn right on the next street.” 通过这些实际情境的模拟对话和相关词汇的...
What you need to learn to pass a Cisco certification exam such as CCNA and what you need to know to survive in the real world are two very different things. The strategies that this book offers weren...
If what you want to accomplish is not supported you should be able to create a subclass of the relevant component and pass it in as a parameter to be used instead of the default implementation.
This means we work closely with ACCA to ensure this Interactive Text contains the information you need to pass your exam. In this Interactive Text, which has been reviewed by the ACCA examination ...
如“她喜欢坐在河边的咖啡馆里”是"She likes sitting in the coffee shop by the river." 而"pass by"意为“经过”,“我没有看到他经过”可以是"I did not see him pass by." 13. sound - 这个词既可作动词表示...
Review how JavaScript’s new features by-pass any limitations of an existing approach Examine the refactoring necessary to go from old to new Demonstrate how JavaScript’s new features work in unison ...
- "My luggage was rejected by security." - "What should I do with my prohibited items?" ### Unit 8: We Cannot Find Our Reserved Car - **情景**: 找不到预订的车辆。 - **常用表达**: - "We cannot find...
= What do you mean by ...? ... 是什么意思? 3. 预习内容: - life的复数是lives - succeed的名词、形容词和副词分别是success、successful、successfully - wonder的同义短语可以是be curious about - mean...
16. "one by one" 表示依次、逐个地,类似短语还有 "year by year" 和 "day by day"。 17. "hold on" 可以表示"不挂断电话"或"抓紧"。 18. "come down" 与 "go up" 相反,表示下来或下降。 19. "help sb. do sth." ...
_.pluck(list, propertyName) A convenient version of what is perhaps the most common use-case for map: extracting a list of property values. _.max(list, [iterator], [context]) Returns the maximum value...
4. **座位设施**:乘客可以了解如何调整座椅(You may adjust your seat back by pressing the button on the arm of your seat.),并知道何时可以使用洗手间(Lavatory is not allowed to use during take-off.)...
- `How many places did you pass by?` 询问经过的地点数量。 - `Chen Jie is trying to be a tour guide for Oliver.` 描述陈洁尝试为Oliver做导游的情景。 这些知识点是PEP六年级上册英语期末复习的重点,学生...
uses artificial intelligence to investigate why students can't pass standardized tests; deploys machine learning to predict which passengers survived the Titanic disaster; and attempts to repair the ...
It’s time to take a deeper dive into what happens when a function is called from an assembly standpoint by exploring some “stack related” registers as well as the contents in the stack. ...
- 了解目的地信息:如天气、文化、习俗等,常用句子:“What's the weather like in Paris in September?”或“Are there any local customs I should be aware of?” 2. 机场流程: - 检票登机:“May I see ...
It can be used to pass data between different parts of the extension or to external systems. 3. **ASObject**: This represents a custom object that can contain properties and methods. It is useful ...