Есть ли более короткая запись сравнения BigDecimal chisl1 >= chisl2, чтобы не писать два условия в if?
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal chisl1 = new BigDecimal(5);
BigDecimal chisl2 = new BigDecimal(5);
if (chisl1.compareTo(chisl2) == 1 || chisl1.compareTo(chisl2) == 0) {
System.out.println("chisl1 >= chisl2");
}
}
}
chisl1.compareTo(chisl2) >= 0
Единственный важный момент: нужно избегать таких сравнений, как
chisl1.compareTo(chisl2) == 1
int compareTo(T o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
compareTo не обязан возвращать 1, если текущий экземпляр больше, он вполне может вернуть другое положительное число. Например, такая реализация вполне корректна:
public class MyByte implements Comparable<MyByte> {
private byte value;
@Override
public int compareTo(MyByte other) {
return this.value - other.value;
}
...
}
Продвижение своими сайтами как стратегия роста и независимости