Tuesday, June 25, 2019

First Java Program - Hello World Java Program

In this Java tutorial we’ll see how to write your first Java program- A Hello World Java program. A “Hello World” program is just a simple program to display “Hello World” on the screen. Since focus is more on how to write, compile and execute a Java program so better to start with a simple Hello World program.

Writing your first Java program

To write a Java program you can use an IDE like Eclipse which provides an integrated environment to write, compile and execute Java program.

You can also use an editor like notepad to write your first Java program, that will give you a better idea about how to compile and execute a Java program.

In this post we’ll cover both ways of writing Java program-

  • Using text editor like notepad or gedit
  • Using Eclipse IDE
  • Compile and run first Java program -Hello World using text editor

    Open a text editor and write Hello World Java program as given here-

    public class HelloWorld {
      public static void main(String[] args){
        System.out.println("First Java program - Hello world");
      }
    }
    

    Following the convention of file name same as class name in Java save this program file as “HelloWorld.java”.

    From command line go to the location where you have saved your code file and compile it using javac command.

    F:\NETJS>javac HelloWorld.java 
    

    If program compiles successfully you should see a class file HelloWorld.class generated at the same location.

    hello world Java

    Once the code is compiled and the class file is generated you can execute your HelloWorld Java program using the following command to see the message displayed on the screen.

    F:\NETJS>java HelloWorld
    
    First Java program - Hello world // Output
    

    Understanding Java Program

    Now when writing Hello World Java program is done let’s try to understand it.

    1- First thing is to define a class, since Java is an object oriented language so code has to be written with in a class.

    In our program class name is HelloWorld with access modifier as public.

    public class HelloWorld {
     ...
    }
    

    2- In HelloWorld class there is only one method main() which is defined as follows.

      public static void main(String[] args){
        System.out.println("First Java program - Hello world");
      }
    

    Note that in Java, every application must contain a main method. This main method is the starting point of the program execution. The signature of the main method should follow the same convention of being public static void. To get more idea about why main method is public void static refer this post - Why main Method static in Java

    3- Statement with in the main method–

    System.out.println("First Java program - Hello world");
    

    prints the String between the double quotes to the output console.

    Hello World Java program using Eclipse IDE

    For writing your first Java program using Eclipse IDE, first step is to download Eclipse if you don’t have it already.

    Download the version of Eclipse depending on your OS and Java version from here- https://www.eclipse.org/downloads/

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

    1- First thing is to create a new Java project in Eclipse for that-

    1. Choose File – New – Project
    2. Select Java Project from the list and click Next.
      Java program Eclipse
    3. In “Create a Java Project” window, enter a name for your Java project.
    4. Click Finish.
    5. Click Open Perspective for the message “This kind of project is associated with the Java perspective. Do you want to open this perspective now”.

    2- Expand the created project and right click “src” folder. Select New – Class.

    First Java program

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

    4- Click Finish.

    5- In the Java editor opened for this class you should see a HelloWorld class with main method.

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

    6- Add the following code inside main method.

    System.out.println("First Java program - Hello world");
    

    To have a class like this-

    public class HelloWorld {
     public static void main(String[] args) {
      System.out.println("First Java program - Hello world");
     }
    }
    

    7- Save the class. Right click on the program file HelloWorld.java and select Run As – Java Application.

    8- In the console you can see the output.

    Hello World Java program in another method

    Till now we have written the code for printing “Hello World” with in the main method, that is fine for a small program like this but very soon you’ll find out that it’s better to divide code in different methods with in a class. This example gives an idea how to do that.

    public class HelloWorld {
     public static void main(String[] args) {
      HelloWorld obj = new HelloWorld();
      obj.displayMessage();
     }
     
     private void displayMessage() {
      System.out.println("First Java program - Hello world");
     }
    }
    

    Output

    First Java program - Hello world
    

    As you can see in this Hello world example there is a new method displayMessage() that does the work of displaying the message. To call that method class object is created using new operator and using that object the method is called.

    It is getting very repetitive to display the same message again and again, why don’t we write a method to which you can pass the message that is to be displayed.

    public class HelloWorld {
     public static void main(String[] args) {
      HelloWorld obj = new HelloWorld();
      obj.displayMessage("Hello from main");
      obj.displayMessage("Call method again");
     }
     
     private void displayMessage(String msg) {
      System.out.println(msg);
     }
    }
    

    Output

    Hello from main
    Call method again
    

    As you can see now the method has a String argument. By passing value for that argument while calling the method you can display different messages in each call.

    That's all for this topic First Java Program - Hello World Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

    >>>Return to Java Basics Tutorial Page


    Related Topics

    1. Java Pass by Value or Pass by Reference
    2. Primitive Data Types in Java
    3. Java for Loop With Examples
    4. Arithmetic And Unary Operators in Java
    5. String in Java Tutorial

    You may also like-

    1. final Vs finally Vs finalize in Java
    2. Why no Multiple Inheritance in Java
    3. HashSet in Java
    4. Thread States (Thread Life Cycle) in Java Multi-Threading
    5. Linked List Implementation Java Program
    6. How to Reverse a String in Java
    7. Autowiring in Spring Using XML Configuration
    8. Constructor in Python - __init__() function

    No comments:

    Post a Comment