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

new methods in Byte, Short, Integer, and Long to properly handle negatives

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • windows_2000



      Name: rmT116609 Date: 09/02/2004


      A DESCRIPTION OF THE REQUEST :
      Byte.parseByte("FF", 16) throws a NumberFormatException. This seems rather illogical since "FF" is a perfectly respectable byte value of -1. The behavior of parseByte, parseShort, parseInt, and parseLong is such that a leading minus sign is required when parsing negative numbers. So parseByte("-1", 16) works but parseByte("FF", 16) does not.

      A handy enhancement would be to overload parseByte etc. with something like this:

      byte parseByte(String s, int radix, boolean acceptTwosCompliment)

      So parseByte("FF", 16) or parseByte("FF", 16, false) would throw NumberFormatException, completely preserving backward compatability, but parseByte("FF", 16, true) would return -1. Of course there would be similar enhancements to Short, Integer, and Long.

      JUSTIFICATION :
      The API should provide a simple way to, e.g, parse string "FF" into a byte. Currently, one would have to call parseInt and manipulate the result into a byte.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      As mentioned above:

      parseByte("FF", 16, true) should return -1 and
      parseByte("FF", 16) should continue to throw NumberFormatException.

      CUSTOMER SUBMITTED WORKAROUND :
      Use parseInt and examine the result.
      (Incident Review ID: 301049)
      ======================================================================

            darcy Joe Darcy
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: