In Which Language Are the Java Compiler and Jvm Written

In which language are the Java compiler and JVM written?

The precise phrasing of the question is slightly misleading: it is not "the JVM" or "the compiler" as there are multiple JVM vendors (jrockit is one, IBM another) and multiple compilers out there.

  • The Sun JVM is written in C, although this need not be the case - the JVM as it runs on your machine is a platform-dependent executable and hence could have been originally written in any language. For example, the original IBM JVM was written in Smalltalk

  • The Java libraries (java.lang, java.util etc, often referred to as the Java API) are themselves written in Java, although methods marked as native will have been written in C or C++.

  • I believe that the Java compiler provided by Sun is also written in Java. (Although again, there are multiple compilers out there)

What is Java written in?

Sun actually has multiple JVMs. The HotSpot JVM is written largely in C++, because HotSpot is heavily based on the Animorphic Smalltalk VM which is written in C++.

More interesting than HotSpot is IMHO the Maxine Research VM, which is written (almost) completely in Java.

Why is javac source code is written in java?

From here :

The very first Java compiler developed by Sun Microsystems was written in C using some libraries from C++

Besides the compiled bytecode is interpreted by JVM which is written in c++. From here:

The Oracle JVM, named HotSpot, is written in the C++ language

How can a JVM be written in Java

Your assumption that Java requires a virtual machine is incorrect to begin with. Check out the project GCJ: The GNU Compiler for the Java Programming Language.

Writing languages for the JVM

I'll answer this from two possible scenarios in your development. With any byte-code language at any time you can update the virtual machine or the language.

Suppose first you wanted to update your language to have new syntax or change the current semantics. Then you'd keep your current compiled compiler written in lang (compiler A) and edit its source so that it can correctly compile your new features. Then you compile your compiler using the old one giving you compiler B. If necessary, you can now rewrite the compiler to use the new features and then compile it using compiler B to give you compiler C.

What if the JVM changes? Well in that case you keep an old version of the JVM around, adjust your compiler to cope with the new bytecode changes, and then compile it with the old one (this is analogous to compiler B from before). That will get you a compiler that compiles to the new bytecode but runs on the old VM. The next step is get it to compile itself, and now you have a new compiler that runs on the new VM (analogous to compiler C).

Why jvm is written in c++, any specific reason.?

Java code needs the JVM to execute. However C++ is compiled into machine code, so it is executed more or less by the hardware.

Thus you can see that to write the JVM using java, would mean that you need a JVM to run the JVM... hence not possible..

This is the same with most if not all interpreted languages. They are written in C / C++. Typically C since that was more stable when the language was taking shape (e.g perl), and also because it is seen as being more light weight and faster (?) than C++.

Is the JVM a compiler or an interpreter?

First, let's have a clear idea of the following terms

Javac is Java Compiler -- Compiles your Java code into Bytecode

JVM is Java Virtual Machine -- Runs/ Interprets/ translates Bytecode into Native Machine Code

JIT is Just In Time Compiler -- Compiles the given bytecode instruction sequence to machine code at runtime before executing it natively. It's main purpose is to do heavy optimizations in performance.

So now, Let's find answers to your questions..

1)JVM: is it a compiler or an interpreter? -- Ans: Interpreter

2)what about JIT compiler that exist inside the JVM? -- Ans: If you read this reply completly, you probably know it now

3)what exactly is the JVM? -- Ans:

  • JVM is a virtual platform that resides on your RAM
  • Its component, Class loader loads the .class file into the RAM
  • The Byte code Verifier component in JVM checks if there are any access restriction violations in your code. (This is one of the principle reasons why java is secure)
  • Next, the Execution Engine component converts the Bytecode into executable machine code

Hope this helped you..

Are java packages written in C++?

Yes JVM is mostly written in C and C++.

What is different is not how it is handled by the JVM but how the programmer write the code in C and in Java.

In java a coder doesn't need to know problems related to how to handle memory operations (for example you don't need to now how much space allocate, when in C you need to know, you don't have to handle aritmethic on pointers).

What the garbage collections does for you in java is done manually by the programmer in C (and C++).

This doesn't mean that java is superior to C++. It only means that memory is handled differently by the programmer.

Here a list of Pro and Cons of both solutions:

  • Easy to develop (Pro Java, Cons C++)
  • Decide when to deallocate memory (Pro C++, Cons Java)
  • Decide how to deallocate memory in detail (Pro C++, Cons Java)
  • Possibility to lost to deallocate memory (Pro java, Cons C++)
  • Quantity of memory to handle the deallocation process (Pro C++, Cons java)

As you can see there are pros and cons for both solutions only considering the equivalent of garbage collection, as you can imagine considering all the aspects of the language is possible to say that there is no a winner but Java is better in some aspect and C++ for others. It is let to you to make the right choice depending on what are you developing.



Related Topics



Leave a reply



Submit