BigDecimal API docs andd "reuse"

Discussion in 'Java' started by Sebastian Millies, Jun 17, 2005.

  1. 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
     
    Sebastian Millies, Jun 17, 2005
    #1
    1. Advertising

  2. Sebastian Millies

    Murray Guest

    "Sebastian Millies" <REPLACE-OBVIOUSsDOTmilliesATidsMINUSscheerDOTde> wrote
    in message news:19chcy2d5870y$.1eapuyx54dihu$...
    > 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.
     
    Murray, Jun 17, 2005
    #2
    1. Advertising

  3. Sebastian Millies

    Murray Guest

    "Murray" <> wrote in message
    news:42b2a137$0$13943$...
    >
    > "Sebastian Millies" <REPLACE-OBVIOUSsDOTmilliesATidsMINUSscheerDOTde>

    wrote
    > in message news:19chcy2d5870y$.1eapuyx54dihu$...
    > > 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.


    Actually, a correction. It seems a new BigDecimal is returned, it's the
    BigInteger that is cached
     
    Murray, Jun 17, 2005
    #3
  4. Sebastian Millies wrote:
    > 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

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Jun 17, 2005
    #4
  5. Sebastian Millies

    Val Guest

    Sebastian Millies wrote:
    > 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
     
    Val, Jun 17, 2005
    #5
  6. Sebastian Millies

    Murray Guest

    "Val" <> wrote in message
    news:...

    > 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.
     
    Murray, Jun 17, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    5
    Views:
    535
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    422
    Hylander
    Feb 26, 2004
  3. Replies:
    3
    Views:
    1,233
  4. quadraticformula

    confusion with pointers andd arrays

    quadraticformula, Jan 3, 2007, in forum: C++
    Replies:
    3
    Views:
    349
    Pete Becker
    Jan 5, 2007
  5. Stanimir Stamenkov
    Replies:
    4
    Views:
    2,591
    Stanimir Stamenkov
    Jul 18, 2008
Loading...

Share This Page