How are the primitive data types in java defined/written?

abadawi :

For example, is there such a thing as a int.java or a double.java?

Since primitive data types are the building blocks of the Java language, I assume they would be written in some lower-level language.

  1. What language are primitive data types in Java written in?

  2. Can one access those files and see how primitive data types such as int are defined?

memo :

No, there is no such thing as int.java or double.java These low level types are hard-coded at the JVM level. They are primitive types even at byte-code level (e.g. different bytecodes apply to operands of 4 bytes as opposed to 8 bytes, or to integral types as opposed to floating point types). You may want to study the implementation of one or more open source Java VMs to see how they are handled (https://en.wikipedia.org/wiki/List_of_Java_virtual_machines#Free_and_open_source_implementations). Also, even compilers treat them specially and make certain assumptions about them, e.g. for optimization purposes.

The other reason for them not having equivalent Java classes (unlike the capitalized ones, java.lang.Integer, java.lang.Double etc.) is that they don't have methods of their own, that you can call, for example with the dot notation. When you do use a primitive type as an object, a conversion takes place (known as autoboxing -- https://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#Autoboxing) where the primitive type is converted to one of the wrapper types, transparently for the programmer. It is worth having a look at the source code of these classes, for some interesting internal functionality.

Also you cannot create instances of them, by using the new keyword. The compiler or the VM allocate space for them, as necessary, on the stack, as object or class fields, or even directly in the code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=69033&siteId=1