- Java.math 包扩展
- Java.math - 枚举
- Java.math - 讨论
Java.math.BigDecimal.hashCode() 方法
描述
java.math.BigDecimal.hashCode() 返回此 BigDecimal 的哈希码。数值相等但刻度不同的两个 BigDecimal 对象(如 2.0 和 2.00)通常不会有相同的哈希码。
声明
以下是 java.math.BigDecimal.hashCode() 方法的声明。
public int hashCode()
重写
类 Object 中的 hashCode。
参数
无
返回值
此方法返回 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
让我们编译并运行以上程序,将产生以下结果 −
打印页面