Data Types in JVM
Data types are essential for dealing with the memory area(class loader components) which is used to store the data during the run time. Since the data are stored n the memory area. One key aspect you should remember is that the data types of JVM differ from data types in java, but most of them are almost equal. In order to store the values that the variable store or the reference to the object or an array, it has to handle the JVM data type.
JVM operates two kinds of data types namely primitive data type and reference data type. The difference is that the primitive data types hold the value itself such as integer, float, char whereas the reference data type just contains the reference to the value also known as pointer such as String.(address or pointers of an object) Primitive data type. Both the primitive data and reference values are stored in the memory area of JVM. An object can be an instance of a class or an interface or simply an array.
The below image illustrates the Data type of Java Virtual Machine
The data types in JVM is divided into two, which are primitive data type and reference data type. Primitive data type holds the value itself, whereas the reference data type stores the value’s location (more details in the latter part). The primitive data types are divided into numeric values, further divided into character(which consist of char), integral part(consider of integer, byte, short, int, long), and floating points, further divided into float and double. Numeric values are also divided into Boolean and return addresses. The return address is used to implement the final block. The reference data type is further divided into class data type, interface data type, array data type, null data type
Primitive Data Type
All the language primitive data types are dealt with the same compared to JVM primitive data types unless when it comes to Boolean data types.
In JVM, Boolean data types are managed with either int or byte. Whereas Boolean false is considered zero and Boolean true is considered non-zero.
An operator Boolean is represented by an int value. while arrays are accessed using byte array instructions. byte, char, short data types are also compiled to use int data type.
JVM Boolean data type has limited instruction support for it. when the java source file is used to convert to bytecode file by the compiler, it uses int or byte data type to present Boolean. Arrays of Boolean are accessed as arrays of bytes although they may be repented on heap as bit field or array of byte
The data type size is the same in every environment (in every language). but the difference is long. Long always take 64bit 2’s compliment as its size doesn’t change based on the platform(independent of the underlying host platform). In every implementation, it takes the same size.
The other primitive data type is the return address type, where developers have no access. This data type is specially dedicated to JVM. This data type is used to implement the final block. (final keyword = to prevent making changes).
JVM has an instruction set for handling the actual java programming data type with JVM data type. It has limited support for the byte, char, short and Boolean data types.
Reference Data Type
Reference data type does not store the value itself therefore as a result it stores only the reference to the actual value it self.(Memory location ). In other words it just a holder that is used to store the address of where the object is located. Additionally reference type can also be assigned null stating there is no memory location. Reference data type simply works as a pointer
In JVM there are 4 reference data types which are class reference, interface reference , array reference and null.
The class type reference the instance of class or also known as dynamically created class instance. Array reference is used to simply reference the array it self(which are full-fledged objects in the Java virtual machine). Where as the interface reference is used to reference the class implementation or array that implements the interface(class instances that implement an interface.) and lastly the null reference is used to state that there is no value in it (no value or memory location/ a specific type that is not referenced to anywhere).
Word Size
What is word size? is it a another data type?🤔
No, Word size is not a data type. It is just a base or a unit. In other words it is just a unit for the size of data values in JVM. The length of a word size is decided by the designer of JVM implementation. (We aren't capable of measuring from word size)
Two Rules when defining the length of the word size
- The particular word should be able to hold any primitive data type ex: int a the word unit /base should be able to hold the value of a.
- Two words combined together should be able to hold a floating or double value.
An implementation designer must therefore choose a word size that is at least 32 bits. Mostly data type is chosen to be the size of a native pointer on the host platform. Internal JVM implementation always deals with word base/unit. For example when you take java stack(the place where you store your local variables / methods) When you store your local variables in stack such as primitive data type it is hold/considered as one word unit(inside stack).When placed into the operand stack, a value occupies either one or two words. Word size does not affect the behavior of a program. It is only an internal attribute of a virtual machine implementation
Reference
JVM part 03 — Data Types in JVM. 2016. [video] Directed by K. Dinesh. Youtube: Krish Dinesh.
Docs.oracle.com. 2022. Chapter 2. The Structure of the Java Virtual Machine. [online] Available at: <https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.3> [Accessed 4 March 2022].
Venners, B., 2022. Java Virtual Machine’s Internal Architecture. [online] Artima.com. Available at: <https://www.artima.com/insidejvm/ed2/jvm3.html> [Accessed 4 March 2022].