Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8183912

java.math.BigDecimal.movePointLeft() should return this if called with zero argument

XMLWordPrintable

    • b01

      A DESCRIPTION OF THE REQUEST :
      BigDecimal.movePointLeft(int n) does not check if it is called with a zero argument, and will unnecessarily create a copy of the receiver, instead of returning this.

      JUSTIFICATION :
      Programmers who want to avoid unnecessary object creation now have to add additional branching:

      return (scale>0)? b.movePointLeft(scale):b;

      after the fix, this becomes

      return b.movePointLeft(scale);

       


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      BigDecimal.movePointLeft(int n), when called with an argument of zero, should return the receiver.
      ACTUAL -
      BigDecimal.movePointLeft(int n), when called with an argument of zero, currently instantiates a copy of the receiver.


      ---------- BEGIN SOURCE ----------
         
       @Test
          public void createsNewInstance() {

              BigDecimal receiver = new BigDecimal("123.45");
              BigDecimal result = receiver.movePointLeft(0);

              assertNotSame(receiver, result);
          }

      ---------- END SOURCE ----------

        1. JI9049870.java
          0.2 kB
          Pallavi Sonal
        2. MovePointLeftZero.java
          2 kB
          Brian Burkhalter

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: