RT

解决方案 »

  1.   

    String s = "tt";System.out.println(s.hashCode());
      

  2.   

    不好意思,弄错了,java里得不到地址吧?
      

  3.   

    用str.hashCode();这个方法是可以的,因为String类的父类Object 类定义的 hashCode 方法会针对不同的对象返回不同的整数,这个整数是通过将该对象的内部地址转换成一个整数来实现的
      

  4.   

    但是String类已经改写hashCode方法了
      

  5.   

    1、  String str1=null;
    2、  String str2=null;
    3、  str1 = new Stirng("abc");
    4、  str2 = str1;当程序执行到第三条语句时,JVM 就在堆内存里生成一个String对象,然后将它的首地址分给栈内里的str1 。 执行第四句时,就是将str1 的值(即刚才生成那个对象的首地址)赋给str2。
      

  6.   

    楼上的我要的是象Recursion@7d772e这样的东东:)
      

  7.   

    public class test
    {
    public static void main(String [] args)
    {
    ab a = new ab();
    System.out.println(a);
    }
    }class ab
    {
    public void say()
    {
    System.out.print("Hello!");
    }
    }
      

  8.   

    你要的那东东 在String 对象中是得不到的,
    用一个普通java对象就可以得到
    比如:
          class A{}
          class B{
                    public static void main(String args[])
                    {
                       System.out.println(new A());
                     }
                  }
    普通对象的这个地址是通过他的toString()方法得到的,而String类重写了toString() 。
      

  9.   

    楼主:我看 Recursion@7d772e 也不是地址。
      

  10.   

    The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
      

  11.   

    Recursion@7d772e 中的“7d772e” 就是 hash code 。  但这不懂这个词组的意思。
      

  12.   

    楼上的,呵呵:)
    public native int hashCode();
    Object源码中这个函数并没有实现,而在String中做了下面的
    public int hashCode() {
    int h = hash;
    if (h == 0) {
        int off = offset;
        char val[] = value;
        int len = count;            for (int i = 0; i < len; i++) {
                    h = 31*h + val[off++];
                }
                hash = h;
            }
            return h;
        }
    但是在所覆写的toString()中
    public String toString() {
    return this;
        }
    可见在类String中toString()与hashCode()并没有什么联系
      

  13.   

    Object类中
    public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }
    才真正和hashCode()相关
      

  14.   

    向上转型成Object?呃,似乎不行
      

  15.   

    在Java中得不到地址的 地址就是C中的指针 java与C或C++的区别就在于其屏蔽了指针(即地址) 所以不能得到
      

  16.   

    但是只要你自己写的类没有覆写toString()方法就可以直接print(对象)而得到它的地址
      

  17.   

    hashCode可以说是jvm上面的虚拟内存地址吧
      

  18.   

    同样楼上的观点,hashCode返回只是一个被加工过的地址
      

  19.   

    但是hashCode在String中覆写过了啊