有这样一段代码:
......
String s=new String("abc");
Object o=new Objece();
System.out.println(s);//打印出“abc”,不能获得引用
System.out.println(o);//打印出对象o的引用
......
请问那我怎样才能获得对象s的引用,各位大侠帮帮我!
......
String s=new String("abc");
Object o=new Objece();
System.out.println(s);//打印出“abc”,不能获得引用
System.out.println(o);//打印出对象o的引用
......
请问那我怎样才能获得对象s的引用,各位大侠帮帮我!
但String对象是final的,用来说明引用不好.
看看下面的StringBuffer:
StringBuffer sb=new StringBuffer("sb");
StringBuffer bs=sb;
bs.append(":bs");
System.out.println(sb);结果打印:
sb:bs
package feng;public class Test {
public static void main(String[] args) {
Object o=new Object();
String str=new String("abc");
System.out.println(o);
System.out.println(str.getClass().getName()+"@"+Integer.toHexString(str.hashCode()));
}
}
如果两个对象的hashcode相同,那么可以确定,两个对象就是同一个对象。
但是:
It is a popular misconception that hashCode provides a unique identifier for an object. It does not.
这句话是从网上看到的。
所以,对于以上的理解,我们还需要深入的学习和了解。
回复人:jianfengqu() ( ) 信誉:100 2007-7-25 23:38:03 得分:0
这样可以:
package feng;public class Test {
public static void main(String[] args) {
Object o=new Object();
String str=new String("abc");
System.out.println(o);
System.out.println(str.getClass().getName()+"@"+Integer.toHexString(str.hashCode()));
}
}
但是可以认为他是Java对象的内存地址,任何时候都可以获得这个值
Object o=new XXX();
System.out.println(System.idendityHashCode(o));