Is Java a compiled or interpreted language?
Unlike C or C++, Java is actually both a compiled and interpreted language because the Java source code is first compiled to the byte code, and then interpreted during execution of program. The source code is compiled by the Java compiler, javac, into several class files( extension .class). The byte codes generated in the class files is interpreted using the Java Virtual Machine, or the JVM. However, there's another compiler, the JIT (just in time) compiler that compiles those parts of the code which occur most frequently. The JVM keeps track of the number of executions required by a method, and if it exceeds a threshold limit, the JIT comes into picture.
Why is Java platform independent?
A very important note : the JVM (Java Virtual Machine) is platform dependent. That being said, the compiled class files, i.e., the bytecode generated after compiling the Java source code can be understood by a JVM running on any operating system. For example, when you create a Java program and compile it with the help of the command " javac programname.java " , a class file "programname.class" is generated which contains the bytecode of your program. This bytecode is portable and can be executed on all machines. It is represented in hexadecimal format, which is the same for all platforms. Hence, Java is platform independent. Now if you wish to run your program on a Linux machine and also on Windows, given that you have their respective JVMs installed on them, you do not need to compile your source code twice. The same compiled class file "programname.class" can be used on both the JVMs.