1. Setting up Java on your computer

What do I need to install?

To write Java code on your computer, you'll need the Java Development Kit (JDK) installed. The JDK contains programming tools for writing Java code, the Java Runtime Environment (JRE), and a bunch of core libraries. The JRE allows you to run Java programs. The libraries are a bunch of prewritten code that you can import and use in your programs.

Check if you already have the JDK installed on your computer. Google how to do this if you don't know. Install it if you don't have it; this page should be helpful.

After you have the Java enviroment installed, follow the instructions for your operating system below to write and run a simple hello world program.

Note: Don't download any IDEs like NetBeans or Eclipse yet. It's better to start with a plain editor like emacs or Notepad++; that way, you get a better sense of the code without having an editor do the work for you.

Running the hello world program

Click for Windows, Mac, and Linux instructions.

Windows

If you have a Windows OS, follow these instructions to get a hello world Java program up and running.

Mac

If you have a Mac OS, open up Terminal. Using a text editor (like emacs) make a file called HelloWorld.java. It must be called that exactly, capitalization intact. Type (don't just copy-paste – you want to get a feel for it) the following code into the file.

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

Save and exit (in emacs with ctrl-x-c). Now run the command javac HelloWorld.java to compile your program. If errors appear, make sure you typed the program correctly; you'll need to fix everything before you can move on. Once you can compile without errors, type command java HelloWorld to run your program. All in all, the commands look like this:

$ emacs HelloWorld.java
$ javac HelloWorld.java
$ java HelloWorld
Hello world!

Linux

If you have a Linux OS, this link will hopefully be helpful. Remember to type the whole program out and not just copy-paste it; you want to get a feel for how to code in Java.

Compilation

Java's compilation process takes a raw .java code file and compiles it to a bytecode file with extension .class. To run our program, we run the bytecode file, but omit the .class extension:

javac HelloWorld.java // compiles HelloWorld.java into bytecode HelloWorld.class
java HelloWorld // runs HelloWorld.class, though we don't type the .class

Why do we compile to bytecode? Here's an oversimplified answer: because bytecode is more easily read by a computer, so compiling to bytecode first makes your Java programs run faster.

(Most implementations of Python also get compiled to bytecode before running - we just don't use two separate commands to do it. Google if you want to learn more).