What Is a Java Compiler? - dummies

By Barry Burd

If you’re a seasoned veteran of computers and computing, you can try installing the required software for creating Java programs. You’ll need the three things listed below in order to start

“A compiler takes the Java code that you write and turns that code into something that can run on your computer.”

Barry Burd, Java For Dummies, 6th Edition

So how many tools do you need for creating Java programs? As a novice, you need three tools:

  • A compiler takes the Java code that you write and turns that code into something that can run on your computer.

  • A Java Virtual Machine runs your code (and other peoples’ Java code) on your computer.

  • An integrated development environment helps you manage your Java code and provides convenient ways for you to write, compile, and run your code.

// This is part of a Java program
// (not a complete Java program).
roomNum = 1;
while (roomNum < 100) {
    if (guests[roomNum] == 0) {
        out.println("Room " + roomNum
                + " is available.");
    } else {
out.println("No vacancy");

The Java code here checks for vacancies in a small hotel (a hotel with room numbers 1 to 99). You can’t run the code without adding several additional lines. But those additional lines aren’t important. What’s important is that, by staring at the code, squinting a bit, and looking past all the code’s strange punctuation, you can see what the code is trying to do:

Set the room number to 1.
As long as the room number is less than 100,
    Check the number of guests in the room.
    If the number of guests in the room is 0, then 
        report that the room is available,
        and stop.
        prepare to check the next room by 
        adding 1 to the room number.
If you get to the non-existent room number 100, then
    report that there are no vacancies.

If you don’t see the similarities between the code and its English equivalent, don’t worry, like most human beings, you can learn to read and write the code. It’s called Java source code.

So here’s the catch: Computers aren’t human beings. Computers don’t normally follow instructions like the instructions in the listing. That is, computers don’t follow Java source code instructions. Instead, computers follow cryptic instructions like the ones in the listing.

putfield Hotel/roomNum I
goto 32
getfield Hotel/guests [I
getfield Hotel/roomNum I
ifne 26
getstatic java/lang/System/out Ljava/io/PrintStream;
new java/lang/StringBuilder
ldc "Room "
invokespecial java/lang/StringBuilder/<init>(Ljava/lang/String;)V
getfield Hotel/roomNum I
invokevirtual java/lang/StringBuilder/append(I)Ljava/lang/StringBuilder;
ldc " is available."
invokevirtual java/lang/StringBuilder/toString()Ljava/lang/String;
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
invokestatic java/lang/System/exit(I)V
goto 32
getfield Hotel/roomNum I
putfield Hotel/roomNum I
getfield Hotel/roomNum I
bipush 100
if_icmplt 5
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "No vacancy"
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

The instructions in this listing aren’t Java source code instructions. They’re Java bytecode instructions. When you write a Java program, you write source code instructions (like the instructions listed first). After writing the source code, you run a program (that is, you apply a tool) to your source code.

The program is a compiler. The compiler translates your source code instructions into Java bytecode instructions. In other words, the compiler takes code that you can write and understand and translates it into code that a computer can execute (like the code here).

You might put your source code in a file named Hotel.java. If so, the compiler probably puts the Java bytecode in another file named Hotel.class. Normally, you don’t bother looking at the bytecode in the Hotel.class file. In fact, the compiler doesn’t encode the Hotel.class file as ordinary text, so you can’t examine the bytecode with an ordinary editor.

If you try to open Hotel.class with Notepad, TextEdit, KWrite, or even Microsoft Word, you’ll see nothing but dots, squiggles, and other gobbledygook. To create a listing, you have to apply yet another tool to your Hotel.class file. That tool displays a text-like version of a Java bytecode file. You can use Ando Saabas’s Java Bytecode Editor.

No one (except for a few crazy developers in some isolated labs in faraway places) writes Java bytecode. You run software (a compiler) to create Java bytecode. The only reason to look at this listing is to understand what a hard worker your computer is.