基本类型传值,Object传引用
解决方案 »
- 是我错了还是oracle出问题了大家帮忙看一下
- Java 运行不了,出现"Error: could not find java.dll" 怎么解决?
- 【大家一起帮忙找】个人关于J2EE的笔记,原创
- 做个JAVA记事本如何拖文件进去
- 谁有在非阻塞NIO的方式下的实现读超时的代码范例
- 请教关于线程的问题
- java中有没有像IE怎样的组件啊?
- ------送100分------applet问题------送100分------
- 各位大哥大姐关于SE的求解下
- 我想学java,但不知怎么架设学习平台,操作系统包括winX和Linux,请各位指点?
- jbuilder的配置问题----初学者请求帮助
- VISUAL J++在XP上的安装
C语言中,传递实参用的是地址,像你们说的,地址也是数值,也要在内存中占4个byte,所以也是传值????不太通吧,好像曲解了传参方式的意思。
不过C++里的引用传递不是这样的呀,主要是JAVA的对象表示方法和C++不同,我倒是觉得这样比较好理解。
这句话是这个意思:由于句柄本身是按值传递的,所以即使在方法中改变了句柄的引用,使它指向了其他的对象,从方法返回后,它还是要指向原来的对象的。
class ValHold{
public int i = 10;
}public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i );
}
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}
}返回到amethod()后v.i是20而不是10,就是由于这个原因。
我要散分了。
其它对象传引用
其它对象传引用
1 String 对象,包括引用地址(指向2)
2 ‘xxxx’在jvm的地址
jvm会在启动时将所有字符串"ab"都统一分配地址
所以当你用 a = b = 'xxx' ;时 a和b都是指向XXX的地址
即使对a 的值修改,也指挥生成新的字符串地址,不会影响其他的引用
-----------
"Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object", the auter give below reference:
--------------------
This can be a flashpoint. There are those who say “clearly, it’s a pointer,” but this presumes an underlying implementation. Also, Java references are much more akin to C++ references than pointers in their syntax. In the first edition of this book, I chose to invent a new term, “handle,” because C++ references and Java references have some important differences. I was coming out of C++ and did not want to confuse the C++ programmers whom I assumed would be the largest audience for Java. In the 2nd edition, I decided that “reference” was the more commonly used term, and that anyone changing from C++ would have a lot more to cope with than the terminology of references, so they might as well jump in with both feet. However, there are people who disagree even with the term “reference.” I read in one book where it was “completely wrong to say that Java supports pass by reference,” because Java object identifiers (according to that author) are actually “object references.” And (he goes on) everything is actually pass by value. So you’re not passing by reference, you’re “passing an object reference by value.” One could argue for the precision of such convoluted explanations, but I think my approach simplifies the understanding of the concept without hurting anything (well, the language lawyers may claim that I’m lying to you, but I’ll say that I’m providing an appropriate abstraction.)
In the first edition of this book, I chose to invent a new term, “handle,” because C++ references and Java references have some important differences.
/**
C++ references and Java references have some important differences.
C++ references and Java references have some important differences.
*/