I am searching about JVM in detail to understand the working of it and to work properly with Java programming language so, how much JVM is important for java compilation except Operating system independent.
-
2JVM is needed to run the compiled code. The compiler is the only thing you need to compile code, JVM has nothing to do with compilation. Then again, the compiler is written in Java, so you need the JVM to run the compiler.Vince– Vince2016-09-26 19:24:16 +00:00Commented Sep 26, 2016 at 19:24
-
1You can write a Java compiler in C if you want. Java won't care.Marko Topolnik– Marko Topolnik2016-09-26 19:24:28 +00:00Commented Sep 26, 2016 at 19:24
-
2It is important as the Java compiler is written in Java. Doesn't have to be, but it is.user207421– user2074212016-09-26 19:31:17 +00:00Commented Sep 26, 2016 at 19:31
-
Why would you want to produce byte code but not use a JVM?Peter Lawrey– Peter Lawrey2016-09-26 20:19:05 +00:00Commented Sep 26, 2016 at 20:19
-
There is an abandoned Java compiler written in C++ (jikes.sourceforge.net), it does not depend on JVM runtime.SK-logic– SK-logic2016-09-27 09:03:42 +00:00Commented Sep 27, 2016 at 9:03
3 Answers
If using the primary compiler, Javac, you'll need a JVM.
The JVM does not affect compilation, it's only needed because the primary compiler is actually a Java program itself, and a JVM is needed to run Java programs.
Theoretically, a compiler is the only thing you need to compile Java code. In fact, the JVM knows nothing of the Java programming language, only the byte-code generated from compilers. The JVM allows your computer to run Java programs, and does not affect the compilation process.
However, Java's primary compiler was written in Java. To run the primary compiler, you need a JVM.
If you used a compiler written in a different language, you wouldn't need the JVM to compile code.
Comments
JVM does not compile the code, it interprets.
Java is both an interpreted and compiled language. The Java compiler ,'Javac' produces byte-code which is platform-independent. This byte-code is, we can say generic, ie., it does not include machine level details, which are specific to each platform. The instructions in this byte-code cannot be directly run by the CPU. Therefore some other 'program' is needed which can interpret the code, and give the CPU machine level instructions which it can execute. This program is the 'JVM' (Java Virtual Machine). Note that the JVM is platform specific.
Comments
The JVM has nothing to do with compiling java code. The JVM is purely responsible for running already compiled code. There exists lots of software for compiling java code, for example, the javac compiler included in the JDK, or the integrated compilers in most IDE's, and there are seperate compiling tools such as gradle or ant.
5 Comments
javac' being written in Java, your answer is not correct.gradle and ant are not separate compiling tools, they all rely on javac and add dependency resolution and management. All widely used and up-to-date Java compilers actually run on the JVM.javac compiler. But, that's beside the point here, sure, javac requires the JVM to run, just like any other Java program, but you don't absolutely need it for compiling, which was what the question originally asked.