如下代码:局部变量
public class Pass {
public static void main(String [] args) {int x=5; //申明局部变量
Pass p = new Pass();
p.doStuff(x); //x++是先输出然后++; ++x是先加再输出
System.out.print(" main x = "+ x); //
}
void doStuff(int x) {
System.out.println("doStuff x = "+ x++);
}
}
输出结果:
doStuff x = 5
main x = 5
在main方法体内声明了 x=5 ;(一个方法题就是用一堆花括号里面的内容) 所以在main方法体内类的对象调用外部方法doStuff()先输出5然后在doStuff()方法体内++变成6;在main内x的值没有改变;为了验证该说法 我们在doStuff()里面再次输出X;
public class Pass {
public static void main(String [] args) {int x=5; //申明局部变量
Pass p = new Pass();
p.doStuff(x); //x++是先输出然后++; ++x是先加再输出
System.out.print(" main x = "+ x); //
}
void doStuff(int x) {
System.out.println("doStuff x = "+ x++);
System.out.println("doStuff x = "+ x);
}
}
输出如下:
doStuff x = 5
6
main x = 5