关于java变量引用的注意!!!
当你在main方法里定义变量
一种 基础数据类型 Integer Long
另外 就是 引用类和自定义类
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Integer a = new Integer(5);
modify(a);
System.out.println(a);
}
private static void modify(Integer b) {
b = 10;
}
这样书写 之后的输出还是五 方法里的b已经不是别名了 而是新开辟的空间存的是10
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Coordinate a = new Coordinate(1, 1);
modify(a);
System.out.println(a.x);
}
private static void modify(Coordinate b) {
b.x = 10;
}
static class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
当你这么写的时候 输出的才是10 因为此时b是a的别名 b指向的空间就是 虚拟机为a开辟的地方
里面存的是1; 然后修改b 也就是修改a 所以才输出10
还有注意一点 静态方法调内部类的时候 要么是静态内部类 要么是把内部类放到外面
}