當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.Object.hashCode()用法及代碼示例


描述

這個java.lang.Object.hashCode()方法返回對象的哈希碼值。支持此方法是為了哈希表,例如由 java.util.Hashtable 提供的哈希表。

聲明

以下是聲明java.lang.Object.hashCode()方法

public int hashCode()

參數

NA

返回值

此方法返回此對象的哈希碼值。

異常

NA

示例

下麵的例子展示了 lang.Object.hashCode() 方法的用法。

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class ObjectDemo {

   public static void main(String[] args) {

      // create a new ObjectDemo object
      GregorianCalendar cal = new GregorianCalendar();

      // print current time
      System.out.println("" + cal.getTime());

      // print a hashcode for cal
      System.out.println("" + cal.hashCode());

      // create a new Integer
      Integer i = new Integer(5);

      // print i
      System.out.println("" + i);

      // print hashcode for i
      System.out.println("" + i.hashCode());
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

Sat Sep 22 00:35:34 EEST 2012
-458465658
5
5

相關用法


注:本文由純淨天空篩選整理自 Java.lang.Object.hashCode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。