Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Virtual Machine

JavaFAQ Home » General Java Go to all tips in General Java

Bookmark and Share

Java: Java Virtual Machine

After you read this section, you should be able to understand how it's possible for a Java applet/application to run on many different machines. The key to Java's portability and security is the Java Virtual Machine.

The Java Virtual Machine

Most compilers translate from the source language (eg, C or Pascal) into machine language for one specific type of machine (eg, Pentium or Sun). The machine language for a Sun computer is not the same as for a Macintosh, or a Windows machine. You cannot use the same machine code program on different kinds of machines.

Most compilers produce code for a real machine

The Java compiler produces code for a virtual machine.

Java compilers translate applets in two steps. The first part of the translation is from Java source to Java Virtual Machine (JVM) machine code. This may seem strange because there isn't any real machine that can use JVM instructions yet!

JVM code is designed so that it is easy to translate it into machine instructions for real machines, so the second part of the translation to real machine instructions is done in the browser on the user's machine. The browser takes the applet JVM code that it gets from the server and translates from JVM code to the machine code the browser is using, eg, a Pentium. This compiler in the browser is often called a "JIT compiler". JIT stands for "Just In Time", and it means that the last part of the compilation is done just before running the program. The diagram below shows how the same applet JVM code is used by browsers on different machines.

Click on the picture to see full size image.

Two step compilation allows Java applets to run on many kinds of machines.

Other languages can use the JVM

See www.robert-tolksdorf.de/vmlanguages.html for a list of languages that generate code for the Java Virtual Machine.

Java portability

So applets are really JVM code that is translated into the correct machine code by the browser. That's why applets can run on any machine, and C and C++ programs cannot.

This same two step translation can be used for Java applications as well as applets. If a program is in JVM code, then it is portable, and can be run on any machine that has a JIT compiler on it.

C and C++ object programs are not portable because they are always translated into the machine language for a specific machine. If you are very careful when you write your C programs, you may be able to compile and run the source program on many machines, but there are often big problems. For example, how big is a C int? On some machines it is 16 bits, on some machines it is 32 bits, and some it is 64 bits. Java has defined the basic types very exactly (eg, ints are always 32 bits). Another big problem is that lack of standard library functions in C/C++. There are some standard library functions for C/C++, but they are very small compared to Java, and don't do many of the things that programmers need to do (eg build a graphical user interface).

Portability is extremely important to many companies, since large companies usually have many kinds of computers. If a company can "write once and run everywhere" they can save an enormous amount of money. If a developer can do this, they will have a much larger market for their software.

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy