Reputation: 4633
int SIZE = 512;
p = new BigInteger(SIZE, 15, new Random());
q = new BigInteger(SIZE, 15, new Random());
r = new BigInteger(SIZE, 15, new Random());
n = p.multiply(q);
temp1=n;
n = n.multiply(r);
if (temp1.multiply(r)!=n) System.out.println("test");
My code here prints test
when it shouldn't. Why?
Upvotes: 0
Views: 496
Reputation: 22243
According to this implementation:
public BigInteger multiply(BigInteger val) {
if (val.signum == 0 || signum == 0)
return ZERO;
int[] result = multiplyToLen(mag, mag.length,
val.mag, val.mag.length, null);
result = trustedStripLeadingZeroInts(result);
return new BigInteger(result, signum == val.signum ? 1 : -1);
}
temp1.multiply(r)
returns a new BigInteger
object, which will have a different address than n
.
Use !temp1.multiply(r).equals(n)
.
Upvotes: 2
Reputation: 48444
You have to use equals
to compare Object equality.
!=
or ==
compare references.
BigInteger b0 = new BigInteger("0");
BigInteger b1 = new BigInteger("0");
System.out.println(b0 != b1);
System.out.println(!b0.equals(b1));
Output
true
false
Upvotes: 5