5

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?

5
  • My buest guess is, that they are directly mapped to the C-types en.cppreference.com/w/c/types/integer
    – hellow
    Jul 30, 2018 at 12:53
  • they are primitives because they aren't considered objects and represent raw values Jul 30, 2018 at 12:54
  • Also, it will be interesting for you to read about boxing and unboxing in Java. Maybe) Jul 30, 2018 at 12:58
  • aren't these mapped to jint, jlong data types and so on?
    – Eugene
    Jul 30, 2018 at 13:08
  • 1
    Answer: It depends. (Since there is not just only one implementation of the Java VM.) Jul 30, 2018 at 13:09

3 Answers 3

2

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.

1

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.

2
  • How can primitive types be "written" in any programming language? You can write operations on them, but you cannot write the types themselves, that's why they are primitive. Jul 30, 2018 at 13:07
  • 1
    @Dukeling See stackoverflow.com/questions/1220914/…
    – bradimus
    Jul 30, 2018 at 13:09
1

The primitive data types are defined in the Java Language Specification. This means that any JVM fulfilling the spec will handle them the same, regardless matter in which language the JVM is written. So,

  1. the primitive types are not written, the JVM is.

  2. if the JVM is open source, you can access the files implementing the specified behavior.

8
  • 1
    aren't these mapped to jint, jlong and so on?
    – Eugene
    Jul 30, 2018 at 13:10
  • @Eugene you can use those if you write in C or C++, but I don't think there are golang jint/jlong etc.
    – daniu
    Jul 30, 2018 at 13:14
  • @RealSkeptic the OP did ask for "good resources to learn more about this" in the original question, but it has since been edited out so I deleted mine as well.
    – daniu
    Jul 30, 2018 at 13:16
  • golang? I am confused... there is a definition in the VM sources jlong.h that includes jlong_md.h - this last file depends on linux/windows environment
    – Eugene
    Jul 30, 2018 at 13:19
  • @Eugene I'm saying that if I were to hypothetically implement a software in golang which fully complied with the JLS, that would be a valid JVM, no? And neither dependent on jlong.h nor jlong_md.h.
    – daniu
    Jul 30, 2018 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.