Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 590
abadawi Avatar asked Jul 30 '18 12:07

abadawi


2 Answers

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.

like image 164
memo Avatar answered Sep 28 '22 17:09

memo


So, int, double are not the class in java. Those are the keyword. And the definition of those keyword written in the native language (I believe c,C++).

1) What language are primitive data types in java written in?

-> I believe those are written n C/C++;

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

-> There is no .java the file you can find for that.

like image 29
Amit Bera Avatar answered Sep 28 '22 19:09

Amit Bera