Indeed, the story of how Java came to be can be found at SunWorld Online. Indeed, many other languages can be run on multiple platforms and are even more compact than Java. And, indeed you can download the Java Developer's Kit for free from Sun Microsystems, the develpers of Java.
Its special place in the Internet world derives from the fact that Java code does not directly run the computer's processor, but runs a virtual machine within the computer.
By way of clarification, programmers write source code in text that is readable by humans, and each computer language has a compiler that transforms those text commands into number commands that the computer's processor understands. A program written in Basic or C++ must be compiled by a DOS compiler to run on IBM- compatible DOS machines, or a Mac compiler to run on Macs. That number file is the executable file that can be fed into the processor to run the program. The numbers may tell an IBM computer to get data from memory, or write something to the screen, for example.
In contrast, Java source code is compiled into so-called "bytecodes" that tell an imaginary computer to do those tasks. These are fed into a Java "virtual machine" program (either a runtime interpreter or an Applet viewer) that in turn requests the computer's processor to do the tasks. A web browser is said to be Java-enabled if it contains such an applet viewer.
The ramifications of this process are ideal for programs distributed over the web. One bytecode file runs on all Java-enabled browsers. Typical viruses can't work because they can't run the processor directly, and aren't allowed to write directly to disk or change operating system files.
There is a tiny Java applet just under the title, "Java" at the top of this page, visible if your browser is Java-enabled. It consists of a single button that says, "Click here." When you click on it, the button changes to say, "Hello."
The source code for this tiny applet looks like C++ code, and is not the least bit intuitive, if you don't speak C++ or Java. In fact, the code for a tinier applet that simply writes, "Hello, world!" without a button is about half the size of this one. In contrast, the same program written in Basic would just be:
Print "Hello, world."
Here is the source code for the Hello button:
Hello.java:
import java.awt.Graphics; /** A sample java applet to say hello. These are only comment lines */ import java.awt.*; import java.applet.Applet; public class Hello extends Applet { Button hiButton; public void init() { hiButton = new Button("Click Me!"); add(hiButton); } public boolean action(Event evt, Object what) { if (evt.target == hiButton) { hiButton.setLabel("Hello!"); return true; } else return false; } }
To make this program run in the web brower, I first ran the compiler, called javac.exe that came in the Java Developer's Kit from Sun Microsystems to make the executable file, hello.class. I then put the code into this web page to instruct it to run the applet. It looks something like:
<APPLET Code="hello.class" Width="250" Height="100"> </APPLET>
[ By the way, you can see this HTML (hypertext markup language) code in this web page by telling your browser to show the document source.]
Finally, when the web browser loaded this page, it saw the reference to the applet, downloaded the bytecode file, "Hello.class" and ran it within the 250 by 100 pixel space it allotted on the page.
If you're really curious, you can see the bytecode Hello.class itself displayed below. Most of the bytes don't happen to represent letters, so they are represented by periods in the sixteen columns to the right of the page. They can all be represented by hexadecimal numbers, though, as they are on the sixteen columns on the left side of the page. (Each hexadecimal number is a two-digit number, of the fifteen digits 0-9 and A-F. They represent numbers from 0 to 255 in decimal numbers, which aren't shown below.)
Sorry if this last part is confusing. Computer code is by convention displayed in hexadecimal format instead of decimal. The only point to be made is that Java is written in text, compiled into numbers, and interpreted by the Java-enabled web browser.
Hello.class:
CA FE BA BE 00 03 00 2D-00 2F 08 00 1C 08 00 14 .......-./...... 07 00 16 07 00 2C 07 00-21 07 00 2B 07 00 25 0A .....,..!..+..%. 00 07 00 10 0A 00 05 00-0E 09 00 04 00 11 09 00 ................ 06 00 13 0A 00 03 00 12-0A 00 07 00 0F 0C 00 24 ...............$ 00 2D 0C 00 24 00 20 0C-00 22 00 20 0C 00 29 00 .-..$. ..". ..). 26 0C 00 23 00 2A 0C 00-1B 00 28 01 00 09 43 6C &..#.*....(...Cl 69 63 6B 20 4D 65 21 01-00 0D 43 6F 6E 73 74 61 ick Me!...Consta 6E 74 56 61 6C 75 65 01-00 12 6A 61 76 61 2F 61 ntValue...java/a 77 74 2F 43 6F 6E 74 61-69 6E 65 72 01 00 25 28 wt/Container..%( 4C 6A 61 76 61 2F 61 77-74 2F 45 76 65 6E 74 3B Ljava/awt/Event; 4C 6A 61 76 61 2F 6C 61-6E 67 2F 4F 62 6A 65 63 Ljava/lang/Objec 74 3B 29 5A 01 00 0A 45-78 63 65 70 74 69 6F 6E t;)Z...Exception 73 01 00 0F 4C 69 6E 65-4E 75 6D 62 65 72 54 61 s...LineNumberTa 62 6C 65 01 00 0A 53 6F-75 72 63 65 46 69 6C 65 ble...SourceFile 01 00 08 68 69 42 75 74-74 6F 6E 01 00 06 48 65 ...hiButton...He 6C 6C 6F 21 01 00 0E 4C-6F 63 61 6C 56 61 72 69 llo!...LocalVari 61 62 6C 65 73 01 00 04-43 6F 64 65 01 00 04 69 ables...Code...i 6E 69 74 01 00 15 28 4C-6A 61 76 61 2F 6C 61 6E nit...(Ljava/lan 67 2F 53 74 72 69 6E 67-3B 29 56 01 00 12 6A 61 g/String;)V...ja 76 61 2F 61 70 70 6C 65-74 2F 41 70 70 6C 65 74 va/applet/Applet 01 00 08 73 65 74 4C 61-62 65 6C 01 00 03 61 64 ...setLabel...ad 64 01 00 06 3C 69 6E 69-74 3E 01 00 0F 6A 61 76 d...>init>...jav 61 2F 61 77 74 2F 42 75-74 74 6F 6E 01 00 12 4C a/awt/Button...L 6A 61 76 61 2F 6C 61 6E-67 2F 4F 62 6A 65 63 74 java/lang/Object 3B 01 00 06 61 63 74 69-6F 6E 01 00 11 4C 6A 61 ;...action...Lja 76 61 2F 61 77 74 2F 42-75 74 74 6F 6E 3B 01 00 va/awt/Button;.. 06 74 61 72 67 65 74 01-00 2A 28 4C 6A 61 76 61 .target..*(Ljava 2F 61 77 74 2F 43 6F 6D-70 6F 6E 65 6E 74 3B 29 /awt/Component;) 4C 6A 61 76 61 2F 61 77-74 2F 43 6F 6D 70 6F 6E Ljava/awt/Compon 65 6E 74 3B 01 00 05 48-65 6C 6C 6F 01 00 0E 6A ent;...Hello...j 61 76 61 2F 61 77 74 2F-45 76 65 6E 74 01 00 03 ava/awt/Event... 28 29 56 01 00 0A 48 65-6C 6C 6F 2E 6A 61 76 61 ()V...Hello.java 00 01 00 06 00 05 00 00-00 01 00 00 00 1B 00 28 ...............( 00 00 00 03 00 01 00 1F-00 2D 00 01 00 1E 00 00 .........-...... 00 37 00 04 00 01 00 00-00 17 2A BB 00 07 59 12 .7........*...Y. 02 B7 00 0D B5 00 0B 2A-2A B4 00 0B B6 00 0C 57 .......**......W B1 00 00 00 01 00 19 00-00 00 0E 00 03 00 00 00 ................ 0E 00 0D 00 0F 00 16 00-0D 00 01 00 27 00 17 00 ............'... 01 00 1E 00 00 00 3C 00-02 00 03 00 00 00 18 2B ......<........+ B4 00 0A 2A B4 00 0B A6-00 0E 2A B4 00 0B 12 01 ...*......*..... B6 00 08 04 AC 03 AC 00-00 00 01 00 19 00 00 00 ................ 12 00 04 00 00 00 13 00-0B 00 14 00 14 00 15 00 ................ 16 00 18 00 01 00 24 00-2D 00 01 00 1E 00 00 00 ......$.-....... 1D 00 01 00 01 00 00 00-05 2A B7 00 09 B1 00 00 .........*...... 00 01 00 19 00 00 00 06-00 01 00 00 00 0A 00 01 ................ 00 1A 00 00 00 02 00 2E ........
For an expample of a Java applet, see this simulator of a nuclear reactor, or this Earthweb's Cool Java Things.
return to Wesley's Page