Home » Core Java » Java Hello World Example

About Ioannis Makrygiannakis

Ioannis Makrygiannakis
John is an undergraduate student at the Department of Informatics of Athens University of Economics and Business. He is specialized in Databases, Knowledge Management, Information Systems and Information Security. In his free time he loves learning new things with regard to programming and network security.

Java Hello World Example

In this article, you will learn how to create a simple code in java. We are going to create a Java Hello World Example. This program is the first for most of the programmers and it will introduce to you the magic world of Java programming.

Firstly, we will show you how to install Java. After that, we will analyze the example and then we will talk about how to compile it.

Before we start, maybe you want to take a look at the basic Java Syntax Rules.

You can also check the Java Tutorial for Beginners in the following video:

Java Tutorial for Beginners: How to download and use Java

1. Introduction

Java is one of the most popular programming languages ​​in the world. It is a fully object-oriented language and allows users to develop a variety of applications such as dynamic websites or JSPs to corporate network applications. Another Java’s major advantage is that it can run on almost all platforms. You can follow this guide where you can find code examples in order to learn Java quickly.

2. Technologies used

The example code in this article was built and run using:

  • Java 1.8.231(1.8.x will do fine)
  • Eclipse IDE for Enterprise Java Developers-Edition

3. Installing Java

The first and most important thing before you start to write code is to set up the environment that you will use to compile and run your programs. The easiest and the convenient way to download and install Java is downloading it from the official site. The link here is from the official site of Oracle at which you can download any version of Java Standard Edition depending on your operating system.

The version that we use in these articles may be different from the version that you will download.

Now you must follow some steps to have a successful installation.

Steps:

  1. The file download from the link mentioned is in the form of “.exe” and has to be opened to install Java on the machine.
  2. After the installation, you must create a new environment variable. To do that you must write at Start: “Edit the system environment variables” (Windows 10) or Environment variables (Windows 7).
  3. Click on Environment Variables
  4. At System variables click “New”
  5. Write a variable name “Path
  6. Write as variable value ” C:\Program Files\Java\jdk_version\bin; ” where “ jdk_version ” the version that you downloaded!!

If you want to confirm that java compiler is ready to use you can write at cmd the command “javac”.Then it must display the following output:

Java Hello World - Cmd output
Cmd output

4. Writing Java Hello World Program

In this section, we will write and analyze the hello world program. Let’s create the hello world program:

Hello_world.java
public class Hello_World {
	public static void main(String args[]){  
	     System.out.println("Hello world!!");  
	    }  
}

The output is:

Hello world!!

4.1 Parameters used

Here we analyze the commands we use at the above code:

  • public: It is an access modifier that represents visibility.
  • class: It is used to declare a class in java.
  • static: If a method is static then there is no need to create an object to invoke the static method. In other words with static, we save memory.
  • void: If a method is void it means that it doesn’t return any value.
  • main: It represents the starting point of the program.
  • String args []: It is used for the command-line argument.
  • System.out.println: It is used to print to the console an argument that is passed to it.

4.2 Similar ways to write a program

The freedom of the programming languages gives you the opportunity to develop your creativity and your imagination and write programs in many ways. For example on this code, we can see some variants below:

  • We can change the sequence of the modifiers:
 static public void main(String args[])  
  • The subscript notation in Java array can change position:
public static void main(String args[])  
public static void main(String[] args)  
public static void main(String []args)  
 
  • You can use a semicolon at the end of the class:
public class Hello_World {
	public static void main(String args[]){  
	     System.out.println("Hello world!!");  
	    }  
};

5. Compile and Execute

After we successfully set up the environment and write our code it is time to compile it to see if we have errors. The program that does that job is the Java Compiler which takes the text file work of a developer and compiles it into a platform-independent java file.

To compile a program simply:

  • Write at “Start“: cmd and open it.
  • Then you must go to the current directory in which is the program
  • Write “javac Hello_World.java” to compile it.
  • If there are no errors you can execute the program with the command: java Hello_World
Java Hello World -  javac
javac Hello_World.java

I hope this article helped you learn the first steps in Java language. You can also check our Java Cheat Sheet.

6. Download the Source Code

Download
You can download the full source code of this example here: Java Hello World Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments