Java tutorial: Hello world

This tutorial helps you get started with Java. It explains what you need to write your first Java program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a Java program before this is the place to start. We have created this article, so you can get started with Java, and move on to our sms examples. If you are already familiar with Java, you can jump directly to one of the following SMS projects.

Java sms examples:

Java send sms with the HTTP rest api (code sample)
Java send multiple sms with the HTTP rest api (code sample)
Java schedule sms with the HTTP rest api (code sample)
Java delete sms with the HTTP rest api (code sample)
Java receive sms with the HTTP rest api (code sample)
Download the latest Java sms api library from Github

What is Java

Java is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is Apache NetBeans

Apache NetBeans is a tool to write a Java program. Apache NetBeans allows you to type in text using the Java language, and it makes it possible for you to tell the computer, to read the text and execute the instructions. We use the term "Run" to tell computer to execute the instructions.

What is a Java Hello world program

The Java hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Java

To write your first program in Java:

  1. Setup Apache NetBeans
  2. Configure a new Apache NetBeans project
  3. Select Java Application
  4. Name the project
  5. Create new Java class type file
  6. Write Hello World program in Java
  7. Run the Hello World program
  8. Check the output

Prerequisites

Here is what you need to get started. To write your first computer program in Java, you need a Windows computer, the Apache NetBeans programming environment and example code presented below.

  • Windows 10 computer
  • Apache NetBeans IDE
  • Ozeki Hello World example project

Download Apache NetBeans

Apache NetBeans Community Edition

In this video tutorial you will find how to download the Apache NetBeans Community Edition installer. You may download the installer from the Apache website. Click on the following URL to open the download page: https://www.apache.org/dyn/closer.cgi/netbeans/netbeans/12.4/Apache-NetBeans-12.4-bin-windows-x64.exe. On this page you will see different links to download Apache NetBeans Community Edition. Follow the instructions in this short video to download the installer on to your computer.

Install Apache NetBeans

To write your first Java program, you need to install Apache NetBeans. The following video shows you how this installation can be done.

Create your first Apache NetBeans project

After Apache NetBeans has been installed, you need to open it, and create a new project.

Create new project

To get started with coding in Java, you first need to open the Apache NetBeans IDE and create a new project. Inside the program click on the File menu on the top left corner of the screen (Figure 1). Next, click on New Project.... This brings up a new window in which you can configure your project.

create new project
Figure 1 - Create new project

Select Java Application

For this tutorial we are using Java Application as the project type. First you need to select Java with Marven from the Categories menu by clicking on it. Then, click on Java Application in the Projects menu on the right. Click on the Next button on the bottom of the window to continue.

java application
Figure 2 - Choose Java Application

Name your project

After you specified the project type you are taken to the Name and Location section. In this next step you need to give your newly created project a name. Type your desired name into the textbox next to the text that reads Project Name: (Figure 3). Here you can also specify the location of your project by clicking on the Browse button and selecting a path. Click on the Finish button on the bottom to create your project.

project details
Figure 3 - Project details

Write your 'hello world' program in Java

The first program you write in any programming language is the 'Hello world' program. The sole aim of this program is to print the term 'Hello world' to the computer screen. In this example the program consists of two lines of code: The first line: System.out.println() prints the text.

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

Write the "Hello world" code in Java

In this short video you are going to learn how to write the "Hello world" program in Java using Apache NetBeans. This is done through the text editor inside the IDE. First, you create a public static void named main. Inside main goes the actual command that prints out the text, which is System.out.println(). The text you wish to print out, in this case, "Hello World" needs to be typed inside the parentheses of the command. It also needs to be inside of quotation marks. The final command looks like this: System.out.println("Hello world!");

Create new file

To get started with coding in Java you need to create a new file. Once you have created and opened a new project you can create a new file from the toolbar on the top. Click on the new file icon in the toolbar on the top left corner of the screen (Figure 4). This brings up a window where you can select a file type.

new java file
Figure 4 - New file

Select file type

In the New File window you first need to choose a file type. First, select Java from the Categories menu (Figure 5). Then, select Java Class from the File Types menu. Once you specified your file type, click on the Next button on the bottom to continue.

file type
Figure 5 - File type

Name the class

After you choose the file type you need to give the new class a name. In the Name and Location section, find the textbox that says "Class Name:" next to it. Type your desired name into this textbox to name your class. Click on the Finish button on the bottom of the window to create your file.

class name
Figure 6 - Get name to the class

Run the Java 'hello world' program

To run the hello world program, you need to click on the green Start button in the Apache NetBeans toolbar. You may also use the F6 key to run your program. Note that when you press F6, Apache NetBeans will first save your newly written file, then it will copmile it to an executable code, and then it will run it on your computer.

Run your code

Now that you wrote your code you need to exectue it. Run your code by clicking on the green arrow on the top of the screen (Figure 7). You can also run it by pressing the F6 key. Both actions will run your code and display the output in the output console on the bottom.

run code
Figure 7 - Run code

Output of your code

Right after you ran your program succesfully your output will appear on the bottom of the screen (Figure 8). The first line in the output console will read "Hello World!". The console will also display a green "BUILD SUCCESS" text. This is to indicate that your program ran without any errors.

hello world
Figure 8 - Hello world!

What happens if I make a mistake in Java

If you make a mistake, when you write your instructions in Java, you will get a Syntax error. To correct the mistake you need to go back to the text editor and modify the program. Programs say they "fix the error" when they correct mistakes.

In the code below we will create a mistake intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in Java and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in Java?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Syntax error in Java

This video shows you what happens when you make a mistake in the code. The computer will not understand your command and therefore will not be able to run the program. If your program fails to run the IDE notifies you in the output console on form of an error message.

Errors in Java

If your code fails to run due to an error, red text appears on the bottom of the screen in the output console (Figure 9). This text contains an explanation of why the error occured. This is a good guideline for the programmer to identify the mistake in the code and fix it.

error
Figure 9 - Error

Summary

Now that you have read the in-depth guide above, you have made your first step in becoming a Java software developer. This program may seem simple, and it may not do much, but this is how great things start. A journey of a thousand miles begins with a single step, so have patience and the hard work will definitely pay off! The next step in your learning journey will be creating an HTTP request.

Java is a simple, object oriented and platform independent programming language and with the help of Ozeki SMS Gateway, you can send and receive SMS messages through this language. It is advised to learn more about Java because this is one of the most popular programming language in the industry.

To continue your studies, visit Ozeki's tutorial page where more information can be found about topics like SMS sending and scheduling in Java.

Now the only thing to do is to download Ozeki SMS Gateway and let the work begin!

More information