Reputation: 116169
There are questions about why Java doesn't support unsigned types and a few questions about dealing with unsigned types. I did some searching, and it appears that Scala also doesn't support unsigned data types. Is the limition in the language design of Java and Scala, in the generated bytecode, or is it in the JVM itself? Could there be some language that runs on the JVM and is otherwise identical to Java (or Scala), yet supports unsigned primitive data types?
Upvotes: 10
Views: 4546
Reputation: 160191
Handling unsigned arithmetic is a language/implementation issue, not platform--it could be simulated on any platform even if there was no native support.
The JVM doesn't have it as a type, except for 'char', an unsigned 16-bit value, so Java/Scala/etc. don't support it "out of the box".
Upvotes: 0
Reputation: 66263
Although unsigned type might be emulated at the bytecode level there are some drawbacks with that:
Performance: You would need several bytecode operations for each simple arithmetic operation. The performance of code using the emulated unsigned types would be two or three times worse than code using signed types.
Compatibility: Most languages running on a JVM try very hard to be compatible with the huge amount of Java code out there. This of course would be spoiled immediately when additional types are introduced or when some variables with "known" types have to handled differently.
In the light of this the benefits for unsigned types are IMHO negligible.
Upvotes: 2
Reputation: 9337
Java Bytecode Specification only defines signed types:
The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit, and 64-bit signed two's-complement integers
But a language implemented on top of the JVM can probably add an unsigned type at the syntactic level and just handle the conversion at the compilation stage.
Upvotes: 10