Java Hello World Program

Java Hello World Program

Java Hello World program

by Ignis Dei
68 views

In this article, we will learn how to write and execute the Java “Hello World” program.

1. Introduction

The “Hello World” Java program has become the very first coding program that’s taught in universities for beginners in Computer Science because it has the ability to give us in no time much information about the structure of every Java program.

2. Technologies Used for Java Hello World Example

To be able to execute the program, the following software and applications must be installed.

  • JDK (1.8.x will do fine). You can visit this page to learn how to install Java, if you are using a Linux distribution and this page in case you use macOS.
  • Set path of the jdk/bin directory.
  • (Optional) Eclipse or IntelliJ IDE tools.

3. Java “Hello World” Example

Now, it’s time to write our first program:

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

The output of the program is:

Hello World!

There are two ways to execute our program. The first way is to execute it from Eclipse or IntelliJ. The second is to use for execution the Cmd (Command prompt).

In order to compile and run our program we need to follow the steps below:

  • We need to open the command prompt by Start menu ➔ All Programs ➔ Accessories ➔ Command Prompt.
Java Hello World
Figure 1. Launching the Command Prompt
  • Then we need to find the path for the Main.class file
Java Hello World
Figure 2. Finding the Path
  • We use the javac keyword to execute the compilation of our program from source code into byte code.
Java Hello World
Figure 3. Program compilation
  • We write the java keyword and the name of the program in order to execute it. In case there is no error, we can see this output:
Java Hello World
Figure 4. Execution of the program

4. Alternative Ways to Write Hello World Java Program

There are many alternative ways to write a Java program. Here, we can see some modifications we can do to every Java program:

4.1 We Can Change the Sequence of the Modifiers

For example:

static public void main(String[] args)  

4.2 We Can Change the Sequence of the Subscript Notation in the Java Array

For example:

public static void main(String args[])  
public static void main(String []args)  
public static void main(String[] args)  

4.3 We Can Change the Java Main() Method Signature

Java gives us the ability to change the Java main() method signature but public static void main is the preferred signature according to Google Java Style Guide.

For example:

static public void main(String[] args)  
public static final void main(String[] args)  
final public static void main(String[] args)  
final strictfp public static void main(String[] args)  

By the way, strictfp is a modifier in the Java programming language that restricts floating-point calculations to ensure portability.

4.4 We Can Add a Semicolon at the End of The Program

For example:

class Example{  
   public static public void main(String[] args){  

...
   }  
}; 

5. Keywords and Parameters Used in “Hello World”

In this section, we will talk about the meaning of the keywords and statements that we used in the “Hello World” Java program.

  • class keyword: It is used to declare a class in Java.
  • public keyword: It is an access modifier that represents visibility of the method or a variable to other objects and classes.
  • static keyword: It is used to save memory because we dont need to create an object to invoke the static method.
  • void keyword: It means that a method doesn’t return any value.
  • main keyword: It is used to represent the starting point of the program.
  • String[] args parameter: It is an array that store command line arguments.
  • System.out.println() statement: It is used to print a statement.

6. Conclusion

In conclusion, after having written this article, you should be able to create your first Hello World Java program. Of course, you can visit our site for more articles.

7.Sources

[1]: Lesson: A Closer Look at the “Hello World!” Application – Oracle

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

Related Posts