Introduction
Programming is the act of creating software or some other set of instructions for a computer. Essentially, this means writing words that mean something to the computer. Wait, no, that’s wrong. Computers can’t read words, they ‘read’ numbers, in fact, only 0′s and 1′s. The final step before a program is executed is to translate the words that you, as the programmer, write into 0′s and 1′s that the computer can understand.
For that, you must run the words, or the written program, through a compiler, which essentially is a translator from code-speak to computer speak. You do not really have to no anything more about compilers since you actually don’t really need to use one, however, for the purpose of these tutorials, I suggest you finding one. If you are on a PC, download BlueJ. If you are on a mac, download jEdit. And if you are on a Linux based operating system, figure it out (or just use jEdit).
Info: The programs to download that I mentioned above are IDE’s, which stands for Integrated Development Environment.
When you write code to create programs, you must learn the language that you will be coding in. In these first few tutorials of mine, we will focus specifically on Java. There are quite a few reasons for this:
- Java is considered a good ‘advanced’ programming language to start with.
- Java is very similar to other big programming languages.
- Java is already installed on most computers, making it easy to start.
- Java is always being improved by Sun Microsystems with all its classes (we’ll talk about this later) that can be found in the API hosted online.
- And many more reasons… you’ll find out!
Getting started with Java
A neat, unique feature Java has: Java compiles for each computer that a program is executed on. Instead of going straight from your typed code to the computer code, it is first transferred to something called byte code and then compiled on the machine that it is on thanks to the Java SDK that exists on the computer. This explains why you see so many online Java applets, because they run on any computer, anywhere, in the same way.
Now, check that you have the Java SDK installed on your computer so that programs can compile. Go to your command prompt (RUN > cmd on PC or just terminal on mac, Linux users should know how to get there) and type in “java -version.” If you get a version of 1.6.-, you should be good to go. If you don’t, go the Sun Mycrosystem site and download the newest version of the Java SDK.
Writing your first program
Now that you have all that ready, let’s test out the process of writing code, seeing it get compiled, and then running our first program. Do not worry, we will go over every step in this process in much more detail. By the next few lessons, you will understand everything about what we are about to do.
So, let’s get started. For this, do not use the IDE I proposed you should download and install. For the purpose of this exercise, it will simply be easier for you to open your most basic text processor (textpad, notepad, textedit, etc…). Once the page is open, copy paste this code exactly as it is written:
// TJ's first lesson "Hello World" program
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Once you type that out, save it as so. Make sure that the words after the word “class” in the code above matches the name of the file. So in this case, save the file with name HelloWorld. Also, the extension for the code you will write is always “.java,” so the filename will be: “HelloWorld.java”
Once you’ve done that, let’s test that everything works. Open your terminal window and go to the directory in which you saved the file. Here are some commands you can use to move around in the terminal:
- “cd [path]” : replace [path] with the actual path or directory
- “ls” : shows all the folders/files in a directory
Now that you’ve found the file (you are in the correct directory), type “javac HelloWorld.java” and make sure that it doesn’t return any errors. If everything works as planned, it should look almost like nothing happened. If there are errors, you will see them. Once that is done, you know the program you wrote has been compiled. In fact, if you type “ls”, you will see that there is a new file called “HelloWorld.class”. This is because when you compile source code, a class file is created that is essentially in computer language.
Now that you’ve compiled the code, type “java HelloWorld” and watch what happens…
Type any problems, comments, and what you saw in the terminal after you completed the final step in the comment box below and let me know what you thought of this first tutorial! All feedback is appreciated. Look forward to getting your questions and/or thoughts!
[...] Just started up! I just finished writing the first tutorial in Java, so please check it out! To find it, simply look to the right navigation menu, under “JAVA TUTORIALS” or simply click here. [...]