Java is a high-level language. This means that it is similar to human language, although of course much less flexible or complex. Programming languages start from the lowest level - machine code, which is literally just binary numbers coding for instructions or data - up to a bit more human-readable - assembly code, which replaces binary numbers with simple combinations of letters - up to higher level languages like C or Java. Java is truly fun to write, but because it is high-level, it needs to be translated all the way down to machine code which your computer's processor can execute. For this, we need a compiler. This is a special program which will take our Java code (which we will save inside text files) and convert it into bytecode, which is like a compressed version of Java code, and virtually unreadable to a human. Here's an example of some Java code and its compiled bytecode form:
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Êþº¾ :
java/lang/Object ()V
java/lang/System out Ljava/io/PrintStream; Hello World
java/io/PrintStream println (Ljava/lang/String;)V Test Code LineNumberTable main ([Ljava/lang/String;)V
SourceFile Test.java ! *· ± % ²
¶ ±
But even then, our computer is unable to understand what it is saying. This bytecode needs to be translated once more by the JVM (Java Virtual Machine) into machine code, which can finally be run by the processor.
This is necessary for Java to be platform independent, which means it can run on almost any machine. The JVM understands the architecture of many different CPUs (Central Processing Units), and can run Java code written, compiled, and tested on a Windows machine on a Linux or MacOS machine, and vice versa. This flexibility is one of the reasons Java was and remains one of the most popular programming languages.
Java was released by Sun Microsystems in 1996, after five years of development. Sun Microsystems was aquired by Oracle in 2009, and has maintained development of Java since. We will be downloading our Java compiler and Java virtual machine from Oracle, here. You'll want to download the newest version's JDK, which right now is version 14. Scroll down and download the installer for your OS, and then agree to the terms. Once you have it downloaded (it doesn't matter where - downloads folder or anywhere else is fine), run the installer. When it prompts you to select the location to save to, just leave it at the default, but make sure to write down or copy the installation folder, because we will need it later. After you get the message that the Java SE Development Kit was installed, you can either close the installer or click Next to view the documentation.
The next step is going to be adding Java to the PATH environmental variable, but first we should learn what this means as well as understand why this is necessary. When we compile and run our Java programs, we are going to do so through something called the command prompt (terminal on MacOS or console on Linux). If you open it right now - just type "command prompt" in the Windows search bar - it might seem a little scary. But before the ubiquity of GUIs (Graphical User Interfaces), the command prompt (or a similar application) was the main way of interacting with the computer. We will (in the beginning, at least) use the Command Prompt to compile and run our Java programs, just so we know how to do it.
The command prompt allows us to use special commands to alter the state of the computer. There are commands to create, move, copy, rename, and delete files and folders, as well as do a whole host of other things.
An environmental variable is a system-wide variable essential for the function of the computer.
For example, the location of the Program Files folder or the number of processors the computer has are both environmental variables.
If you want to see all of the environmental variables on your computer, you can type the command set
into the command prompt.
These environmental variables are a convenient place for computers to store the location of compilers and interpreters for the languages running on them.
By "adding Java to the PATH", we will essentially be telling the command prompt the location of the Java compiler and JVM, which will allow it to understand commands which tell it to compile or run our programs.
I want to preface this by saying that it is most likely that the Java installer already added itself to the PATH, and you don't need to do anything. Also, DO NOT modify ANYTHING unrelated to Java in your PATH; I don't want to scare you, but let's just say that messing your environment variables up is a bad thing.
On Windows, the easiest way to check if Java is in your PATH is to go to 'Control Panel' > 'System and Security' > 'System' and then click on 'Advanced System Settings' on the left-hand side.
Then, click the button at the bottom of the 'Advanced' tab which is labeled 'Environment Variables'.
In the 'System Variables' box, click on 'Path', then 'Edit'.
There should be a list of file locations and other things, but you're looking for one that has the Java installation folder.
Mine looks like this: C:\Program Files\Java\jdk-14.0.1\bin
, and yours should look similar (it should be the same as the default installation folder the installer saved Java to).
If you see a line that looks like that, you're good. If you don't, you should first open your file system and go to that folder location to make sure it exists, and then click 'New' in the path variable list window. In the new environment variable, paste that location in, then click 'OK'. You're done now, and can move on to the next portion.
If you are not a Windows user, adding Java to your PATH should essentially be the same in that you're adding a folder to a scary looking system window/file, but getting to the window is a little different.
I'm not a big Linux user and I'm not at all a Mac user, so here are guides written by more qualified people on how to do it:
Linux: here or here
MacOS: here or here
As I hinted at previously, when we write our first Java program, we'll literally write a few lines of code in a text file, rename it 'Test.java', and then run the command javac Test.java
in the command prompt.
This will cause the compiler to create a file with the bytecode in the same folder as our original 'Test.java' file.
We can then run the command java Test
in the command prompt and the JVM will execute our program.
This process will be the exact same for every single program we write.
However, there is another way. We can use something called an Integrated Development Environment, which will make our programming flow much easier. This is a special text editor application which allows us to edit, debug, compile, and run our programs all in the same place, and it is fantastic. The most popular Java IDEs are Eclipse and IntelliJ. However, I found them almost impossible to understand as a beginning programmer, so I will actually recommend using the console to compile and run your programs, at least for now. I used to recommend Dr. Java, but the newest release requires some maneuvering to get it to work, as well as Java 8, so I don't recommend it right now.
What I do recommend you do is download Notepad++, because it is a very good text editor which will provide syntax highlighting (highlighting variable names, strings, reserved words, etc in different colors).
Keeping yourself organized and setting up a workspace is extremely important for being able to find the files you want to find. I suggest you make a folder on your desktop for all of the files you make while learning Java.
You'll need to be able to navigate to this file using the command prompt, specifically the cd
(change directory) command.
If you go to your folder in the file system and copy the address, you can type cd address
to move the command prompt's execution region to your programming folder.
You'll need to do this each time you want to compile and run a program.