Java #1 – Getting started

In this section of the website, we will be learning how to code in the Java programming language.
Java is a very popular programming language, with over 7.6 million developers. It is mainly used for writing server-side code (backend code), but it can also be used to write applications for desktop users and android apps, for example Minecraft is written in Java (at least the PC version is).

In this article, we will set up Java on your computer, create your first program and set up an IDE (Integrated Development Environment) to make it easier for you to code.

1. Downloading and installing Java

To be able to run and compile programs on your computer (we’ll get to what those words mean a little later), you first need to install Java on your computers.
Most computers actually already have Java installed, but only the JRE. The JRE, or Java Runtime Environment, does allow us to execute Java code, however we cant compile it. Compiling a program is an essential step in a large amount of programming languages, as it turns our human-readable code (code that is typed out in words and letters) into binary, or computer-readable code (code that looks like a jumbled mess to the human eye but makes sense for a computer).

Anyways, you can easily install Java from their website. Download and run the installer for your OS. You should get a message telling you that Java was successfully installed.
We will be using Java 8 in this tutorial, as it is still the most popular version, however the most recent version of Java is Java 15.

To check if Java was correctly installed, open the command prompt (if you don’t know how to do this, check this article). Then, type out the command javac -version and hit enter.

If you see something like this, java is correctly installed :

C:\Users\Abitofevrything>javac -version
javac 1.8.0_261

If you see an error, try rerunning the installer. Reopen the command prompt and try again. If you still get an error, try the solution here. If the problem still isn’t fixed try asking on the discord server for help.

2. Writing your first program

Once that java is successfully installed, you can start writing programs!

For our first program, we’re going to create the classic Hello, World! program in Java. To start off, create a file called HelloWorld.java. This will be the file where we write our code, so open it in your text editor.

Note: the text editor you open this file with has to be plain text. If not, java won’t be able to compile the file as a bunch of other data (font, text size, text colour…) will also be included and will mess up the compiler. If you’re unsure if your editor is plain text, give it a google search.

Next, paste this code into the file and save it. I’ll explain what all of this means in the next tutorial, but for now just paste it in and save your file, noting down what directory (folder) it was saved in.

public class HelloWorld {

    public static void main(String[] args) {

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

    }

}

Now, we need to compile our code to be able to run it. To compile our code, we use the javac command in the command line.
So, open the command prompt and navigate to your folder. You can do this with the cd command, like this.
If I had saved my file in C:\Users\Abitofevrything\Documents, I would type the following command :

cd "C:\Users\Abitofevrything\Documents"

Note : make sure not to leave the file name on the end of the path to your directory! Running cd "C:\Users\Abitofevrything\Documents\HelloWorld.java" won’t work!
Double note : make sure to replace C:\Users\Abitofevrything\Documents with your directory! Chances are you aren’t called Abitofevrything, after all.

The quotes are optional, but its generally a better idea to put them there.

Once you’ve navigated to the directory you saved your file in, run the command javac HelloWorld.java if you get an error, its most likely one of three things:

  1. You aren’t in the correct directory. Make sure that the path shown to the left of where you type the commands matches the one of the directory where you saved your file
  2. You didn’t copy the code into the file correctly. Make sure that you copy the entire code into your file, then save it and try again.
  3. The file name isn’t correct. Make sure you have saved it as HelloWorld.java and try again.

If all goes well, there should now be a file called HelloWorld.class in the same directory as HelloWorld.java. This is the compiled version of the code we pasted earlier, and it looks like this:

Êþº¾ 4 
    
     ()V Code LineNumberTable main ([Ljava/lang/String;)V
SourceFile HelloWorld.java     
Hello, World!   
HelloWorld java/lang/Object java/lang/System out Ljava/io/PrintStream; java/io/PrintStream println (Ljava/lang/String;)V !           *· ± 
      %   ² ¶ ± 
    
 

Not very readable, however the computer is able to understand this mess much better than we are. To see this, run the command java HelloWorld, still in the same directory, and you should see Hello, World! printed to the command prompt. You’ve just executed your first Java program!

I’ll explain how this works in the next post.

3. Installing an IDE

It’s a bit tedious to write all your Java code like this though, and luckily for us there are tools called IDEs that can help us with just that.
An IDE provides you with fancy featuers like code completion, syntax highlighting, an easy way to run your code and tools for debugging.

There are a lot of options when it comes to IDEs for Java, however the two most popular IDEs for Java are Eclipse and IntelliJ. You can download Eclipse here and IntelliJ here.

I will be using Eclipse in the tutorial series, but you can use IntelliJ, or any IDE that supports if you want to. You might have to search how to do things though.

If you’ve gone with Eclipse, you’ll need to select the Eclipse IDE for Java Developers option from the installer.

I’ll explain how to use an IDE in the next post.

Phew, you made it! That was quite a long article and you’re probably just as confused as I was when I first started. Don’t worry though, I’ll do my best to explain everything and you’ll become an expert in no time.

Don’t forget, if you ever get confused or have a problem that you can’t fix, there are amazing people on the subreddit and discord that are ready to help you!

Next Post