Java – Getting Started

Compiler vs. Interpreter

Compiler – Checks syntax.
Interpreter – Checks syntax.

Compiler – Generates machine-code instructions. Running the program is a separate step.
Interpreter – Executes the program statements as it converts them to machine-code.

Compiler – Is not needed to run the executable program.
Interpreter – Must remain installed while program is run.

Compiler – Runs faster.
Interpreter – Is slower.

Generally, when you compile a program, you’re creating machine language for a specific platform. For example, you might compile a program to run on a Windows 2000 platform. If you want to run the same program on an Apple computer, you can use a compiler that creates machine language for a Mac OS X platform.

When you compile a Java program, it doesn’t create machine language, it creates bytecode. To run a Java program, the computer running the program needs a Java Virtual Machine to convert the bytecode into machine language. This arrangement allows you to write once, then run anywhere that has a Java Virtual Machine.

Java programs can be written in any text editor, such as Microsoft Notepad. When you save a Java program, you should save it with a .java extension. For example, if you were to create a program that writes the famous phrase “Hello world“, you might save it as HelloWorld.java.

In addition to using the .java extension, you should use UpperCamelCase for the program name. CamelCase is the practice of writing compound words or phrases where the words are joined without spaces or dashes or underscores, and each new word is capitalized. The name comes from the camel-like “bumps” in the middle of the compound word that are created by the uppercase letters. When you name something in CamelCase that starts with a lower case letter, it’s referred to as lowerCamelCase. In Java, there’s a convention that you should use lowerCamelCase for variable names. On the other hand, names starting with an upper case letter are referred to as UpperCamelCase. The compiler, however, doesn’t care which case you use for any of these names, as long as you are consistent.

To compile Java programs using the Standard Edition 5.0 of the Java 2 platform, you need to download JDK 5.0 Update 6.

To compile HelloWorld.java, you type in javac HelloWorld.java at the MS-DOS Prompt (in Windows 95/98) or Command Prompt (in Windows NT/XP). This will create bytecode in HelloWorld.class.

To run HelloWorld.class, you type java HelloWorld.class.

A HelloWorld program could look like this:

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

The first line starts with the word class. This is just a reserved word that tells java that we’re building a new class. The other word on the first line is HelloWorld. This is the name that we have chosen for this class, and it MUST match the program name. By convention, all class names should be UpperCamelCase so programmers will recognize that it is a class name, not a variable name. The entire first line is a class header.

The second line of the program is an opening curly brace. This defines the beginning of the class body. The last line of our program is a closing curly brace. This indicates the end of our class. Everything between these two curly braces is part of class HelloWorld.

The HelloWorld class contains one method. Method is just another name for something that is called a function, procedure, paragraph, or subroutine in other languages. It’s a block of code that can be called from another location. In this case, it’s called from the run statement. All java applications start at a method named main().

The third line is a method header for the main() method. It’s followed by curly braces that define the body of the method and enclose a println statement that sends the literal “Hello World” to the screen.

This particular method starts with the word public. Public is an access modifier. It lets Java know who’s allowed to call this method. Public is the least restrictive access modifier.

The next word is static. Static changes main() from an instance method to a class method. Yes, I understand that you don’t know what this means yet.

Static is followed by the word void, which indicates that the main method doesn’t send a value back to the place that called it. If it did return a value, we would replace the word void with the data type of the returned value.

The method body contains a single statement:
System.out.println(“Hello World”);
It ends with a semicolon. All statements end with a semicolon. Class headers and method headers are not statements.

The word System is a class. It starts with an uppercase letter.
The word out is an object.
The word println() is a method.
The statement System.out.println(); is a method call.
The statement System.out.println(“Hello World”); is a method call that passes a literal argument to the method.

If you’ld like to try it for yourself, here are some good instructions and a closer look at a Hello World program from the folks at Sun.

A good learning path would be to continue writing Applications.

Once you have a good foundation writing applications, you can start learning about Applets.

Applets
Building Applets

At some point, you may want to start using an Integrated Development Environment.

Here are some Java Tips.

Step by Step Programming.

AWT Swing1 Swing2

Free University of Bolzano/Bozen.

Did you like this? Share it:
This entry was posted in Uncategorized by Joel. Bookmark the permalink.

Leave a Reply