JVMs and applets vs applications

JVM vs Scripting

Java is an interpreted language. For several reasons this does not mean it is a scripting type language. The code is compiled into what is called "Java byte code", a psuedo machine code, and is executed by a "Java Virtual Machine" which is an emulator for an abstract computer type. (I think SUN has actually built hardware versions of this "Java computer".) Normally, emulation is a slow way to run software and this was true of early versions of Java. Some JVMs now have JIT (just-in-time) compilers which recompile the Java byte code into native processor code as they run it to improve performance. The JDK we are using for this class has this feature. We will check actual peformance later. The Java byte code was designed to make it fairly easy to do this dynamic recompiling. An interpreted scripting language seems the same on different platforms, but if you have tried to use one in different environments, you know that you have to relearn how to talk to the host operating system to accomplish anything interesting. The Java VM adds a well defined abstraction layer between the Java code and the native operating system, so that you always request services the same way and the JVM translates the request into OS specific services transparently (at least in theory).

Applets vs Applications

There are actually two types of Java Virtual Machine: The "application" JVM and the "applet" JVM. The application JVM is a program generally called "java.exe" which is native machine code. When you start it, you pass it the name of the Java program you want to run and it loads the program and runs the byte code in a virtual environment. The applet JVM is usually a subsystem in a Web browser like Netscape's Communicator or Microsoft's Internet Explorer. It downloads Java byte code programs and runs them in it's version of a JVM, which is called a "sandbox". I think the term means that these applications are only "play" applications as far as the executing computer is concerned. The reason is that applets are not allowed by the applet JVM (as a default) to access any files or data on the system they are run on, nor on any other system other than the one where the applet was loaded from.  This greatly limits their usefulness, but also prevents any nasty "virus" type behaviours. Web browsers which have JVMs recognize a special HTML tag, "<applet>" which tells the browser things like what Java code to download, what parameters to pass to it, and how big of a "panel" in the displayed document to allow the Java applet to interact with the user in. SUN also provides a program with the Java environment called "appletviewer". It is basically designed to allow you to test applets you develop. It can load and execute applets in the same way as a Web browser would, but does not do any of the other stuff that normal web browsers do, such as display documents and follow links. You give it the name of an HTML document when you start it. Here is an example of an HTML document which loads and executes some Java applets:

 <applet code=HelloWeb width=300 height=200> </applet>

 <applet code=HelloWeb2 width=300 height=200>
 <param name="message" value="Hello Kermit"> </applet>

 <applet code=HelloWebNR width=300 height=200> </applet>

 <applet code=HelloWeb2NR width=300 height=200>
 <param name="message" value="Hello NR2"> </applet>

What is the difference between a Java application and a Java applet from a programmers viewpoint? I am glad you asked that question! A Java application has one entry point which is called when the program is loaded, a code segment called "main" just as in other languages such as C/C++. In contrast, an applet does not get called at the entry point "main", but is called at several entry points, depending on what the browser is doing at the time. When the applet is first loaded, the "init" entry point is called. When the applet becomes visible on the browser screen, it is called at "start". When it is no longer visible, entry point "stop" is called. When the browser feels that the applet needs to refresh it's display area, it calls the "paint" entry point. There are other differences, but those are the most basic and enough for now.