Topic: 挺绕人的问题 |
Print this page |
1.挺绕人的问题 | Copy to clipboard |
Posted by: javabt Posted on: 2004-05-02 17:46 public class A{ int i=11; public void go(int s){ s=23; System.out.println(s); System.out.println(i); } public static void main(String[] args){ A a=new A(); a.go(a.i); System.out.println(a.i); } } //output is: 23 // 11 // 11 我觉得输出应该都是23啊; go()函数内的s不是a.i吗 a.go(a.i)不是通过把a.i改成了23吗 |
2.Re:挺绕人的问题 [Re: javabt] | Copy to clipboard |
Posted by: midlet Posted on: 2004-05-03 10:14 this is a simple question, and the output is expectable. please notice that 'int' is one of the primitive data type, and it is not a Object Reference. When you pass an int as a parameter, you are actually pass the real data (or you can think it as the real object) but not its reference. |
3.Re:挺绕人的问题 [Re: javabt] | Copy to clipboard |
Posted by: tangming Posted on: 2004-05-03 13:38 primitive 是传值的, 不是传reference的。 |
4.Re:挺绕人的问题 [Re: javabt] | Copy to clipboard |
Posted by: huangjiacai Posted on: 2004-05-04 12:38 第一个是23你知道 第二个是11: 因为A a=new A();时候就定义了i=11;System.out.println( i );也是class A中的方法呀(int i=11在总个class A中可见呀)所以第二个是11了 第三个是11: 因为a.i其实就是引用class A中i属性呀,而在A a=new A();是就定义了其为11呀,所以打印出11 希望一楼的朋友去看看变量“作用域”和“生存期”。 |
5.Re:挺绕人的问题 [Re: huangjiacai] | Copy to clipboard |
Posted by: taogang Posted on: 2004-05-06 12:43 huangjiacai wrote: 同意, 如果把int 换成 Integer 会更有迷惑性, 可能出乎楼主的意料吧 |
6.Re:挺绕人的问题 [Re: javabt] | Copy to clipboard |
Posted by: javabt Posted on: 2004-05-06 20:11 public class A{ Integer i=new Integer(11); public void go(Integer s){ s=new Integer(23); System.out.println; System.out.println; } public static void main(String[] args){ A a=new A(); a.go(a.i); System.out.println(a.i); } } 输出一样对吧 |
7.Re:挺绕人的问题 [Re: javabt] | Copy to clipboard |
Posted by: kam Posted on: 2004-05-07 08:42 Object is store in heap and reference is in stack, when passing object, it will only pass reference, but if pass primitive type, it will actually pass the value as it is in the stack. ^.^ |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |