Java Intro
Java Intro
All a computer can do is follow the instructions that people give to it.
Everything you do with a computer involves gobs and gobs of code. Take a CD-ROM with a
computer game on it. It’s really a CD-ROM full of code. At some point, someone had to write
the game program:
Without a doubt, the people who write programs have valuable skills. These people have two
important qualities:
They know how to break big problems into smaller step-by-step procedures.
They can express these steps in a very precise language.
Computer languages can be very different from one another but, in some ways, they’re all
the same. When you get used to writing IF COLUMN-NUMBER IS GREATER THAN 60,
then you can also become comfortable writing if (columnNumber > 60). It’s just a
mental substitution of one set of symbols for another.
Computer program — a set of instructions telling the computer what to do. Another name
for a program (or part of a program) is code.
Computers deal with zeros and ones. Computer circuits don’t deal directly with letters of the
alphabet. Computers break everything down into very low-level, unfriendly sequences of
zeros and ones, and then put things back together so that humans can deal with the results.
So, what happens when you write a computer program? Well, the program has to get
translated into zeros and ones. The official name for the translation process is compilation.
Without compilation, the computer can’t run your program.
You can write source code, and then get the computer to create object code from your
source code. To create object code, the computer uses a special software tool called a
compiler.
If( columnNumber > 60 ) . . .// Java Source Code ( a .java file)
Your computer’s hard drive may have a file named javac or javac.exe. This file contains that
special software tool — the compiler. (Hey, how about that? The word javac stands for “Java
compiler!”) As a Java programmer, you often tell your computer to build some new object
code. Your computer fulfils this wish by going behind the scenes and running the instructions
in the javac file.
Bytecode (the stuff in a .class file) contains a complete description of the operations that the
computer is to perform. When you write a computer program, your source code describes an
overall strategy — a big picture. The compiled bytecode turns the overall strategy into
hundreds of tiny, step-by-step details. When the computer “runs your program,” the
computer examines this bytecode and carries out each of the little step-by-step details.
That’s how it works in many programming languages, but that’s not how it works in Java.
There’s a special piece of software that carries out the instructions in Figure 1-4. That special
piece of software is called the Java virtual machine (JVM). The JVM walks your computer
through the execution of some bytecode instructions. When you run a Java program, your
computer is really running the Java virtual machine. That JVM examines your bytecode, zero
by zero, one by one, and carries out the instructions described in the bytecode.
With most programming languages — the computer runs some object code. But with Java —
the computer runs the JVM, and the JVM follows the bytecode’s instructions.
For years, computer professionals were seeking the Holy Grail — a way to write software so
that it’s easy to reuse. Don’t write and rewrite your search-and replace code. Just break the
task into tiny pieces. One-piece searches for a single character, another piece looks for blank
spaces, a third piece substitutes one letter for another. When you have all the pieces, just
assemble these pieces to form a search-and-replace program. Later on, when you think of a
new feature for your word processing software, you reassemble the pieces in a slightly
different way. It’s sensible, it’s cost efficient, and it’s much more fun.
Java came along in 1995, so it was natural for the language’s founders to create a library of
reusable code. The library included about 250 programs, including code for dealing with disk
files, code for creating windows, and code for passing information over the Internet. Since
1995, this library has grown to include more than 3,000 programs. This library is called the
API — the Application Programming Interface
Every Java program, even the simplest one, calls on code in the Java API. This Java API is both
useful and formidable. It’s useful because of all the things you can do with the API’s
programs. It’s formidable because the API is so extensive. No one memorizes all the features
made available by the Java API. Programmers remember the features that they use often,
and look up the features that they need in a pinch. They look up these features in an online
document called the API Specification (known affectionately to most Java programmers as
the API documentation, or the Javadocs). The API documentation describes the thousands of
features in the Java API. As a Java programmer, you consult this API documentation on a
daily basis. You can bookmark the documentation at the Sun Microsystems Web site and
revisit the site whenever you need to look up something. But in the long run (and in the not-
so-long run), you can save time by downloading your own copy of the API docs.
Write Once, Run Anywhere: When Java first hit the tech scene in 1995, the language became
popular almost immediately. This happened in part because of the Java virtual machine. The
JVM is like a foreign language interpreter, turning Java bytecode into whatever native
language a particular computer understands. So, if you hand my Windows computer a Java
bytecode file, then the computer’s JVM interprets the file for the Windows environment. If
you hand the same Java bytecode file to my colleague’s Macintosh, then the Macintosh JVM
interprets that same bytecode for the Mac environment. Look again at Figure 1-5. Without a
virtual machine, you need a different kind of object code for each operating system. But with
the JVM, just one piece of bytecode works on Windows machines, Unix boxes, Macs, or
whatever. This is called portability, and in the computer programming world, portability is a
very precious commodity. Think about all the people using computers to browse the
Internet. These people don’t all run Microsoft Windows, but each person’s computer can
have its own bytecode interpreter — its own Java virtual machine. The marketing folks at
Sun Microsystems call it the Write Once, Run Anywhere model of computing. I call it a great
way to create software.
Your Java Programming Toolset:
You need a Java compiler.
You need a Java virtual machine.
You need the Java API. (“Code you can use.”)
You need the Java API documentation.
You need an editor to compose your Java programs. And You need a way to issue
commands.
In the best of all possible worlds, you do all your program editing, documentation reading,
and command issuing through one nice interface. This interface is called an integrated
development environment (IDE).
Java is case-sensitive, which means that system.out.printLn isn’t the same as
System.out.println. If you type system.out.printLn, you’re program won’t work.