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,我怎么想也想不通 请高手们指点迷津,
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,我怎么想也想不通 请高手们指点迷津,
解决方案 »
- linux 安装出问题 u盘安装
- FileInputStream/Out跟Reader/Writer这2对哪对什么时候用?
- 一个导入的项目,eclipse不认为web项目,又没有object Deploy的选项,怎么解决??
- think in java 第四版错误
- 刚学java请帮助!!!
- 高分求助,也是connection reset by peer:JVM_recv in socket input stream read
- 在java中怎样对一个四则运算表达式(如1+2*3)求值?
- 相当菜鸟的问题,得到答案马上给分。
- 那里有MYSQL的JDBC驱动程序
- paint和repaint函数的区别,一定给分
- 一个关于focusLost问题的处理
- 谁来谈一谈,Java是如何捕获鼠标或者键盘事件的,谢谢
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;
}
中的StringTest.y和y不是一个东西
你确一直在改变 StringTest.x StringTest.y,而x,y这两个值一直没有边,所以结果就很
显然了 就是2-3
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为什么不是一个东西呢?
请高手朋友们给解释一下。
楼主已经说了:由于基本数据类型是传值的,不是传引用
在改变的过程中 实参始终没有改变值
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可以看出,方法内部的局部变量会屏蔽掉类中的成员变量
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
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是两码事,一个只是引用传入,作用域只有在这个方法内,一个是作用域在整个类