If you are new to programming in Java, then this is the first article that you should read. In this article we explain how a java program is written and run. The purpose of this example is give you a feel of a simple Hello World program in java, don’t worry if you don’t understand what each word in the below program means. Before we write the example though, download the Java Standard Edition JDK from oracle Website.
What is the difference between java JRE and Java JDK
Lets look at the ubiquitous “Hello World” example. Create a plain text file called “HelloWorld.java” and write this :
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
This is the java source file. If you have worked with any other programming language, you might expect that the next step would be to convert this text code into machine instructions that the processor can understand. However, java converts the code to what is known as bytecode. The bytecode then runs in an environment called the JVM or Java Virtual Machine. The JVM is then responsible for converting the byte to machine instructions. This setup gives Java its biggest advantage and that is we can use the same byte code and run it on any operating system, since all that the bytecode cares about is the JVM. So you can install JVM on windows and mac machine and the same bytecode will run on both the machines. How cool is that!
The bytecode is called a class file in java, so lets create the class file. Go to the directory where you have created the class file and type in
$ javac HelloWorld.java
This will read in the HelloWorld.java file and create a class file. The class file will be created in the same folder as the HelloWorld.java file. The class file is the bytecode and the bytecode can now run on any JVM on any machine. Lets just run it on the same folder. To run the class file type in
$ java HelloWorld Hello World
Now that you are comfortable writing a java application, lets step back a bit and look at the history of java.