1.1 Get-go

Before we can start developing anything in Java, we first need to install it onto our systems. As Java is a somewhat old language, installing it can be a hassle at times, so do make sure to follow this section as thoroughly as possible.

Installation

We’re going to start off by installing Java from Oracle’s Java SE Development Kit 8 Downloads page. Java 8 is the latest available version of the Java programming language. One of the boxes somewhere high up on the page should say something along the lines of “Java SE Development Kit 8u [update_version]”, you’re going to want to accept the license agreement, followed by pressing the download link for you system specifications.

Windows

Should you be on a Windows-based system, you will be presented with an installer for the Java JDK. All you have to do is follow the installation through. Once that is done, there’s one more thing that needs be doing before you can get started: you need to add Java to your computer’s path, so that you can access it from the command prompt.

  1. Press CTRL+Win to open the Windows file explorer. Right-click This computer and press Properties at the very bottom of the menu popping up upon right-clicking.
  2. Click Advanced at the far right of the window, then Environment Variables at the very bottom.
  3. Scroll through the list containing all your computer’s environment variables and look for one named ‘Path’ or ‘PATH’. If you locate it, press it in the list, then click Edit. If you do not have it, press New and give it the name ‘PATH’. If you already had a path environment variable, add a semicolon at the end of its value.
  4. Add the path (in the form of text) to the Java JDK bin folder, which is located on your computer wherever you chose to install it. You may have to restard your computer depending on what version of Windows you’re on. Should you happen to be on Windows 10, there should be no more pain than potentially having to restard the command prompt if you already have it open.

If you’re having trouble location the path to the Java JDK bin folder, it usually looks something like: “C:\Program Files\Java\jdk1.8.0\bin” depending on where you chose to install it, as well as what version of Java currently is available to you.

Mac

Should you be on Mac, you may have to stick to using Java JDK 7, depending on your current skills regarding computers. Use this link if you have any difficulty installing the Java JDK using the link provided to the Java SE Development Kit 8 Downloads. It will provide you with an installation guide for installing Java JDK 7 on Mac.

Linux

Should you happen to be using Linux, you most certainly have the skills required to write a query into your favorite search engine and find the solution for installing the Java JDK on your own. As installing the Java JDK looks very different on different tastes of Linux, it would not be fit for this guide, even though this guide is highly extensive when it comes to learning the Java programming language.

There are plenty of good resources out there for as to how you may install the Java JDK on each major flavor on Linux. If you’re on a more rare flavor of Linux, there’s always the possibility that someone yet has to successfully download and install the Java JDK. Perhaps you can be the first one to manage it?

Testing

In order to make sure the installation of Java was as successful as we certainly hope ’twas, we can use the javac command in our console—terminal or command prompts depending on what system you’re on—with the -version flag appended to it:

$ javac -version

The dollar sign $ symbolizes input into the console. This command in particular, if everything works as according to plan, will print out the version of Java installed present on your computer:

javac 1.8.0_111

This may look very different depending on what time throughout life as a whole you decide to run the command. Version 1.8.0_111 is the latest version available when this tome was written.

Hello, world!

Once we’ve made sure Java JDK is installed on our computers and successfully works, it’s about time we try it out!

Text editor

If you already have a programmer’s text editor of choice, go ahead and open that up. For those of you who hasn’t, open up regular Notepad on Windows-based operating systems and stick to something else similar as for other systems. The only real importance is that it’s able to save the files as plain text.

Get’n to cod’n

Once your text editor is opened up, it’s about time we write our first Java code—save the following to a file named HelloWorld.java:

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

As to what this code does is explained in the next section Behind the scenes. For now, look at it as the simplest of programs you’re able to create—one that prints one single message into the console.

When saving this file, do make sure to save it as HelloWorld.java with .java as an extension rather than something else. Notice how it’s written all funny? This file is written using camel case. By convention we use it for all file names in Java.

But before we can celebrate, we’ll need to compile and run this application!

Console navigation

The first thing you’re going to want to do is navigate to the folder where you saved your file. Should you not know how navigation within your console works, kindly go over one of these quick tutorials to get started:

  • Windows → Follow this link.
  • Mac and Linux → Follow this link.

Once you know how console navigation works, go ahead and navigate to the folder where you saved your HelloWorld.java.

Compiling and running

Compiling and running looks the same on all operating systems—as long as the initial setting up of the Java JDK worked out as according to plan, there’s no more to it than typing in a simple command inside the same folder where you saved your HelloWorld.java:

$ javac HelloWorld.java

If you’re not notified by any means, it means it worked. A new file should now have been created into the same file where your HelloWorld.java is located. This file should go by the name HelloWorld.class. This is a class file and is what’s produced upon compiling a piece of Java code.

When using javac followed by the name of a file, you’re telling the Java compiler that you wish to compile the provided file. Follow this command up by entering yet another command into your console:

$ java HelloWorld
Hello, world!

This time around, we received some output! The command java is a means of telling Java’s virtual machine we wish to run a Java class file. This command produced the message “Hello, world!” and printed it out into the console.

It’s about time we celebrate!

Moreover

It’s always recommended writing a test program once you’ve installed a new programming language—that way you can make sure the compiler works the way you want it to. In the next section Behind the scenes we’ll discuss what this code does by scratching its surface a slight bit. When you reach chapter two, you’ll be taught one concept at a time and learn what this code does more in-depth.