public class test {
public static void stringReplace (String text)  {
     text = text.replace ('J' , 'I');
}
 
public static void bufferReplace (StringBuffer text){
     text = text.append ("C");
}
  
public static void main (String args[]){
     String textString = new String ("Java");
     StringBuffer textBuffer = new StringBuffer ("Java");
         stringReplace (textString);
     bufferReplace (textBuffer);
         System.out.println (textString + textBuffer);
     }
}
在这个题目中输出的是JavaJavaC这到底是为什么呢?如果说是因为方法是无返回值的,那也应该是JavaJava啊,要不两个都改变应该是IavaJavaC啊,怎么一个改变另外一个不改变呢?
第2个问题,
public class test1 {

     public static void add3 (Integer i){
      int val = i.intValue ( );
      val += 3;
      i = new Integer (val);
 }
 public static void main (String args[]) {
      Integer  i = new Integer (0);
  add3 (i);
      System.out.println (i.intValue ( ) );
  }}
输出是0我觉得和第一个问题应该是同一原因不知道是不是第3个问题
public class Foo {
 public static void main (String [] args)  {
             StringBuffer a = new StringBuffer ("A");
         StringBuffer b = new StringBuffer ("B");
         operate (a,b);
         System.out.println(a +","+b);
 }
static void operate(StringBuffer x, StringBuffer y)  {
     x.append(y);
     y = x;
    }
 }输出的是AB,B
但我觉得这个是不是也应该是AB,AB啊
这三个问题我的感觉好象是同一类型的,我分析半天不知道是什么原因!希望高手指点一二不胜感激
我在线等了!

解决方案 »

  1.   

    第一个问题:
    String 类型是不可变的,而StringBuffer类型是可变的。故对String类型的取代操作是无效的.
      

  2.   


    更正下:因为是传值,方法里对参数i的操作只是i的拷贝,不会对原来的i有任何影响。
      

  3.   

    public class test1 {     public static void add3 (Integer i[]){ 
          int val = i[0].intValue ( ); 
          val += 3; 
          i[0] = new Integer (val); 

    public static void main (String args[]) { 
          Integer[]  i = new Integer[1]; 
           i[0] =  new Integer(0);
      add3 (i); 
          System.out.println (i[0].intValue ( ) ); 
      } }