In the book of P. Nouton, G. Shildt "Java 2. The most complete guide" says the following about the size of data types:
The width (or the number of bits allocated for storing values) of an integer type cannot be thought of as the amount of memory it occupies, but rather, as the behavior that it defines for variables and expressions of this type. The Java runtime is free to use whatever size it wants, while the types behave according to their declaration. There is at least one implementation of the execution environment that stores the
byte
andshort
numbers as 32-bit (not 8-bit and 16-bit) values to improve efficiency, because this value expresses the word size of most computers currently used.
Elsewhere in this book it says:
It may seem that using
short
orbyte
saves memory, but there is no guarantee that Java will not internally somehow extend these types toint
. Remember that type determines behavior, not size. (The only exception is arrays, where the typebyte
guarantees the use of only one byte per array element, whileshort
will take two bytes andint
- four bytes).
In connection with the above, I had a question: what is meant by the word behavior of variables of integer type and is it true that for variables not declared as arrays, the size is not defined?