Why below statement return false?
BigInteger bigInteger = new BigInteger("0");
System.out.println((BigInteger.ZERO == bigInteger));
What should i pass in new BigInteger(?) so condition will be true.
Why below statement return false?
BigInteger bigInteger = new BigInteger("0");
System.out.println((BigInteger.ZERO == bigInteger));
What should i pass in new BigInteger(?) so condition will be true.
By specification, new
always creates a new instance (or it fails).
Whatever instance is assigned to BigInteger.ZERO
, it's not the one you create in your code with new BigInteger("0")
.
Since it's not the same instance, and ==
only returns true if the operands refer to the same instance (provided they are both reference types, which they are in this case), the result is false.
You almost never want to compare objects using a == b
or a != B
. You should use a.equals(b)
or !a.equals(b)
instead. (Or Objects.equals
, if a
might be null).