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?
It consist both negative and positive values but in different formats like (-1 to -128) or (0 to +127) . An unsigned integer can hold a larger positive value, and no negative value like (0 to 255) . Unlike C++ there is no unsigned integer in Java.
Primitive data types - includes byte , short , int , long , float , double , boolean and char. Non-primitive data types - such as String , Arrays and Classes (you will learn more about these in a later chapter)
Primitive Data Type: In Java, the primitive data types are the predefined data types of Java. They specify the size and type of any standard values. Java has 8 primitive data types namely byte, short, int, long, float, double, char and boolean.
Since the primitive data types consume less memory and can be accessed faster, they are not objects. The equivalent Wrapper classes are also available in java like "Integer" "Short" etc. They can be used as objects if you want. However, the wrapper classes will be stored in Heap and they are slow.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With