January 1, 2022

Writing First Java Program - Hello World

This Java tutorial shows how you can write, compile and execute your first Java program which is going to be the HelloWorld Java program.

If you have just started learning Java and done with the installation of Java it is time to write your first Java program.

Want to install Java in Windows, see this post- Installing Java in Windows

Writing Java program

For writing your first Java program you can use an IDE like Eclipse and run it from there or write it in notepad and compile and run it from command line. Once you start some serious Java programming you will definitely use an IDE but for your first Java program it is better if you use command line for compilation and execution of the program. In this post we’ll cover both ways.

Hello World Java program from command line

Open a text editor like Notepad or gedit and copy the following program.

public class HelloWorld {
  public static void main(String[] args){
    System.out.println("Hello world, this is my first Java program");
  }
}

Save the file as HelloWorld.java, you have to give the file the same name as the name of the class which is public in you Java program. Since the name of the public class is HelloWorld, you will have to name the file as “HelloWorld”.

Open command prompt, go to the location where you have saved your file and compile it using javac command. For example I saved the file in knpcode folder.

D:\knpcode>javac HelloWorld.java

This command compiles your Java file, if compilation is successful you should see a file HelloWorld.class generated at the same location.

If you get error “javac is not recognized as an internal or external command” that means path to bin directory in your Java installation is not set, so ensure that path is set.

To execute your HelloWorld Java program execute the following command.

D:\knpcode>java HelloWorldHello world, this is my first Java program

As you can see message is displayed.

Understanding your first Java program

For writing a Java program you need to first define a class and then write methods with in the class.

The following statement defines a class HelloWorld which has an access modifier as public.

public class HelloWorld 

In this class you have only a single method main() which is defined as follows.

public static void main(String[] args){  System.out.println("Hello world, this is my first Java program");}

Java programming language mandates that there should be a main method which should be public, void, static. This main method is the starting point of the program execution.

To understand why main method is static in Java, refer this post- Why main Method static in Java

To understand the different keywords that make up the main() method-
publicAccess modifier for visibility. Public access modifier means it is visible to all the classes.

staticstatic keyword in Java when used with method means you don’t need to create an object of the class to invoke a static method. If you see in the Java program main method is executed on its own (actually it is executed by JVM) you didn’t need to create an object and invoke main method.

void– It represents the return type of the method, void means nothing is returned from the method.

String[] args– It is used for passing command line arguments.

The statement- System.out.println(“Hello world, this is my first Java program”); prints the message, with in the double quotes, to the console.

Hello World Java program using Eclipse IDE

If you want to use Eclipse IDE for writing Java program then the first step is to get Eclipse if you don’t have it already.

You can download the Eclipse version as per your OS and Java version from here- https://www.eclipse.org/downloads/

Once you have Eclipse downloaded and installed, you can open it and set the location for the workspace (folder where your projects will be stored).

Once Eclipse is started-

1- choose File – New – Project

2- Select Java Project from the list and click Next.

Hello World Java program

3- In “Create a Java Project” window, enter a name for your Java project, for example “myproject”.

4- Click Finish. Once prompted with the window having the message “This kind of project is associated with the Java perspective. Do you want to open this perspective now”. Click Open Perspective.

5- Once the Java perspective is opened you will see your created project on the left hand side. Expand it and right click the src folder. Select New – Class.

Java program in eclipse

6- In the New Java class window enter the class name “HelloWorld”. In the “Which method stubs would you like to create?” section select the public static void main(String[] args) checkbox. Click Finish.

7- That will create the class HelloWorld.java with the main method. Java editor for this class will also open.

public class HelloWorld {  public static void main(String[] args) {    // TODO Auto-generated method stub  }}

8- With in the main method add this line.

System.out.println("Hello world, this is my first Java program");

So that your HelloWorld.java class looks as follows.

public class HelloWorld {  public static void main(String[] args) {    System.out.println("Hello world, this is my first Java program");  }}

9- Save the file (you can use ctrl-s to save). Your Java file will also be compiled automatically when saved.

10- Right click the program file HelloWorld.java on the left hand side and select Run As – Java Application.

11- Eclipse Console will open and display "Hello world, this is my first Java program".

Done with first Java program, want to add more methods in your class then refer this post- Methods in Java

That’s all for the topic Writing First Java Program – Hello World. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment