posted 21 years ago
Here's what's going on "behind the scenes" with Java. The Java language is extremely portable and powerful because it is (at different levels) both a compiled language and an interpreted language.
In an interpreted language (such as PERL or LISP or a shell script/batch file), the code must be read and parsed. This means that your machine must have something installed to parse the file; you cannot run a Python script without the Python interpreter present. Interpreted code is very flexible; you could write a PERL script to work on any platform. However, since the interpreter is required, each platform must have its own interpreter.
Interpreting a script is a time-consuming process, since each time the program is run the script must be re-parsed. Compiling a language allows you to turn your code into efficient, machine-understandable code that does not need to be parsed every time.
(Rather silly) Example: You have a program with 1000 lines of code. 999 lines are comments describing what you intend the program to do and the entire revision history of the project. The remaining line reads 'print "Hello, World"'
An interpreter would have to parse the file every time, reading the 999 lines of comments (even if it does nothing with them, it still has to figure out where the comments end...) and then executing the code. A compiled language would parse the file once, throw out all the comments, optimize the code, and then create a new file that can be run to print "Hello, World". Every time the resulting file is run, it is optimized so that the machine can efficiently print "Hello, World"
The problem with compiling a program is that different platforms have different machine-level code sets; a Windows processor has no clue how to run a program compiled for a Mac Classic. To write code that is portable, it must be interpreted.
Enter Java.
Java provides a special kind of interpreter called a virtual machine. There are different Virtual Machines out there: one to interpret for UNIX, one to interpret for Windows, one to interpret for your cell phone, etc. They all take the same input and produce different output.
The difference between a Virtual Machine and a classic interpreter is what they are interpreting. A classic interpreter has to parse the source file every time (like our file with 999 lines of comments). A virtual machine takes a standard set of "virtual machine instructions" and interprets them for the platform that the VM is running on. These virtual machine instructions are the bytecode that is produced when you compile your java code. what javac does is exactly like the compiler above; it strips out the comments, optimizes the code, etc. But the resulting file is not intended for a UNIX or Windows processor; it is intended for the JVM. The JVM then interprets this code for the specific platforms.
So, to answer your questions:
1). No, the byte code is not truly "executable." It must be interpreted by the JVM
2). Yes, the JVM must be present. The code does not take the JVM with it; it must be installed separatly. You can either have the person installing your program make sure that java is installed -OR- use an installation program (such as InstalAnywhere) to automatically detect if the JVM is installed and install it if not.
3). This is different from C++ in that C++ must be recompiled on every platform you want to run it on (but Java requires the JVM to be present, where a compiled C++ program is truly "stand-alone"). Using java, you compile your program once and then any JVM can run it (depending on version, of course...)
This is different from VB in that Java works on more platforms than just Windows....
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.