Primitive data types and portability in Java

I quote from Herbert Schildt Chapter 3 Data Types, Variables, and Arrays:

Primitive types are single values, not complex objects. Although Java is otherwise completely object oriented, there are no primitive types. The reason for this efficiency. Creating primitive types would degrade performance too much.

Primitive types with an explicit range and mathematical behavior are defined. Languages ​​such as C, C ++ allow the integer to vary depending on the requirements of the runtime. However, Java is different. Due to Javas portability requirements, all data types have a very specific range. For example, int is always 32-bit regardless of the specific platform. This allows programs to be executed without porting any machine architecture. Strictly specifying the size of an integer can result in a slight loss of performance in some environments, this is necessary to ensure portability.

What does he mean by the last 2 lines? And how to determine the size of an integer can lead to a small loss of performance in some environments?

+6
source share
4 answers

In lower languages, the sizes of primitive data types are often inferred from the ability of the CPU to process them. For example, in c , a int is defined as “at least 16 bits in size,” but its size may vary by architecture to ensure that “the int type must be the integer type that is most efficient for the target processor”. ( source ). This means that if your code makes careless assumptions about the size of int , it can break down very much if you port it from 32-bit x86 to 64-bit .

, as noted above, is different. int , for example, will always be 32 bits. This means that you don’t have to worry about resizing it when you run the same code in a different architecture. The trade-off, as also mentioned above, is performance — on any architecture that does not require 32-bit processing, these int need to be deployed to its own size, which the processor can handle (which will have a small penalty), or worse, if the processor can only handle smaller int s, each operation in int may require several CPU operations.

+8
source

Several (very few) computers use a 36-bit architecture , so you need an extra step to mask bits, simulate overflow, etc.

+3
source

AFAIK, in java there is no way to determine the size of an integer. This is always a 32-bit int like here . But some programming languages ​​may indicate the size of the integer (Ex: Ada).

A performance problem occurs when the compiler tries to convert our code to machine code instructions (see here and here ). Typically, machine code instructions are 32 or 64 bits. If our ints are the same as size in machine code, it is easy to convert them to machine code. Otherwise, the compiler must make additional efforts to hide them in machine code. This is when the performance problem.

+2
source

Java implements its own data pointer mechanism at the top of the base system pointer mechanism. In case the data is not large enough, many systems can use smaller pointers.

For example: If integer data requires only a 16-bit pointer, the system will allocate only the necessary storage that uses a 16-bit pointer. But if you use Java, it converts 16-bit to 32-bit pointer allocation, where large memory space is required, which also degrades performance in terms of data storage and retrieval . Because your pointer is big .

+2
source

Source: https://habr.com/ru/post/974343/


All Articles