How Java achieves platform independence
First thing is what platform independence means?
It means compiled Java code can run on any OS without change.
Now the question is how Java achieves this?
Java achieves this with the help of Bytecode and JVM (which is OS dependent).
When we compile a Java program it creates a .class file which contains JVM specific bytecode instead of machine language. Byte code is also known as portable code (p-code). To execute the bytecode we need JVM. Since bytecode is same for JVM of any platform hence this compiled code is platform independent.
Difference between JDK and JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JRE stands for Java Runtime Environment. It is used to provide runtime environment for .class file. It is the implementation of JVM specifications. It contains set of libraries + other files that JVM uses at runtime.
JDK stands for Java Development Kit.It contains JRE + new development tools. If you need to compile new Java programs instead of only executing .class files, you need JDK.
JVM, JRE and JDK are platform dependent because configuration of each OS differs.