• Post Reply Bookmark Topic Watch Topic
  • New Topic

Effective Java 2nd Edition - item 15  RSS feed

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

can anyone explain me this stanza mentioned in Effective Java 2nd Edition

Not only can you share immutable objects, but you can share their internals.
For example, the BigInteger class uses a sign-magnitude representation
internally. The sign is represented by an int, and the magnitude is represented by
an int array. The negate method produces a new BigInteger of like magnitude
and opposite sign. It does not need to copy the array; the newly created BigInteger
points to the same internal array as the original.


Kindly help.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is about the design of objects; if you have one immutable object, all its fields will be immutable and those fields can be re-used. I presume in the case of an int[] array that means re-used within instances of the same class.
 
ashu Suri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BigInteger is stored in a twos complement form.
And since its signed ie both + and - numbers can be represented. The magnitude and sign are kept at two separate places. The sign of the number is stored in a separate integer and the magnitude of the number is stored in the int array.
So a bigInteger -10 would have - stored in the int for sign and 10 stored in an array on int.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ashu Suri:
The BigInteger is stored in a twos complement form.
. . .
So a bigInteger -10 would have - stored in the int for sign and 10 stored in an array on int.


I am afraid you are mistaken. That's not two's complement. It's sign and magnitude.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the API it is written

All operations behave as if
* BigIntegers were represented in two's-complement notation
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what causes the confusion: "as if".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!