There are two types of programs that you can make with the Java language: applets and applications. Applets are designed to be run inside of a web browser, such as Netscape or Internet Explorer. I can put an applet right into this web page and the program would run on any computer in the world with internet access (and with Java enabled in the browser).
Applications are freestanding and do not need to have a graphical user interface. They do not run across the internet. Your ARHS Java Editor is an example of a Java application. To get started with Java, you will write some simple applications. Here is the basic structure for all of your applications:
public class NameOfProgram{
public static void main(String[] args){
your code goes here
}
}
In the line that starts "public class," you may name the program anything you want. The next line of code sets up a method named "main." You need to write that line exactly the way you see it here. We will learn later what all of that public static void stuff means. For now, just accept it and be careful as you type it. Inside the main method, you will put the lines of code you want to experiment with. The simplest thing to do is to get the program to print a message on the screen. This can be accomplished with this program:
public class MyFirstApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Try that now with the Java editor. Be careful about the curly brackets ({). Those always come in pairs, and you must have a closing bracket to match an opening bracket. In the code above, you see one curly bracket opening the program on the first line, and it is closed on the very last line. There is another curly bracket opening the main method, and it is closed right after the hello world line. When you type an opening bracket, the editor will automatically put in a closing bracket. This will also happen for parentheses and square brackets. If you don't like that feature and would prefer to close them yourself, just click on "Preferences." Every line of Java code ends either with a curly bracket or a semicolon.
Once you have the code typed in properly, choose the icon for save. The editor will arrange for it to be saved in a file named "MyFirstApp.java." Your file names must always match the name that follows the word "class." All of the files will be stored in a folder named "h:\java" on your account. Do not attempt to save anything on the hard drive of your computer, because these machines are re-imaged frequently and you could lose what you saved.
In the BASIC programming environment, you had an interpreter that allowed you to test your programs without compiling them. In Java, you are required to compile them first. Compiling is a procedure that takes the source code you write in Java and converts it to a what is called "bytecode." Java bytecode is the same whether you are using a PC, a Mac, a Linux machine, or any other operating system. Then your particular machine takes the bytecode and interprets it to run properly on that machine (converting it to "machine language"). In this way, Java programs can run on any platform. This is a unique and important feature of Java. With other programming languages, you would have to write a separate version of your program for each kind of computer.
A program must be saved first before it can be compiled. After saving it, click on "Test" and then "Compile." If the program compiles correctly, you will see a message at the bottom of your screen. If errors prevent the program from compiling, you will see a list of those errors in the lower window. Fix them and then save again before you try to recompile. When the source code compiles successfully, there will be a new file in your directory called "MyFirstApp.class." The .class files contain the actual Java bytecode. (If you're curious you might try opening a .class file, but it will just look like gobbledygook. It's not supposed to be something that a human can read.)
Once a program has properly compiled, it's possible to run it and see what it does. Again, click on "Test" and then choose "Run Application" or "Run Applet." The program may still have some errors, even though the compiler didn't pick them up. Those errors will be reported in the lower window. You will also see any output from the program in that same window, so if you ask it to print a message on the screen, that is where it will appear.
There is also a handy shortcut button on the editor, "Compile and Run", which will save your current file, compile it and run it. You can either click on that button, or press Shift-F5.
| Read more about machine language: | David Eck's textbook. |
| Course Home Page | Syntax Sheet | Next Topic |