Java.math.BigDecimal.hashCode()方法


java.math.BigDecimal.hashCode() 返回此BigDecimal的雜湊程式碼。兩個BigDecimal物件是數值上相等,但不同的規模(比如2.0和2.00),一般不會有相同的雜湊碼。

宣告

以下是java.math.BigDecimal.hashCode()方法宣告

public int hashCode()

重寫

  • hashCode in class Object

引數

  • NA

返回值

此方法返回BigDecimal物件的雜湊碼值

異常

  • NA

例子

下面的例子顯示math.BigDecimal.hashCode()方法的用法

package com.yiibai;

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