有这样一段代码:
......
String s=new String("abc");
Object o=new Objece();
System.out.println(s);//打印出“abc”,不能获得引用
System.out.println(o);//打印出对象o的引用
......
请问那我怎样才能获得对象s的引用,各位大侠帮帮我!

解决方案 »

  1.   

    你的意思是想获得指向“abc”那个引用s的引用吧? java不能。
      

  2.   

    直接用=赋值对象就是引用.
    但String对象是final的,用来说明引用不好.
    看看下面的StringBuffer:
    StringBuffer sb=new StringBuffer("sb");
    StringBuffer bs=sb;
    bs.append(":bs");
    System.out.println(sb);结果打印:
    sb:bs
      

  3.   

    这样可以:
    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()));
    }
    }
      

  4.   

    看一下Object类的toString()方法
      

  5.   

    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
      

  6.   

    我觉得,我们所能得到的对象的地址,可以通过其hashcode来体现。
    如果两个对象的hashcode相同,那么可以确定,两个对象就是同一个对象。
    但是:
    It is a popular misconception that hashCode provides a unique identifier for an object. It does not.
    这句话是从网上看到的。
    所以,对于以上的理解,我们还需要深入的学习和了解。
      

  7.   

    Object打印出来的不是地址引用吧;只是hashCode而已同意
    回复人: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()));
    }
    }
      

  8.   

    打印的只是hascode.真正的引用地址是看不到的!
      

  9.   

    hashCode默认返回的是虚拟机地址,当然不可能是实际的内存地址。   
      但是可以认为他是Java对象的内存地址,任何时候都可以获得这个值   
        
      Object   o=new   XXX();   
      System.out.println(System.idendityHashCode(o));