Development

How to Code Hello World in Java

Captain Salem 2 min read

How to Code Hello World in Java

A “Hello, World!” program is a simple computer program that outputs “Hello, World!” on a display device. It is often used to illustrate the basic syntax of a programming language for a working program. The phrase has become a tradition for new programmers who are learning to code. The first known version of the “Hello, World!” program was created by Brian Kernighan in 1972, as part of the development of the C programming language.

Java is a programming language and computing platform used for creating and running applications. It was first released by Sun Microsystems in 1995 and is now maintained by Oracle Corporation. It is used to create applications for a wide range of devices, including computers, mobile phones, and television set-top boxes Java is one of the most popular programming languages becoming the go to language for building Android apps.

In this tutorial, we will learn how we to write the classic Hello World program using the Java programming language.

Requirements

To write and run the code provide in this tutorial, you will need to have:

  1. The Java JDK or JRE installed on your machine.
  2. A code editor of your choice.

Java Hello World Program.

Start by creating a file ending in .java extension to store the source code for your Hello world program.

$ touch hello_world.java

Edit the file with your text edito:

$ nano hello_world.java

Finally, add the source code as shown below:

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

Let us break down the code above.

  • public class HelloWorld - This line declares a class called HelloWorld. The public keyword indicates that the class is visible to other classes.
  • public static void main(String[] args) - This line declares the main method of the program. The main method is where the program starts when it is run. The public keyword indicates that the method is visible to other classes. The static keyword indicates that the method can be called without creating an instance of the class. The void keyword indicates that the method does not return any value. The String[] args is an array of strings that can be passed to the program when it is run.
  • System.out.println("Hello, World!") - FInally, we print the string “Hello, World!” to the screen using this block. The System class is part of the Java language library and provides access to the system’s input and output. The out field is a PrintStream object that is used to print output to the screen. The println method prints a line of text to the screen and moves the cursor to the beginning of the next line.

To run the code above, save the file and close your text editor. In the termninal, run the java compiler and pass the file name as shown:

java hello_world.java

Running the command above should compile the program and print the string “Hello, World!” on the console.

Conclusion

In this tutorial, you discovered the basics of creating and running a Hello World program using the Java programming language. You also discovered the anatomy of the Hello world program and what each section represents.

We hope you enjoyed this post.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.