JDK-4879804 : unsigned data types in Java
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-06-17
  • Updated: 2003-06-18
  • Resolved: 2003-06-18
Related Reports
Duplicate :  

Name: rmT116609			Date: 06/17/2003

I've seen that there have been several requests for unsigned integral types in Java, and you have generally blown these off in a fairly cavalier manner.  Usually you report that these types are unnecessary and can be worked around.  In some cases you are correct, and I can write code that chews up CPU time masking and shifting bits around, using the char type as an unsigned temporary bucket, etc.  However, I'm dealing with a stream of bytes that contains numeric values.  I have to grab a few bytes at a time and make a number out of them - an integer, a long, whatever.  Obviously, some of those bytes may have the high-order bit set, not because they are negative numbers but because that's how they fit in that particular byte in the sequence.  Java's insistence on treating each byte as a signed value, and therefore doing sign-extension for me, is a major pain.
What this all boils down to is this: as a language vendor you should not be continually telling your user community that they are wrong when they request support from the language.  Support the unsigned types because it is a reasonable thing to do, and stop pontificating.  This is a major oversight in the language, and quite frankly, I'm amazed that you think it's okay to waste CPU run time rather than CPU compile time!

Support the unsigned integral data types.
See above.  Sometimes requires significant work-arounds.

Bit shifting, masking.
(Review ID: 187835) 

EVALUATION If you want to more conveniently convert bytes to int or long, consider using nio byte buffers and viewing the byte buffer as an int or long buffer instead. A parallel family of unsigned types was deliberately omitted from Java to avoid the confusion of combining signed and unsigned values in arithmetic expressions. That said, there are times when having unsigned arithmetic operations would be convenient. However, if the bits of an int or long are interepreted as unsigned two's complement numbers, the output of add/subtract and multiply are the same as if the numbers are interpreted as signed values (this is a features of two's complement arithmetic). Therefore, if you track signed-ness yourself, you only need a separate unsigned divide method and some conversion methods to have all the operations. Closing as a duplicate of a previously submitted unsigned rfe. ###@###.### 2003-06-17