Is Java the absence of unsigned primitive types characteristic of a Java platform or Java language?

There are questions about why Java does not support unsigned types and a few questions about working with unsigned types. I did some searches, and it seems that Scala also does not support unsigned data types. Is there a limitation in the language design of Java and Scala, in the generated bytecode, or in the JVM itself? Could there be some language that runs on the JVM and is otherwise identical to Java (or Scala) but supports unsigned primitive data types?

+8
java types language-design unsigned
source share
3 answers

Only the Java bytecode specification defines signed types :

Integral types are short, int, and long bytes whose values ​​are 8-bit, 16-bit, 32-bit, and 64-bit signed integers with two additions

But a language implemented on top of the JVM may possibly add an unsigned type at the syntax level and simply handle the conversion at compile time.

+8
source share

Although the unsigned type can be emulated at the bytecode level, there are some disadvantages:

  • Performance. For each simple arithmetic operation, you will need several bytecode operations. Code performance using emulated unsigned types would be two to three times worse than code using signed types.

  • Compatibility. Most JVM languages ​​try very hard to be compatible with a huge amount of Java code. This, of course, will be corrupted immediately after the introduction of additional types or when some variables with "known" types will be processed differently.

In light of this, the benefits for unsigned IMHO types are negligible.

+3
source share

Unsigned arithmetic processing is a language / implementation problem, not a platform - it can be modeled on any platform, even if there was no built-in support.

The JVM does not have it as a type, therefore Java / Scala / etc. do not support it out of the box.

+2
source share

All Articles