First Java Program | Hello World Example
In this page, we will learn how to write the simple program of java. We can write a simple hello java program easily after installing the JDK. To create a simple java program, you need to create a class that contains the main method. Let’s understand the requirement first. The requirement for Java Hello World Example
Creating Hello World ExampleLet’s create the hello java program: Test it Now save this file as Simple.java
Output:Hello Java Compilation Flow: When we compile Java program using javac tool, java compiler converts the source code into byte code. Parameters used in First Java ProgramLet’s see what is the meaning of class, public, static, void, main, String[], System.out.println().
To write the simple program, you need to open notepad by start menu -> All Programs -> Accessories -> notepad and write a simple program as displayed below:
How many ways can we write a Java programThere are many ways to write a Java program. The modifications that can be done in a Java program are given below: 1) By changing the sequence of the modifiers, method prototype is not changed in Java. Let’s see the simple code of the main method. 2) The subscript notation in Java array can be used after type, before the variable or after the variable. Let’s see the different codes to write the main method. 3) You can provide var-args support to the main method by passing 3 ellipses (dots) Let’s see the simple code of using var-args in the main method. We will learn about var-args later in Java New Features chapter. 4) Having a semicolon at the end of class is optional in Java. Let’s see the simple code. Valid java main method signatureInvalid java main method signatureResolving an error “javac is not recognized as an internal or external command”?If there occurs a problem like displayed in the below figure, you need to set path. Since DOS doesn’t know javac or java, we need to set path. The path is not required in such a case if you save your program inside the JDK/bin directory. However, it is an excellent approach to set the path. Click here for How to set path in java.
|