Introduction to Java for Programmers

Java from the Command Line

Built-In Types and Objects

Access Levels: public, protected, private, and package-private

static and final



Some Other Differences

Interfaces and Inheritance


This page is designed as a self-help guide for learning Java. In this guide, I'm not planning on going into lots of detail on how programming in general works—I'm assuming that you already understand the basics of programming, especially C/C++. That's why I'm calling it an introduction for programmers. I'm creating this mostly for JSIG, but I figure others might be able to use it as well.

A Little About Java

Java is a multi-purpose programming language that is designed to provide a cross platform object oriented paradigm with a syntax that is similar to C and C++. It is often used for interactive environments and web-based applications, so there is a lot of support for user interfaces and all sorts of networking components.

The Java programming language is cross-platform, meaning that it will run on any type of system or architecture. So, for example, when you're writing C++ code, if you want to execute your program on a Windows machine, you will need to compile it with a Windows compiler. If you want to run it on a Linux machine, you need to compile it with a Linux compiler. With Java, you can compile it once, and it will run on any platform. This is possible because Java uses a virtual machine architecture. In this case, a virtual machine is simply a software program that can execute compiled code. The Java Virtual Machine (often abbreviated JVM) takes the compiled Java code, which is in a form called bytecode and converts it into code that the hardware and operating system can execute. So, in a loose sense Java code is interpreted. However, a great deal has gone into optimizing Java. The JVM will compile sections of code as they are executed the first time, so they do not need to be reprocessed repeatedly. Java has a reputation for being slow, which was true at the beginning, but it has come a long way since then, and is fairly competitive with even C++. In fact, there are those out there who claim that eventually Java will be faster than C++.

The Java programming language is extremely simple. In fact, because of its simplicity, there are a lot of schools that teach it as their introduction to programming, rather than C++. And honestly, if you already know C++, there is very little that you will have to learn to get a grasp of the Java language. (In fact, you'll probably have to do more forgetting than learning.) Aside from the language, though, Java has a huge number of libraries that can be used. And in addition, they are very well documented. A particularly useful library called the Java 2 Platform API comes when you install Java. Once you get a grasp of the basic language features, you'll be able to go ahead and start learning some of the important and useful parts of this library, as well as other libraries. But in reality, you can learn these as the need comes up.

A Simple Example

The first thing on our list is to create a simple program, so that you can see how it works. Below is some sample code that will print out random numbers:

package demo;
import java.util.Random;
public class DemoClass
    public static void main(String[] args)
        Random random = new Random();
        int total = Integer.parseInt(args[0]);
        for(int i = 0; i < total; i++)
            int number = random.nextInt(100);

Let's take a moment and explain this code. The first line states what package this class is in. Packages are logical groupings of related code. All classes should be in a package, though if you leave this off, it will still compile. In that case, the class would be in the "default" package. It is usually a bad idea to use the default package.

Below that we have the line that says import java.util.Random;. This line is similar to the #include directives in C++ and using statements in C#. This statement works different than a #include statement, though. In C++, #include statements tell the compiler to go find the listed code and embed it into your file. In Java, import statements just tell the compiler what you are referring to when you use the type listed. So for instance, our import statement is simply