问题:关于别名效应
首先我想先问一下:
String a =new String( "abc "),String b =new String( "abc ") a和b不相等这我知道,为什么
String a= "abc ",String b= "abc " a和b相等呢,有什么不一样啊?
关于别名效应:
public class Alias {
private int i;
public Alias(int ii) { i = ii; }
public static void main(String[] args) {
Alias x = new Alias(7);
Alias y = x; // Assign the reference
System.out.println( "x: " + x.i);
System.out.println( "y: " + y.i);
System.out.println( "Incrementing x ");
x.i++;
System.out.println( "x: " + x.i);
System.out.println( "y: " + y.i);
}
}
结果是:x:7
y:7
x:8
y:8
这就是别名效应,y=x后,改变x的内容,y也改变
按我的理解 以下代码:
public class Alias {
public static void main(String[] args) {
String a =new String( "abc ");
String c = a;
System.out .println(a);
System.out .println(c);
a= new String( "sdf ") ;
System.out .println(a);
System.out .println(c);
}
}
结果应该是:abc
abc
sdf
sdf
因为String 也是个对象啊,但结果是:
abc
abc