In this tutorial, we are going to learn about the necessary tools you need in order to start programming Java applications and create our first simple Java Program. If you prefer video tutorials, be sure to check out my youtube series, Introduction to Java here.

What is Java?

Before we jump onto the Java train, let’s understand at a higher level what Java is exactly.

Java is a general-purpose, concurrent, class-based, and object-oriented language. Whoa, that was very technical, let’s break this down.

A general-purpose language is a language that isn’t bounded to a specific domain. Examples of other general-purpose languages are C++, C#, Python. An example of a domain-specific language would be HTML because it only works for web pages (hence domain-specific).

Concurrent means that multiple programs or parts of a program can run in parallel, this is also referred to in Java as multithreading.

Class-based means that every program in Java must have at least one class. This is different than other programming languages like C or C++ that which does not require your code to be in a class.

Last but not least, object-oriented language, this is a little more complicated. An object-oriented language is used to follow the programming paradigm OOP. OOP aims to implement real-world entities like abstraction, inheritance, encapsulation, and polymorphism in programming.

What tools do I need?

Before we start programming in Java we need to download the necessary tools to write a Java program. Throughout these tutorials, I will be using IntelliJ as my IDE. Download the community edition. IntelliJ does bundle a JDK with it but you can download the latest Oracle JDK here (at the moment of writing this tutorial it is JDK 14). If you decide to download the latest Oracle JDK, extract the JDK to somewhere you’ll remember, I like to put mine in a folder called Java that’s either on the root of my main drive or in my Program Files folder.

Create a New Project

After installing IntelliJ (and downloading Oracle JDK if you chose to) open IntelliJ and click “Create New Project”.

Open IntelliJ and click "Create New Project"
Open IntelliJ and click “Create New Project”

The “New Project” wizard will now open and your Java SDK will show at the top. If you downloaded the latest Oracle JDK you can click “New” and navigate to where you extracted the JDK. Click the “Next” button.

New Project Wizard
New Project Wizard

The next window will ask if you want to create a project from a template, you do not, so leave this unchecked and click “Next” again. Here you will be asked to new your project in this tutorial series I am naming the IntroToJava.

Name the Java Project
Name Your Project

Click “Finish” and your new project will be opened.

Empty Java Project
Empty Java Project

Packages

You will now be in an empty project. Before jumping straight to our program, let’s learn about packages in Java.

A package in Java is a way to group classes, interfaces, and sub-packages. Packages are used to prevent naming conflicts, help with organization, and provide data encapsulation (an OOP principle).

Right-click “src”, choose “New”, and then choose “Package”.

When you name a package you want to make sure your naming is unique. Packages are therefore named from general to more specific. When you google online you may see a packaged name:

com.company.my

That’s saying my is part of company, which is part of the .com. The website URL would be my.company.com. In the case of this tutorial. my website is chaotictoejam.com. So I am going to name my package

com.chaotictoejam.part1

“part1” will change for each part of the tutorial I am on.

This is a good practice to have because as you introduce more third party packages you have a likelihood of name conflict, keeping your files in set packages will help you with the organization.

Your First Java Program

After creating the package we are now ready to make our first Java program. As is tradition with our people we will write “Hello World”.

Right-click your package and choose “New”, then “Java Class”. Name your Java Class “HelloWorld” and double click “Class”. You will now be brought into the HelloWorld.java file. The following is the code we will test.

package com.chaotictoejam.part1;
/**
 * Joanne Skiles
 * A simple Java program that prints to the console.
 */

public class HelloWorld {
    // Main method (the entry point to our program)
    public static void main(String[] args) {
        // Displays Hello World to the terminal
        System.out.println("Hello World!");
    }
}

Let’s break down this code.

Main Method

This is the entry point for our Java program, most Java programs require the main method in order to execute. The main method is always defined as:

public static void main(String[] args)

Command-line arguments are passed through the args parameter, which is an array of String s we will learn more about array later in these tutorials

Comments

There are multiple ways to leave a comment in Java. Comments are the statements that are not executed by the compiler and interpreter. You leave comments for code documentation, for future you, or a future developer. You should always comment your code, future you will thank you.

// This is a single line comment

/**
 * This is a comment block (a.k.a multi-line comment)
 */

Out Printing to the Terminal

In Java, you write out to the terminal using the following statement.

System.out.println("Hello World!");

System.out.println() is a Java statement that prints the argument passed, into the System.out which is generally stdout (standard output – e.g. the console in simple programs).

  1. System is a Class
  2. out is a Variable
  3. println() is a method that out prints text to and then a linebreak, there are several other methods

Compile and run your code

Now that you understand what the code is doing it is time to compile and run your program.

Right-click your HelloWord.java file and click Run”HelloWorld.main()”

Compile and Run HelloWorld.java
Compile and Run HelloWorld.java

The program will build and then your terminal will open. With “Hello World!” out printed.

Congratulations you just wrote your first Java program! You also learned the tools you need to write Java programs, how packages work, and what the main method is.