class StringTest {
 static int x =2;
 static int y=3;
public static void change( int x, int y){
StringTest.x = x + y;
StringTest.y = x - y;
StringTest.x = x - y;
                   }
public static void main(String []args) {
                           StringTest.change(StringTest.x,StringTest.y);
System.out.println(StringTest.x);
System.out.println(StringTest.y);
}
}
本来想将两个数互换,由于基本数据类型是传值的,不是传引用,所以就这样写了,但为什么还是不能两个数互换,结果竟然是-1,我怎么想也想不通 请高手们指点迷津,

解决方案 »

  1.   

    public static void change( int x, int y){
    StringTest.x = x + y;
    // 既然你知道不是传引用,那么你改变了StringTest.x,又怎么会影响到x呢?
    StringTest.y = x - y;
    StringTest.x = x - y;
    }1)按照你思路:
    // 如果x、y之和超出int的域,肯定出错。
    public static void change( int x, int y){
    x = x + y;
    y = x - y;
    x = x - y; StringTest.y = y;
    StringTest.x = x;
    }2)正解(你可别哭):
    public static void change( int x, int y){
    StringTest.y = x;
    StringTest.x = y;
    }
      

  2.   

    StringTest.y = x - y;
    中的StringTest.y和y不是一个东西
      

  3.   

    你传参数的时候只是用了一下 StringTest.x 和 StringTest.y的值 在使用的时候
    你确一直在改变 StringTest.x StringTest.y,而x,y这两个值一直没有边,所以结果就很
    显然了 就是2-3
      

  4.   

    class StringTest {
     static int x =2;
     static int y=3;
    public static void change( int x, int y){
    StringTest.x = x + y;
    StringTest.y = StringTest.x - y;
    StringTest.x = StringTest.x - StringTest.y;
                       }
    public static void main(String []args) {
                            StringTest.change(StringTest.x,StringTest.y);
    System.out.println(StringTest.x);
    System.out.println(StringTest.y);
    }
    }这样做是对的,
    我对这个概念也不是很清楚,但尝一下我这样做是对的
    楼主原代码中这一句:
    StringTest.x = x + y;是将x+y的值赋于了StringTest.x,下一句直接用x-y,而不是StringTest。那么疑问来了,既然是static x,y,那么StringTest.x和x为什么不是一个东西呢?
    请高手朋友们给解释一下。
      

  5.   

    to: xunmeng_hot() 
    楼主已经说了:由于基本数据类型是传值的,不是传引用
      

  6.   

    StringText.x 与实参x 不是一回事
    在改变的过程中 实参始终没有改变值
      

  7.   


    public static void change(int, int);
      Code:
       0:   iload_0//从局部变量表中取出索引为0的整数值压入栈就是change()的参数x为2
       1:   iload_1//从局部变量表中取出索引为0的整数值压入栈就是change()的参数y为3
       2:   iadd//x+y
       3:   putstatic //将和赋给StringTest.x      #2; //Field x:I
       6:   iload_0//从局部变量表中取出索引为0的整数值压入栈就是change()的参数x为2
       7:   iload_1//从局部变量表中取出索引为0的整数值压入栈就是change()的参数y为3
       8:   isub//x-y
       9:   putstatic //将差赋给StringTest.y      #3; //Field y:I
       12:  iload_0//从局部变量表中取出索引为0的整数值压入栈就是change()的参数x为2
       13:  iload_1//从局部变量表中取出索引为0的整数值压入栈就是change()的参数y为3
       14:  isub//x-y
       15:  putstatic //将差赋给StringTest.x      #2; //Field x:I
       18:  return可以看出,方法内部的局部变量会屏蔽掉类中的成员变量
      

  8.   

    尽量作到变量名字不一样,既是为了程序的正确,也是为了可读性
    public static void change( int x, int y){
    StringTest.x = x + y;
    StringTest.y = x - y;
    StringTest.x = x - y;
                       }
    如果直接使用x,y,默认是方法内部的局部变量
    除非给jvm一个明确的指示要用类的内部变量如:StringTest.x
      

  9.   


    public static void change( int x, int y){
    StringTest.x = x + y; // x=2,y=3 引用被传入,StringTest.x = x+y ,StringTest.x=5 StringTest.y = x - y; //x= 2,y=3 还是引用,StringTest.y = 2-3 
    StringTest.y = -1
    StringTest.x = x - y; //同上                   }x ,y和StringTest.x ,StringTest.y是两码事,一个只是引用传入,作用域只有在这个方法内,一个是作用域在整个类