In this post, we will see how to write first java program. This post is intended only for java beginner.
Table of Contents
hello world program will be a very simple program that will print
Hello, World!
to console.
Before running the program, you need to make sure java is properly installed on your machine.
Prerequisite for running a java program
- Install the JDK if you don’t have installed it, download the JDK and install it.
- set path of the jdk/bin directory. you can follow this link for setting the path
You can either write a program in IDE such eclipse or you can simply write in in text editor.
1 2 3 4 5 6 7 8 |
// First java hello world program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } |
You need to save this file as HelloWorld.java
.
How this java program works
Let’s see detailed explanation of Hello world program.
Class declaration
1 2 3 |
// First java hello world program |
This is a comment in java and this statement will be ignored at run time.
1 2 3 |
public class HelloWorld { |
As java is object oriented programming, every java application should have class definition.
- Class declaration: A class declaration includes name and visibility of the class.
class HelloWorld
is declaration of class which includes keywordclass
, followed by identifierHelloWorld
- Class declaration is followed by curly braces
{}
, which defines class’s definition.
Main method
1 2 3 |
public static void main(String[] args) { |
This is called main method in java. This is entry point for this program.
public
: This is access modifier which is used to define visibility. Here main method will be visible to all other classes.
static
: static members belongs to class rather than any specific object. It simply means you can access the static members without object.
void
: void is another keyword which defines return type of main method. It simply means main method does not return anything brefore terminating the program.
main
: main is method name here.
String args[]
: You can pass arguments to java program using args[] array. It is used to take user input using command line.
Print statement
1 2 3 |
System.out.println("Hello, World!") |
System.out.println is used to print literals
in double quotes(""
) to console. As we have passed "Hello, World!" here, it will print Hello, World!
to console.
Semicolon
As you can see, each statement is terminated with a semicolon(;)
. You can put new lines or spaces in the code but statement has to be ended by semicolon.
Compile and run the program
If you run this program in eclipse ide, you can simply right click and on run as java application
.
You can compile this java program using command line as below:
Open the command prompt and go to the location where you have saved HelloWorld.java
You can run the program using command line as below:
When you run above program, you will get below output:
When we execute java program, we need to give full classname without .java
extension.
Exercise
You need to print Yeah!! I executed my first java program
on console and name of the class should be MyFirstJavaProgram
Important points
Let’s go through some important points about Hello world program
.
- Any java source file can have multiple classes but it can have only one public class.
- Java source file name should be same as public class name.That’s why I said above “you need to save this file as
HelloWorld.java
”. - When you compile the java file, it is converted to byte code with
.class
extension, so you should be able to seeHelloWorld.class
in the directory. - When you execute the program, jvm looks for main method and will execute it. You need to be very careful with signature of main method, otherwise program may throw
java.lang.NoSuchMethodError: main.