Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The BigInteger class represents integers as immutable objects.</p> <p>There are two points here.</p> <ol> <li>Don't use <code>==</code> to test if two <code>BigInteger</code>s are equal</li> <li>To change the value of a BigInteger variable you must do <code>i = i.add(ONE);</code> and not just <code>i.add(ONE);</code>.</li> </ol> <p>The reason not to use <code>==</code> to compare <code>BigInteger</code>s is because instead of checking for numerical equality you are checking that they are the same object in memory.</p> <p>Consider with <code>String</code>s.</p> <pre><code>String a = "a"; String b = "b"; String x = a + b; String y = "ab"; </code></pre> <p>In the above example <code>x.equals(y)</code> is true because they contain the same number of characters in exactly the same order. However, <code>x == y</code> is not true because they are different objects in memory.</p> <p>The reason you need to to assign the result of arithmetic operations to a variable is because BigInteger is immutable. Thus arithmetic operations cannot change the value of the object it is operating on, but it can create a new BigInteger (which it returns). Which is why you must assign the result of the arithmetic operation to the variable you want it saved in.</p> <p>As an aside a shortcut for your lesserThanOrEqual to is this.</p> <pre><code>boolean result = m.compareTo(n) &lt;= 0; </code></pre> <p>Basically </p> <ul> <li><code>m == n</code> becomes <code>m.compareTo(n) == 0</code></li> <li><code>m != n</code> becomes <code>m.compareTo(n) != 0</code></li> <li><code>m &lt; n</code> becomes <code>m.compareTo(n) &lt; 0</code></li> <li><code>m &gt; n</code> becomes <code>m.compareTo(n) &gt; 0</code></li> <li><code>m &lt;= n</code> becomes <code>m.compareTo(n) &lt;= 0</code></li> <li><code>m &gt;= n</code> becomes <code>m.compareTo(n) &gt;= 0</code></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload