Java Reference
In-Depth Information
program is. This provides an unheard-of measure of portability. A Java program
runs the same on a Macintosh, a PC, Linux, Sun's Solaris —on any computer that
implements a Java compiler and the JVM correctly. This is not the case, for
example, with programs written in C, C++, and Fortran. Why? Because these
programs interact directly with hardware, so on different hardware (i.e. on dif-
ferent kinds of computers), they may do different things. Java programs instead
interact with a JVM (which in turn interacts with hardware), so any operating
system that has a JVM can run any Java program.
The introduction of the Java Virtual Machine did have a drawback: pro-
grams ran slower in Java than in, say, C. Figure 0.5 explains this. At the bottom,
you see that a C program is compiled into the language of the machine on which
the compiler is running, and the resulting program is then executed by the
machine. A Java program, on the other hand, is compiled into the JVM, and then
another machine-language program has to execute the resulting JVM program.
This makes executing (or interpreting ) a program up to ten times slower.
However, we are often quite willing to pay the price of slower programs
because in return we have a portable and simpler language. In fact, we do not
even notice the change in speed on many programs. Further, if a particular part
of a program has to be faster, we can program it in C (or another suitable lan-
guage).
Fourth, the language is safe . As an example of what this means, if a Java
program stores an integer somewhere, it is guaranteed that it will be used only as
an integer (and not, for example, as a string of characters). This safety has ram-
ifications that cannot be completely explained at this point, but here are two
examples. In some languages (e.g. C and C++), managing memory correctly is
very difficult; in Java, safety allows Java to handle memory management, so the
programmer is freed from that task. Secondly, because of safety (and other fac-
tors), a browser can run a Java program called an applet on your computer with
the assurance that it cannot store things on your computer, so it cannot destroy
files or wreak havoc in other ways.
Fifth, the object-oriented nature of Java allows the language itself to be rel-
atively small. Various parts that are usually part of a non-object-oriented lan-
guage are instead written as Java classes that accompany every JVM. Literally
thousands of Java programs, written as classes, come in an Application
Programming Interface (API), which your Java programs can use. For example,
all input and output (I/O) such as reading to or writing from a file, reading from
the keyboard, and drawing on the monitor are defined in classes that accompany
the language. I/O is not part of the language itself. But you do have to learn how
to use these prewritten classes.
 
Search WWH ::




Custom Search