Signup/Sign In

Java Hello World! Program

Creating a Hello World Program in Java is not a single line program. It consists of various other lines of code. Since Java is a Object-oriented language so it require to write a code inside a class. Let us look at a simple java program.

class Hello
{
  	public static void main(String[] args)
  	{
     	System.out.println ("Hello World program");
  	}
}

Lets understand what above program consists of and its keypoints.

class : class keyword is used to declare classes in Java

public : It is an access specifier. Public means this function is visible to all.

static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.

void : It is the return type, meaning this function will not return anything.

main : main() method is the most important method in a Java program. This is the method which is executed, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error.

String[] args : This represents an array whose type is String and name is args. We will discuss more about array in Java Array section.

System.out.println : This is used to print anything on the console like printf in C language.


Steps to Compile and Run your first Java program

Step 1: Open a text editor and write the code as above.

Step 2: Save the file as Hello.java

Step 3: Open command prompt and go to the directory where you saved your first java program assuming it is saved in C drive.

Step 4: Type javac Hello.java and press Return(Enter KEY) to compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line.

Step 5: Now type java Hello on command prompt to run your program.

Step 6: You will be able to see Hello world program printed on your command prompt.


Ways to write a Java Program

Following are some of the ways in which a Java program can be written:

Changing the sequence of the modifiers and methods is accepted by Java.

Syntax: static public void main(String as[])

Example:

	
class Hello
{
	static public void main(String as[])
	{
	System.out.println ("Welcome to Studytonight");
	}
}
	

Output of First Program


Hello World Program using Eclipse

Eclipse is an IDE (Integrated Development Environment) which is used to develop applications. It is design and developed by Eclipse foundation, if you don’t have eclipse download, then download it from its official site by following this download link Download Eclipse from here Here we will see how to create and run hello world program using eclipse IDE. It require following steps that consists of creating project, class file, writing code, running code etc.

Run Eclipse and Create Project

Open eclipse startup and then create new project. To create project click on File menu and select Java project option. It will open a window that ask for project name. Provide the project name and click on the finish button. See the below screenshot.

eclipse-program

After creating project, we can see our new created project in the left side bar that looks like below.

eclipse-program

Create Java Class

Now create Java class file by right click on the project and select class file option. It will open a window to ask for class name, provide the class name and click on finish button.

eclipse-program

Write Hello World

The above created class file includes some line of codes including main method as well. Now we need to write just print statement to print Hello World message.

eclipse-program

Run The Program

Now run the program by selecting Run menu from the menu bar or use Ctrl+F11 button combination. After running, it will print Hello World to the console which is just bottom to the program window.

eclipse-program

This is a simple program that we run here while using IDE we can create and build large scale of applications. If you are a beginner and not familiar to the Eclipse then don’t worry it is very easy to operate just follow the above steps to create the program.

Now let us see What happens at Runtime

After writing your Java program, when you will try to compile it. Compiler will perform some compilation operation on your program.

Once it is compiled successfully byte code(.class file) is generated by the compiler.

class-file at runtime in Java

After compiling when you will try to run the byte code(.class file), the following steps are performed at runtime:-

  1. Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
  2. Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
  3. Interpreter reads the byte code stream and then executes the instructions, step by step.