Java Reference
In-Depth Information
With Java, you are not concerned with the target platform. The exact same
program can run on Windows and the Palm OS, without changing the code at
all. This “write once, run anywhere” capability is an exciting feature of Java
that makes it appealing for anyone developing software.
Classroom Q & A
Q: So, how is platform independence possible?
A: Well, I have been leading you up to that question. How do you
think it is possible?
Q: I am assuming you recompile the program, using a compiler
designed for the specific platform you are targeting.
A: Yes and no. You do use a compiler for a specific platform, but
there is no recompiling. In fact, compiled Java code, which is
referred to as bytecode , is well defined and looks the same no
matter what type of device you are targeting. This is because in
Java, the platform you target is a Java Virtual Machine, or JVM for
short. You do not write Java programs for Windows, Unix, a Palm
PC, or any other device. You write Java programs to run on a JVM.
Q: So if I want my Java program to run on Windows, I need a JVM for
Windows?
A: Exactly. And if you want your Java program to run on your watch,
you need a JVM for your watch. If you want a Java program to run
on your cell phone, you need a JVM for your cell phone, and so on.
Q: And the JVMs are written in Java?
A: No. Interestingly enough, most JVMs are written in C or C++.
When you run a Java program, you are really running a JVM, and
the JVM is interpreting your Java code.
Q: This must make Java programs considerably slower.
A: That is a definite concern. Five years ago, I would have had to con-
cede that a Java program was noticeably slower than a C or C++
program. But modern JVMs are much more efficient and include a
feature known as a Just-In-Time (JIT) compiler. A JIT compiler actu-
ally takes your Java bytecode and translates it into native code.
This translated code will run just as fast as any C++ program.
There is more overhead at the beginning of the Java program
when the code is being translated, but the end result of a JIT com-
piler is well worth it.
Search WWH ::




Custom Search