In this article, I have explained how you can create your first java program, using Java "Hello World" program example. Simply by writing your first program code in notepad and then using the command prompt to compile it & show output.
Before we begin, you need to follow these steps, if you haven't done it yet.
- Install the JDK
- Set path of the JDK/bin directory
To set the path of JDK, you need to follow the following steps:
- Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok
After completing the above procedure.
Creating First Hello World program in Java
In this example, we'll use Notepad. it is a simple editor included with the Windows Operating System. You can use a different text editor like NotePad++
Your first application, HelloWorld, will simply display the greeting " Hello World ". To create this program, you will to follow these steps:
- Open Notepad from the Start menu by selecting Programs -> Accessories -> Notepad.
- Create a source file: A source file contains code, written in the Java programming language, that you and other programmers can understand.
The easiest way to write a simple program is with a text editor.
So, using the text editor of your choice, create a text file with the following text, and be sure to name the text file HelloWorld.java.
Java programs are case-sensitive, so if you type the code in yourself, pay particular attention to the capitalization.
public class HelloWorld{ public static void main(String args[]){ System.out.println("Hello World"); } }
- Save the file as HelloWorld.java make sure to select file type as all files while saving the file in our working folder C:\workspace
- Open the command prompt. Go to Directory C:\workspace. Compile the code using the command,
javac HelloWorld.java
Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the JVM.
The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes. - Now type 'java HelloWorld' on the command prompt to run your program
You will be able to see "Hello World" printed on your command prompt.
Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret and run applets in any Web browser with built in JVM.Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program.
The Java application launcher tool (java) uses the Java virtual machine to run your application.
And then when you try to run the byte code(.class file), the following steps are performed at runtime:
- Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
- Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
- The interpreter reads the byte code stream and then executes the instructions, step by step.
Understanding the HelloWorld.java code (With few important points)
- Any java source file can have multiple classes but they can have only one public class.
- The java source file name should be same as public class name. That’s why the file name we saved our program was HelloWorld.java
- class keyword is used to declare a class in java
- public keyword is an access modifier which represents visibility, it means this function is visible to all.
- static is a keyword, used to make a static method. The advantage of static method is that there is no need to create object to invoke the static method. The main() method here is called by JVM, without creating any object for class.
- void is the return type of the method, it means this method will not return anything.
- Main: main() method is the most important method in a Java program. It represents startup of the program.
- String[] args is used for command line arguments.
- System.out.println() is used to print statements on the console.
- When we compile the code, it generates bytecode and saves it as Class_Name.class extension. If you look at the directory where we compiled the java file, you will notice a new file created HelloWorld.class
- When we execute the class file, we don’t need to provide a full file name. We need to use only the public class name.
- When we run the program using java command, it loads the class into JVM and looks for the main function in the class and runs it. The main function syntax should be same as specified in the program, else it won’t run and throw an exception as Exception in thread "main" java.lang.NoSuchMethodError: main.
Creating Hello World Java program in Eclipse
In the above example, you were using a command prompt to compile Java program, but you can also use Java IDE like Eclipse, which helps in making development easier.
Here are the steps to follow for creating your first Java program in Eclipse
- Download Eclipse IDE
- Install Eclipse in your Machine
- Now open Eclipse, and then create a new Java project by navigating to "File"-> "New" -> "Project" -> Select "Java Project"
- Now in the next screen, give the project name "HelloWorld" and click "Finish". (If you see Perspective window click "Open")
- Now, you can see "src" in the left-pane, right-click on it, select "New" -> Select "Package" and name your package "helloworld" and click "Finish".
- Create a class inside package, by right-clicking on "helloworld" package, we just created in above step, then right-click on it, select "New" -> "Class", name it "FirstHelloWorldProgram" and click "Finish".
- Now, you can use the below code in the class
package helloworld; public class FirstHelloWorldProgram { public static void main(String args[]){ System.out.println("Hello World"); } } ?
- Once you have copy-pasted, above code, save your file and then click on "Green" button, to run your program and show output in the console.
That's it, I have already explained about the code sample above.
You may also like:
How to open console in Eclipse?
Various Java programming examples with outputPyramid Triangle pattern programs in Java with explanation
Java program to reverse a string (Different ways explained)
Leap year program in Java (multiple ways)
Fibonacci series program in Java (With and without recursion)