BigDecimal API docs andd "reuse"

S

Sebastian Millies

I am confused about a statement in the API docs for java.math.BigDecimal
in J2SE 1.4.2. They state:

public static BigDecimal valueOf(long unscaledVal, int scale)
Translates a long unscaled value and an int scale into a BigDecimal.
This "static factory method" is provided in preference to a (long, int)
constructor because it allows for reuse of frequently used BigDecimals.

What is meant by "reuse" here? The valueOf method creates a new BigDecmal
based on a new BigInteger. So the API docs seem to refer to reuse by a
client. Am I missing something important here? My usual way of reusing
BigDecimals goes like this:
private static final BigDecimal CENT = new BigDecimal("0.01");
and plugging in the constant whenever I want to compute with cents.

Thanks,
Sebastian
 
M

Murray

Sebastian Millies said:
I am confused about a statement in the API docs for java.math.BigDecimal
in J2SE 1.4.2. They state:

public static BigDecimal valueOf(long unscaledVal, int scale)
Translates a long unscaled value and an int scale into a BigDecimal.
This "static factory method" is provided in preference to a (long, int)
constructor because it allows for reuse of frequently used BigDecimals.

What is meant by "reuse" here? The valueOf method creates a new BigDecmal
based on a new BigInteger. So the API docs seem to refer to reuse by a
client. Am I missing something important here? My usual way of reusing
BigDecimals goes like this:
private static final BigDecimal CENT = new BigDecimal("0.01");
and plugging in the constant whenever I want to compute with cents.

Thanks,
Sebastian

I believe they mean it will act like a cache. e.g. if you call valueOf()
twice using the same arguments, the second time won't return a "new"
BigDecimal, rather the same instance as the first.
 
M

Murray

Murray said:
"Sebastian Millies" <REPLACE-OBVIOUSsDOTmilliesATidsMINUSscheerDOTde> wrote
in message news:[email protected]...

I believe they mean it will act like a cache. e.g. if you call valueOf()
twice using the same arguments, the second time won't return a "new"
BigDecimal, rather the same instance as the first.

Actually, a correction. It seems a new BigDecimal is returned, it's the
BigInteger that is cached
 
T

Thomas Weidenfeller

Sebastian said:
What is meant by "reuse" here? The valueOf method creates a new BigDecmal
based on a new BigInteger.

No, it doesn't have to create a new object. BigDecimals are immutable.
Once an object with a particular value is created, it can't be changed.
This allows to use reuse the same object without the danger of getting
it changed silently behind your back by an other part of your code.

So valueOf() could cache previously generated BigDecimals, and when you
ask for a BigDecimal with the same value, it could give you the
previously cached object, not a new one.

The question is: Does it indeed do this? I have no idea. You would have
to study the source code to figure it out.

/Thomas
 
V

Val

Sebastian said:
I am confused about a statement in the API docs for java.math.BigDecimal
in J2SE 1.4.2. They state:

public static BigDecimal valueOf(long unscaledVal, int scale)
Translates a long unscaled value and an int scale into a BigDecimal.
This "static factory method" is provided in preference to a (long, int)
constructor because it allows for reuse of frequently used BigDecimals.

What is meant by "reuse" here? The valueOf method creates a new BigDecmal
based on a new BigInteger. So the API docs seem to refer to reuse by a
client. Am I missing something important here? My usual way of reusing
BigDecimals goes like this:
private static final BigDecimal CENT = new BigDecimal("0.01");
and plugging in the constant whenever I want to compute with cents.

Thanks,
Sebastian
IMO the documentation for BigDecimal.valueOf(long,int) is misleading.
From JDK 1.4.1_01 source:
public static BigDecimal valueOf(long unscaledVal, int scale) {
return new BigDecimal(BigInteger.valueOf(unscaledVal), scale);
}
So, for version 1.4.x it returns always a new object.
BUT, in JDK 1.5.0_02 one can find:
public static BigDecimal valueOf(long unscaledVal, int scale) {
if (scale == 0) {
if (unscaledVal == 0)
return ZERO;
if (unscaledVal == 1)
return ONE;
if (unscaledVal == 10)
return TEN;
}
return new BigDecimal(BigInteger.valueOf(unscaledVal), scale);
}
Therefore for JDK 1.5 "frequently used BigDecimals" means ZERO, ONE and
TEN. :D
 
M

Murray

return new BigDecimal(BigInteger.valueOf(unscaledVal), scale);

BigInteger#valueOf(long) does cache however, so indirectly there is caching
going on :) You'll always get a new BigDecimal, but the BigInteger contained
within it will/might be reused.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top