Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
Thus when you write a piece of Java code in a particular platform and generated an executable code .class file. You can execute/run this class file on any system the only condition is that the target system should have JVM (JRE) installed in it.
In Short, If you have a Java Run Time Environment installed, you can execute Java code on any system though, the platform on which the program is written (executable code generated) and, the platform where the code is getting executed are not same.