System.out.println(new Student());我知道输出的是引用,引用不是内存地址是吗,Student@de6ced,引用的后几位是什么啊如何算的啊,谢谢

解决方案 »

  1.   

    这里会调用toString()方法,结果就是
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
      

  2.   

    Object.hashCode() 就是32位jvm内存地址。应该没有啥花样。
    String.hashCode() 算法有点不一样,忘记具体了。
      

  3.   

    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;
        }
      

  4.   

    我想问一下上面的hashCode算法是哪里的啊,jdk里面的hashcode是本地方法阿
      

  5.   

    你如果要重写hashCode一般遵循质数加加减减乘乘除除什么的以减少hash算法的冲突
      

  6.   

    到 OpenJDK 下载 OpenJDK 的源代码,解压后找到 hotspot/src/share/vm/runtime 目录,里面有个 synchronizer.cpp 文件,找到:intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
      if (UseBiasedLocking) {
        // NOTE: many places throughout the JVM do not expect a safepoint
        // to be taken here, in particular most operations on perm gen
        // objects. However, we only ever bias Java instances and all of
        // the call sites of identity_hash that might revoke biases have
        // been checked to make sure they can handle a safepoint. The
        // added check of the bias pattern is to avoid useless calls to
        // thread-local storage.
        if (obj->()->has_bias_pattern()) {
          // Box and unbox the raw reference just in case we cause a STW safepoint.
          Handle hobj (Self, obj) ;         
          // Relaxing assertion for bug 6320749.
          assert (Universe::verify_in_progress() ||
          !SafepointSynchronize::is_at_safepoint(),
         "biases should not be seen by VM thread here");
          BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
          obj = hobj() ; 
          assert(!obj->()->has_bias_pattern(), "biases should be revoked by now");
        }
      }  // hashCode() is a heap mutator ...
      // Relaxing assertion for bug 6320749.
      assert (Universe::verify_in_progress() ||
      !SafepointSynchronize::is_at_safepoint(), "invariant") ; 
      assert (Universe::verify_in_progress() ||
      Self->is_Java_thread() , "invariant") ; 
      assert (Universe::verify_in_progress() ||
     ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;  ObjectMonitor* monitor = NULL;
      Oop temp, test;
      intptr_t hash;
      Oop  = ReadStableMark (obj);  // object should remain ineligible for biased locking 
      assert (!->has_bias_pattern(), "invariant") ; 
     
      if (->is_neutral()) {
        hash = ->hash();              // this is a normal header
        if (hash) {                       // if it has hash, just return it
          return hash;
        }
        hash = get_next_hash(Self, obj);  // allocate a new hash code
        temp = ->copy_set_hash(hash); // merge the hash code into header
        // use (machine word version) atomic operation to install the hash
        test = (Oop) Atomic::cmpxchg_ptr(temp, obj->_addr(), );
        if (test == ) {
          return hash;
        }
        // If atomic operation failed, we must inflate the header
        // into heavy weight monitor. We could add more code here
        // for fast path, but it does not worth the complexity.
      } else if (->has_monitor()) {
        monitor = ->monitor();
        temp = monitor->header();
        assert (temp->is_neutral(), "invariant") ; 
        hash = temp->hash();
        if (hash) {
          return hash;
        }
        // Skip to the following code to reduce code size
      } else if (Self->is_lock_owned((address)->locker())) {
        temp = ->displaced__helper(); // this is a lightweight monitor owned
        assert (temp->is_neutral(), "invariant") ; 
        hash = temp->hash();              // by current thread, check if the displaced
        if (hash) {                       // header contains hash code
          return hash;
        }
        // WARNING:
        //   The displaced header is strictly immutable.
        // It can NOT be changed in ANY cases. So we have 
        // to inflate the header into heavyweight monitor
        // even the current thread owns the lock. The reason
        // is the BasicLock (stack slot) will be asynchronously 
        // read by other threads during the inflate() function.
        // Any change to stack may not propagate to other threads
        // correctly.
      }  // Inflate the monitor to set hash code
      monitor = ObjectSynchronizer::inflate(Self, obj);
      // Load displaced header and check it has hash code
       = monitor->header();
      assert (->is_neutral(), "invariant") ; 
      hash = ->hash();
      if (hash == 0) {
        hash = get_next_hash(Self, obj);
        temp = ->copy_set_hash(hash); // merge hash code into header
        assert (temp->is_neutral(), "invariant") ; 
        test = (Oop) Atomic::cmpxchg_ptr(temp, monitor, );
        if (test != ) {
          // The only update to the header in the monitor (outside GC)
          // is install the hash code. If someone add new usage of
          // displaced header, please update this code
          hash = test->hash();
          assert (test->is_neutral(), "invariant") ; 
          assert (hash != 0, "Trivial unexpected object/monitor header usage.");
        }
      }
      // We finally get the hash
      return hash;
    }这个函数基本上就在这了,原始的 hashCode 在 jdk/src/share/native/java/lang/Object.c 这个文件中,调来调去就调到上面那个函数去了。OpenJDK 6 的下载页面:http://download.java.net/openjdk/jdk6/ 上面有个 46.9MB 的 tar.gz 文件,下载回来后是整个 JDK(JVM、JDK 工具和 J2SE 类库和底层类库)的源代码,解压后有 254MB,大约有 28750 个文件。
      

  7.   

    谢谢上面的bao110908,真的非常感谢