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


Java Java.math.BigDecimal.hashCode()用法及代碼示例


描述

這個java.math.BigDecimal.hashCode()返回此 BigDecimal 的哈希碼。數值相等但規模不同(如 2.0 和 2.00)的兩個 BigDecimal 對象通常不會具有相同的哈希碼。

聲明

以下是聲明java.math.BigDecimal.hashCode()方法。

public int hashCode()

覆蓋

類中的哈希碼Object

參數

NA

返回值

此方法返回 BigDecimal 對象的 hashCode 值。

異常

NA

示例

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

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 3 BigDecimal objects
      BigDecimal bg1, bg2, bg3;

      // create 3 int objects
      int i1, i2, i3;
   
      bg1 = new BigDecimal("125");
      bg2 = new BigDecimal("125.50");
      bg3 = new BigDecimal("125.80");

      // assign the HashCode value of bg1, bg2, bg3 to i1, i2, i3
      // respectively
      i1 = bg1.hashCode();
      i2 = bg2.hashCode();
      i3 = bg3.hashCode();

      String str1 = "HashCode of " + bg1 + " is " + i1;
      String str2 = "HashCode of " + bg2 + " is " + i2;
      String str3 = "HashCode of " + bg3 + " is " + i3;

      // print i1, i2, i3 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

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

HashCode of 125 is 3875
HashCode of 125.50 is 389052
HashCode of 125.80 is 389982

相關用法


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