The answer is
BYTECODE.!
Bytecode it the key that makes Java language most secure and Portable.
When you compile your java program then on successful compilation , java compiler (javac) generates a class file with .class extension which contains the Bytecodes of your java program. Now the Bytecodes which are generated are secure and they can be run on any machine (portable) which has JVM.
No doubt Java is Platform Independent, but Java is JVM dependent.!
Actually , JVM is an interpreter for Bytecode.
The details of the JVM will differ from platform to platform, but all interpret the same Java Bytecode according to machine/platform.
The Bytecode which are generated by the compiler will be tested by the JVM on the execution of the program or we can say every Java Program is under the control of the JVM which checks the code on the runtime many times for viruses and any malicious.
The Bytecode generated by the compiler are also supported on any machine which has the JVM which makes Java a platform independent language.
The details of the JVM will differ from platform to platform, but all interpret the same Java Bytecode according to machine/platform.
The Bytecode which are generated by the compiler will be tested by the JVM on the execution of the program or we can say every Java Program is under the control of the JVM which checks the code on the runtime many times for viruses and any malicious.
The Bytecode generated by the compiler are also supported on any machine which has the JVM which makes Java a platform independent language.
What happens if Java Program were not compiled with Bytecodes?
If the Java Program were compile to native code (other than java language) , than different versions of the same program would have to exist for each type of CPU connected to the Internet. Thus, the interpretation of the bytecode is the easiest way to create truly portable programs.
Can Interpretation of Bytecodes makes the Execution Slower?
The answer is NO.!
Since, many old programming languages like C, C++ etc. only compiled (whole code conversion to machine code) or interpret (line by line conversion) the code on execution but this is not the case with Java language which first compile the source program to Bytecode and than interpret the code on execution. Now this interpretation of the Bytecode is very fast and which makes the Java is much faster language than any other.
These are the reasons which makes Java the most Secure and Portable language than any other language.