Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java's lack of unsigned primitive types a characteristic of Java the platform or Java the language?

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?

like image 229
Thomas Owens Avatar asked Dec 01 '11 17:12

Thomas Owens


People also ask

Does Java support unsigned integers?

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.

Which of the following is not a primitive data type 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)

What is primitive types in Java?

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.

Why primitive data types in Java are not objects?

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.


2 Answers

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.

like image 71
Dmitry B. Avatar answered Nov 09 '22 13:11

Dmitry B.


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.

like image 40
A.H. Avatar answered Nov 09 '22 13:11

A.H.