ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to - your first Hello World Java Program

Updated on September 17, 2014

In this tutorial you will learn your first Java program. The first Java program is a simple Hello World program. The following code is a simple Java program which prints some string on the console. This is just a simple program which illustrates working of many features of Java programming language. In this application, there is one class named as 'HelloWorldDemo'. This class contains a method named as main. It’s the starting point for any application to execute. In this tutorial you will find a complete video demonstrating your first Java program. Just below the video tutorial you will get complete Java source code along with its output on the console.


A simple video demonstrating, how to create and run a simple Java program in Eclipse Integrated Development Environment.

Eclipse IDE or Text Editor creates source code file (.java file). This .java file is than compiled to .class file by the Java compiler. The .class file is also called as bytecode. Java Virtual Machine interprets this .class file and run the program.

The Java program is executed and result is printed on the console.

Creating , Compiling and Executing a Java program.
Creating , Compiling and Executing a Java program.

Steps involved in Creating, Compiling and Executing a Java program

Step 1 - Using a normal text editor or Java IDE (Integrated Desktop Environment) such as Eclipse, Netbeans etc Java source code is created. This source code file has a extension of .java into it. The name of .java file must match to the name of class used in the source code.

Step 2 - After creation of Java source file, it is sent to Java compiler. Using javac command .java file is compiled to .class which is the bytecode. This bytecode is secured and interpreted by Java Virtual Machine. If the Java source file suppose has a name as Welcome.java, after compilation Welcome.class file is created.

Step 3 - Java bytecode file named as Welcome.class gets run and interpreted by Java Virtual Machine when java command is executed on it. Java Virtual Machine knows only how to run a Java bytecode.

Step 4 - As soon as, java Welcome command is executed, Java Virtual Machine tends to find a main method into the Welcome.class file and starts its execution.

Step 5 - In this Java tutorial, if we run below program, Java Virtual Machine loads HelloWorldDemo.class file and start executing its main method. The signature of main method should be "public static void main(String [ ] args) ". It finds method with this signature and prints "Hello Hubpages !!!." with the help of System.out class println method.

Source code : How to create and run a simple "Hello World" Java program.

public class HelloWorldDemo {
	
	public static void main( String[ ] args ) {
		
		// Below line prints a string "Hello Hubpages !!!"
		// on the console screen. 
		// System has static member of PrintStream 
		// which prints the string by calling method
		// println().
		System.out.println("Hello Hubpages !!!");

	}

}

Steps to implement -

1. Create a Java class by name "HelloWorldDemo".

2. Create a main method into "HelloWorldDemo". The signature of the method should be - public static void main( String [ ] args ) { }. The Java Virtual Machine searches this main method and starts executing logic written inside it. This is the starting point where we usually write our code.

3. Create few inline comments. Inline comments are started by // and proceeds till line ends. These comments are highly used and are very important for Java programmer to put description of what he is about to code.

4. In order to print "Hello Hubpages !!!." on the Java console, call println() method of the print writer object out present in the System class. System.out instance usually points to our Java console. It prints text on the Java console whenever we call print method present in it.

5. Compile the Java program by the command as -

javac HelloWorldDemo.java

As soon as code compiles successfully a .class file gets generated. This .class file has a name of Java class name. In our example it has name as HelloWorldDemo.class. This .class file is now ready to be interpreted or run by Java Virtual Machine.

6. After compilation of HelloWorldDemo.java, a file with .class extension will be created.

7. Run HelloWorldDemo Java program by the command as -

java HelloWorldDemo


Hello World program in Java.
Hello World program in Java.

Output of the program -

Hello Hubpages !!!

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article