I was going through the book "Java-The Complete Reference" and I came across a sentence which says that
"The width of an integer type should not be thought of as the amount of storage it consumes,but rather as the behavior it defines for variables and expressions of that type. The java run-time environment is free to use whatever size it wants,as long as the types behave as you declared them".
Does this mean the java is free to choose the size of the integer data type which is typically of 4 bytes?
Primitive data types in Java are predefined by the Java language and named as the reserved keywords. A primitive data type does not share a state with other primitive values. Java programming language supports the following eight primitive data types.
What is the incorrect statement about primitive data types? Primitive data types can not be directly used in any program. We have to create a user-defined data type and then use it. Primitive data types can be used to create non-primitive or user-defined data types.
A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: byte: The byte data type is an 8-bit signed two's complement integer.
"The width of an integer type should not be thought of as the amount of storage it consumes,
A JVM may store your typical Java 16-bit short
in 32 bits on 32-bit implementations to improve performance..
but rather as the behavior it defines for variables and expressions of that type. The java run-time environment is free to use whatever size it wants,as long as the types behave as you declared them".
.. but for a Java developer, it will behave like a 16-bit signed two's complement integer, regardless of how it's physically stored in the computer.
This makes perfect sense. You want your program to run on any JVM, so all JVMs need to treat your program and its integers the same way. But how the JVM is implemented doesn't matter to a Java developer, so it allows for optimizations that are specific for the architecture.
As @BasilBourque notes in the comments: This is the basic idea behind all of the specifications for the Java language and for the JVM: Define the required behaviors but leave the implementation details to the implementer. There have been many implementations of Java over the years, all behaving the same but varying quite dramatically as to what's “under the hood”.
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