2

I am new to java and bit confused about the role of compiler and JVM . I have read couple of resources and to name few

  1. What-are-the-functions-of-Java-Compiler ?
  2. Is the JVM a compiler or an interpreter?

Compiler As i save the file .java file in system, computer internally saves it in bytes 0 and 1's. I understand compiler is validating whether written java program confirms to java standard or not. If not throws the error otherwise jenerate the class file.

My question is what is need of generating .class file. Can't JVM interpret it directly (from bytes generated corresponding to .java file) without needing .class file? Does compiler(javac) do any kind of optimization here ?

JVM :- This question is other way around. Can't compiler generate the byte/machine code which can be interpreted by CPU directly? so why JVM is needed here ? Is JVM required to interpret the byte code specific to platform for example windows or linux ?

Community
  • 1
  • 1
scott miles
  • 1,511
  • 2
  • 21
  • 36
  • 2
    The basic reasons for doing it this way are (i) object code portability and (ii) this way you only need one compiler and *N* JVMs, instead of *N* compilers. – user207421 Feb 15 '17 at 07:22
  • 1
    Setting up a build environment is complex and time consuming. A larger project could take minutes to build (including javadoc) You don't want to be doing that each time you run the program. – Peter Lawrey Feb 15 '17 at 09:09
  • 1
    There is compliers for Java which generate machine code (Excelsior JET) However Java benefits from dynamic compliation by optimising based on how the code is actually used and can be much faster than an AOT. – Peter Lawrey Feb 15 '17 at 09:10
  • 1
    Windows and Linux on the same processor use the same machine code (but different system calls) – Peter Lawrey Feb 15 '17 at 09:10
  • 2
    @PeterLawrey `Setting up a build environment is complex and time consuming. A larger project could take minutes to build (including javadoc) You don't want to be doing that each time you run the program ` Actually this what my first question is. what is the need to build/compile java file in to class file. why JVM was not designed to interpret the java file itself. What optimization/benefit we get first compiling the source to byte code and then giving to JVM. I know i am asking the very basic question but i did not find it anywhere on google – scott miles Feb 15 '17 at 16:37
  • @scottmiles the most effective way to fix bugs is as early as possible. If you validate at runtime you might delay knowing whether there is a simple bug for hours / months. When it could have been detected and fixed almost immediately. – Peter Lawrey Feb 15 '17 at 16:39
  • @PeterLawrey ok get it. But in that case compiler can just validate the file and inform user whether it is fine or not . Why it is converting it to class file. I believe there must be some other reason too. Is n't it ? Does compiler do any kind of optimization also which makes the interpretaion by JVM faster ? – scott miles Feb 15 '17 at 17:05
  • @scottmiles it saves the jvm having to compile the code. It does very little optimisation but does save work. – Peter Lawrey Feb 15 '17 at 17:22
  • @PeterLawrey can we have quick chat for 5 mins ? you said ` it saves the jvm having to compile the code` again my question is what is need to compile it even by JVM. Why JVM was not designed o interpret it directly ? – scott miles Feb 15 '17 at 17:45
  • @scottmiles I have a library which generates and compiles code on the fly using Compiler API but even for simple classes thus takes a performance hit and you want to do it as little as possible. – Peter Lawrey Feb 15 '17 at 17:48
  • @PeterLawrey i read more about and couple of other answers (including yours) like http://stackoverflow.com/questions/8791955/what-is-the-role-of-the-os-when-jvm-executes-a-java-application-and-why-do-we-n.i Believe what you are trying to convey is . Java compiler compiles the bits into byte code(or we can say instructions specific to JVM) which JVM can interpret/execute easily. So it saves time and performance. That is why sometimes interprted language like PHP and javascript may be inefficient ometimes like take more CPU or memory. Is that correct now ? – scott miles Feb 16 '17 at 01:26
  • @scottmiles performance is a factor. It is also useful to have a cut down version of the code so you can see what is actually used. Ie what should code do at a byte code Level. – Peter Lawrey Feb 16 '17 at 01:43

6 Answers6

0

Java bytecode is an intermediate, compact, way of representing a series of operations. The processor can't execute these directly. A processor executes machine instructions. They are the only thing that a processor can understands.

The JVM processes stream of bytecode operations and interprets them into a series of machine instructions for the processor to execute.

My question is what is need of generating .class file. Can't JVM interpret it directly (from bytes generated corresponding to .java file) without needing .class file? Does compiler (javac) do any kind of optimization here ?

The javac generate .class file which is an intermediate thing to achieve platform independence.

To see what compiler optimized, simply decompile the bytecode, for instance with javap or JD.

This question is other way around. Can't compiler generate the byte/machine code which can be interpreted by CPU directly? so why JVM is needed here ? Is JVM required to interpret the byte code specific to platform for example windows or linux ?

The designers of the Java language decided that the language and the compiled code was going to be platform independent, but since the code eventually has to run on a physical platform, they opted to put all the platform dependent code in the JVM. Hence for this reason we need JVM to execute code.

To see what the just in time compiler optimized, activate debug logging and read the disassembled machine code.

fabfas
  • 2,200
  • 1
  • 21
  • 21
0

The compiler generates byte code, in a Java meaning, which is a .class file, containing the byte code, which is not made of 1 or 0, no matter what OS you are running on.

And JVM interprets byte code to run it on specific OS

DamCx
  • 1,047
  • 1
  • 11
  • 25
0

A JVM, is an implementation of the Java Virtual Machine Specification. It interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform").

JVM is the target machine for byte-code instead of the underlying architecture.

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) which is platform specific. That's why you have different JVM for Windows, Linux or Solaris.

Rohit Gulati
  • 542
  • 3
  • 15
0

The main point of having these two stages and intermediate representation ("byte code" in Java) is platform-independence.

The Java program, in a source code, is platform-independent (to a degree). When you compile it into byte code, the Java compiler basically does (almost) all of the things it can do while still maintaining the platform-independence:

  • validates syntax
  • performs static type checks
  • translates human-readable source code into machine-readable byte code
  • does static optimizations
  • etc.

These are all things, that:

  • maintain platform-independence
  • only need to be performed once, since they don't rely on any run-time data
  • take (possibly long) time to do so it would be waste of time to do them again each time the code is executed

So now you have .class files with byte code, which are still platform-independent and can be distributed to different OS or even hardware platform.

And then the second step is the JVM. The JVM itself is platform-specific, and it's task is to translate/compile/interpret the byte code on the target architecture and OS. That means:

  • translate byte code to the instruction set of given platform, and using target OS system calls
  • run-time optimizations
  • etc.
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
0

What-are-the-functions-of-Java-Compiler ?

'Javac' is a Java compiler which produces byte-code(.class File) and that code is platform-independent.

Is the JVM a compiler or an interpreter? Ans- Interpreter

JVM is Java Virtual Machine -- Runs/ Interprets/ translates Bytecode into Native Machine Code and it internally uses JIT

JIT Compiles the given bytecode instruction sequence to machine code at runtime before executing it natively and do all the heavy optimizations.

All the above complexities exists to make java compile once run anywhere or Platform independent Language and Because of that bytecode or JAVAC Output is platform-independent but JVM executing that bytecode is Platform-dependent i.e we have different JVM for Windows and Unix.

Ankur Singh
  • 339
  • 3
  • 16
0

I think first we should discuss the difference between executing an interpreted runtime vs a machinecode runtime vs a bytecode runtime.

In an interpreted runtime the (generally human readable) source code is converted to machine code by the interpreter only at the point when the code is run. Generally this confers advantages such that code is platform independent (so long as an interpreter exists for your platform) and ease of debugging (the code is right there in front of you) but at the cost of relatively slow execution speed as you have the overhead of converting the source code into machine code when you run the program.

In a compiled runtime the source code has been compiled into native machine code ahead of time by a dedicated compiler. This gives fast execution speed (because the code is already in the format expected by the processor), but means that the thing you distribute (the compiled binary) is generally tied to a given platform.

A bytecode runtime is sort of a halfway house that aims to give the advantages of both intepretation and compilation. In this case the source code is complied down into an intermediate format (byte code) ahead of time and then converted into machine code at runtime. The byte code is designed to be machine friendly rather than human friendly which means it's much faster to convert to machine code than in a traditionally interpreted language. Additionally, because the actual conversion to machine code is being done at run time, you still get all that nice platform independence.

Note that the choice of whether to intepret or compile is independent of the language used: for example there is no reason in theory why you could not have a c intepreter or compile python directly into machine code. Of course in practise most languages are generally only either compiled or interpreted.

So this brings us back to the question of what the Java compiler does- essentially it's main job is to turn all your nice human readable java files into java bytecode (class files) so that the JVM can efficiently execute them.

The JVM's main job, on the other hand, is to take those class files and turn them into machine code at execution time. Of course it also does other stuff (for example it manages your memory and it provides various standard libraries), but from the point of view of your question it's the conversion to machine code that's important!

d80tb7
  • 863
  • 2
  • 9
  • 20