BigDecimal类之compareTo()和equals()方法源码分析

书欣 Java经验 发布时间:2023-01-10 08:58:19 阅读数:16558 1
下文笔者讲述BigDecimal对象之compareTo()和equals()方法的源码详解,如下所示

BigDecimal之compareTo()源码

   /**
    * Compares this BigDecimal with the specified BigDecimal. 
    * Two BigDecimal objects that are equal in value but have 
    * a different scale (like 2.0 and 2.00) are considered equal
    * by this method. This method is provided in preference to 
    * individual methods for each of the six boolean comparison 
    * operators(<,<=,==,>,>=,!=). The suggested idiom for 
    * performing these comparisons is: x.compartTo(y) <op> 0, 
    * where <op> is one of the six comparison operators.
    * 
    * @param  val - BigDecimal to which this BigDecimal is to be 
    *         compared.
    * @return -1,0 or 1 as this BigDecimal is numerically less than,
    *         equal to, or greater than val.
    */
public int compareTo(BigDecimal val) {
    // Quick path for equal scale and non-inflated case.
    if (scale == val.scale) {
        long xs = intCompact;
        long ys = val.intCompact;
        if (xs != INFLATED && ys != INFLATED)
            return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
    }
    int xsign = this.signum();
    int ysign = val.signum();
    if (xsign != ysign)
        return (xsign > ysign) ? 1 : -1;
    if (xsign == 0)
        return 0;
    int cmp = compareMagnitude(val);
    return (xsign > 0) ? cmp : -cmp;
}
CompareTo()方法源码说明

将当前BigDecimal实例同给定BigDecimal实例进行比较

注意事项: 
    数值相同但精度不同(例:3.0 和 3.00)两个BigDecimal是相同

本方法的返回值总共有3种
-1:当前BigDecimal数值小于给定BigDecimal
0:当前BigDecimal数值等于给定BigDecimal
1:当前BigDecimal数值大于给定BigDecimal

BigDecimal对象之equals()源码

/**
 * Compares this BigDecimal with the specified Object for equality. 
 * Unlike compareTo, thi method considers two BigDecimal objects equal
 * only if they are equal in value and scale (thus 3.0 is not equal to 3.00
 * when compared by this method).
 * 
 * @param  x - Object to which this BigDecimal is to to compared.
 * @return   - true if and only if the specified Object is a BigDecimal whose 
 *             value and scale are equal to this BigDecimal`s.
 */
@Override
public boolean equals(Object x) {
    if (!(x instanceof BigDecimal))
        return false;
    BigDecimal xDec = (BigDecimal) x;
    if (x == this)
        return true;
    if (scale != xDec.scale)
        return false;
    long s = this.intCompact;
    long xs = xDec.intCompact;
    if (s != INFLATED) {
        if (xs == INFLATED)
            xs = compactValFor(xDec.intVal);
        return xs == s;
    } else if (xs != INFLATED)
        return xs == compactValFor(this.intVal);

    return this.inflated().equals(xDec.inflated());
}
equals源码详解

将当前BigDecimal实例同给定BigDecimal实例进行比较
注意事项: 
  只有数值和精度都相同的两个BigDecimal 才是相同(所以在equals方法中3.0和3.00是不同)

bigdecimal之equals方法的返回值共2种
true:当前 BigDecimal 的数值等于给定的 BigDecimal。
false:当前 BigDecimal 的数值不等于给定的 BigDecimal。

compareTo和equals方法的区别

BigDecimal类compartTo方法和equals方法
   都可用于比较BigDecimal 实例代表的数值的大小
但两个方法的区别:
   compareTo只对值进行判断
   equals对值和精度都进行判断 

例:
public static void main(String[] args) {
    BigDecimal b1 = new BigDecimal("3.0");
    BigDecimal b2 = new BigDecimal("3.00");

    System.out.println("b1.compareTo(b2):" + (b1.compareTo(b2) == 0));
    System.out.println("b1.equals(b2):" + b1.equals(b2));
}
 
b1.compareTo(b2):true
b1.equals(b2):false
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16733131455322.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者