Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is BigInteger immutable or not?

In .NET 4 beta 2, there is the new Numerics namespace with struct BigInteger. The documentation states that it is an immutable type, as I would have expected.

But I'm a little confused by the post-increment operator (++). This defintely seems to mutate the value. The following while-loop works:

static BigInteger Factorial(BigInteger n)
{
    BigInteger result = BigInteger.One;
    BigInteger b = BigInteger.One;

    while (b <= n)
    {
        result = result * b;
        b++;  // immutable ?
    }
    return result;
}

This is what MSDN has to say about the Increment operator:

Because BigInteger objects are immutable, the Increment operator creates a new BigInteger object whose value is one more than the BigInteger object represented by value. Therefore, repeated calls to Increment may be expensive.

All well and fine, I would have understood if I had to use b = b++ but apparently ++ by itself is enough to change a value.

Any thoughts?

like image 671
Henk Holterman Avatar asked Oct 28 '09 08:10

Henk Holterman


People also ask

Is BigInteger thread safe?

BigInteger is immutable and as such it is safe for access by multiple threads. Side-note: Thread-safety is a term that deals with multiple threads accessing a piece of code (not just a variable).

Which data types are immutable in Java?

In Java, an immutable class is a class (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) which once created then its body can not be changed and the same applies to immutable objects which once created cannot be changed.

Does BigInteger have a limit?

The BigInteger class allows you to create and manipulate integer numbers of any size. The BigInteger class stores a number as an array of unsigned, 32-bit integer "digits" with a radix, or base, of 4294967296. The class stores the digits in little-endian order, with the most-significant digit at the end of the array.

Is Java BigDecimal immutable?

Since BigDecimal is immutable, these operations do not modify the existing objects. Rather, they return new objects.


1 Answers

The operators ++ and -- are implemented in terms of the normal + and - operators, so in reality:

b++;

is equivalent to:

var temp = b;
b = b + 1;
<use temp for the expression where b++ was located>

Now, as commented, this might seem like it breaks immutability, but it does not.

You should instead look at this code as doing this:

var temp = b;
b = BigInteger.op_Add(b, 1); // constructs a new BigInteger value
<use temp ...>

This will leave two objects in memory, the original BigInteger value, and the new one, now referenced by b. You can easily check that this is what happens with the following code:

var x = b;
b++;
// now inspect the contents of x and b, and you'll notice that they differ

So the original object did not change, hence it does not break immutability, and to answer the new part of the question, this should be thread-safe.

This is the same thing that happens to strings:

String s1 = s2;
s2 += "More";
// now inspect s1 and s2, they will differ
like image 115
Lasse V. Karlsen Avatar answered Nov 03 '22 17:11

Lasse V. Karlsen