Ever wondered why java is called write-once/run-anywhere ?, or sometimes we even see that java is platform independent, what does this all mean ?? I'll be writing a few lines on it in this article .
Before going in, we should first know what a interpreted and compiled language mean
Compiled Language: It first converts all the code into machine code and then executes the code. [ Eg: C, C++ ]
Interpreted Language: It executes code line by line, without previously converting to machine code.[ Eg: python ]
-> Java after getting compiled by javac [ Java compiler ] generates Byte-code, not the machine code.
-> This byte code is then sent to JVM [ java virtual machine ], where JVM uses Java Interpreter to convert the Byte code to machine code, then the machine code is executed on JVM.
Now we know why java is said to be both comiled and interpreted language.
So where is the Byte code stored??
-> After you compile the code in your desktop, you'll find another file in the same location of your java file with an extension ".class" , this is where the byte code is stored.
E.g : Lets say the name of your file is Firstprogram.java, now when you execute the file, another file appears in the same path with name Firstprogram.class [ Byte code ].
-> Now to execute ( run the byte code through JVM ) the command is java Firstprogram.
This .class file can be run on any platform which has JVM in it, the code dosent need to be compiled again, this is why java is said to be Platform Independent or Write-Once/Run-Anywhere.