日期:2014-05-20  浏览次数:20643 次

2个题搞得有点糊涂
Exhibit:
1.   public   class   X   {
2.   public   static   void   main   (String[]args)   {
3.   string   s   =   new   string   (“Hello”);
4.   modify(s);
5.   System.out.printIn(s);
6.   }
7.
8.   public   static   void   modify   (String   s)   {
9.   s   +=   “world!”;
10.   }
11.   }
What   is   the   result?
E.   The   program   runs   and   prints   “Hello”
F.   An   error   causes   compilation   to   fail.
G.   The   program   runs   and   prints   “Hello   world!”
H.   The   program   runs   but   aborts   with   an   exception.
Answer:   A

Exhibit:
1.   public   class   X   {
2.   public   static   void   main   (String[]args)   {
3.   int   []   a   =   new   int   [1]
4.   modify(a);
5.   System.out.printIn(a[0]);
6.   }
7.
8.   public   static   void   modify   (int[]   a)   {
9.   a[0]   ++;
10.   }
11.   }
What   is   the   result?
A.   The   program   runs   and   prints   “0”
B.   The   program   runs   and   prints   “1”
C.   The   program   runs   but   aborts   with   an   exception.
D.   An   error   “possible   undefined   variable”   at   line   4   causes   compilation   to   fail.
E.   An   error   “possible   undefined   variable”   at   line   9   causes   compilation   to   fail.
Answer:   B

希望能够详细说说,希望把知识点说到

------解决方案--------------------
1.String类型的对象是常量,不可以被更改
2.当定义一个int数组时,如果未被赋值,则默认值为0,a[0]++,以后a[0]的值就变为1
------解决方案--------------------
两者传递的都是对象的引用,两个方法都没有改变原来的对象的引用指向,但是第一个由于java中的String是final的,所以s += “world!”会创建一个新的String对象,对原来对象的内容没有改变,第二个则改变了对象的内容。
------解决方案--------------------
看《javw2全方位学习》里面有详细解答
------解决方案--------------------
这两道题的实质是考察参数传递。所有参数的传递都是对象副本的引用。
先看第一题:
string s = new string (“Hello”);将s传递给方法 modify其实将s的副本传递给了方法 modify。在方法 modify中, s += “world!”;这个s不是string s = new string (“Hello”);中的s。 s += “world!”;中的s其实string s = new string (“Hello”);的副本,所以的它的改变不会影响对象本质的改变。所以已经是Hello。
第二题:
int [] a = new int [1],树组a传递给方法modify(a);而方法modify中a[0] ++;此时的a[0]就变成了1。因为通过a[0] ++;在int [] a = new int [1]中,对象a已经是实质的改变。
也许这样的回答LZ还不能明白,最好看看传输传递这一章
------解决方案--------------------
java只有参数传值调用,所以A无法改变String对象的值;
然而数组比较特殊,在参数传递过程中是传地址,所以值发生了变化。
------解决方案--------------------
第一个很好理解...

JAVA中..函数传递的都是对象的副本,是值传递.没有传递地址,即 改变的只是在modify方法中新建的s,main下的s并没有改变 .沙发上说string出来的是常量比较搞笑

第二个我也没整明白...上面有人解释得无法自圆其说
按理说JAVA中的传递都是对象的副本传递.....
------解决方案--------------------
第二題的詳細解釋是什麽呀
------解决方案--------------------
1.String类型的对象是常量,不可以被更改
2、对于对象类型,也就是Object的子类,如果你在方法中修改了它的成员的值,那个修改是生效的