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?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
abadawi
  • 378
  • 1
  • 5
  • 15

3 Answers3

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.

memo
  • 1,868
  • 11
  • 19
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.

Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • 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. – RealSkeptic Jul 30 '18 at 13:07
  • 1
    @Dukeling See https://stackoverflow.com/questions/1220914/in-which-language-are-the-java-compiler-and-jvm-written – bradimus Jul 30 '18 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.

daniu
  • 14,137
  • 4
  • 32
  • 53
  • 1
    aren't these mapped to `jint`, `jlong` and so on? – Eugene Jul 30 '18 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 '18 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 '18 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 '18 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 '18 at 13:22
  • you only made it worse with this comment :( `JLS` compliance is != an implementation detail, like `jint`. `JLS` != `JVMLS` – Eugene Jul 30 '18 at 13:37
  • @Eugene Your comments are not making it better either :/ how is `jint` not an implementation detail? – daniu Jul 30 '18 at 13:46
  • you're right, my bad. I don't think that the `JLS` stipulates how a `long` for example is defined internally, neither is the `JVMLS` (*I think*). This *is* the implementation details of `jlong`. So the answer to the question IMO is - they are mapped to `jint` and `jlong` - but this is not specified anywhere in the specification. – Eugene Jul 30 '18 at 13:49