Java is Simple #
Java is an excellent teaching language and an excellent choice with which to learn programming. The language is small so it’s easy to become fluent. The language is interpreted so the compile-run-link cycle is much shorter. The runtime environment provides automatic memory allocation and garbage collection so there’s less for the programmer to think about. Java is object-oriented unlike Basic so the beginning programmer doesn’t have to unlearn bad programming habits when moving into real-world projects. Finally, it’s very difficult (if not quite impossible) to write a Java program that will crash your system, something you can’t say about any other language.
Java is Object-Oriented #
In object-oriented programs data is represented by objects. Objects have two sections, fields (instance variables) and methods. Fields tell you what an object is. Methods tell you what an object does. These fields and methods are closely tied to the object’s real-world characteristics and behavior. When a program is run messages are passed back and forth between objects. When an object receives a message it responds accordingly as defined by its methods.
Java is Platform Independent #
Java is compiled to an intermediate form called byte-code. A Java program never really executes natively on the host machine. Rather a special native program called the Java interpreter reads the byte code and executes the corresponding native machine instructions Thus to port Java programs to a new platform all that is needed is to port the interpreter and some of the library routines. Even the compiler is written in Java. The byte codes are precisely defined, and remain the same on all platforms.
Java is Safe #
Java was designed around the principle of secure execution of code across a network This required the elimination of many features of C and C++. Most notably there are no pointers in Java. Java programs cannot access arbitrary addresses in memory. All memory access is handled by the trusted runtime environment. Furthermore, Java has strong typing Variables must be declared, and variables do not change types when you aren’t looking. Casts are strictly limited to casts between types that make sense. Thus you can cast an int to a long or a byte to a short but not a long to a boolean or int to a String.
Java implements a robust exception handling mechanism to deal with both expected and unexpected errors. The worst that an applet can do to a host system is to bring down the runtime environment. It cannot bring down the entire system.
Most importantly Java applets can be executed in an environment that prohibits them from introducing viruses, deleting or modifying files, or otherwise destroying data and crashing the host computer. A Java-enabled web browser checks the byte codes of an applet to verify that it doesn’t do anything nasty before it will run the applet.
Java, by making it easier to write bug-free code, substantially improves the security of all kinds of programs.
Java is High Performance #
Java byte codes can be compiled on the fly by using a “just-in-time compiler.” Several companies are also working on native-machine-architecture compilers for Java. These will produce executable code that does not require a separate interpreter, and that is indistinguishable in speed from C++.
The speed and performance of the Java program are suitable for all but the most demanding applications.
Java is Multi-Threaded #
Java is inherently multi-threaded. A single Java program can have many different threads executing independently and continuously. Three Java applets on the same page can run together with each getting equal time from the CPU with very little extra effort on the part of the programmer.
This makes Java very responsive to user input. It also helps to contribute to Java’s robustness and provides a mechanism whereby the Java environment can ensure that a malicious applet doesn’t steal all of the host’s CPU cycles.
Java is Dynamic #
Java does not have an explicit link phase. Java source code is divided into java files, roughly one per class in your program. The compiler compiles these into .class files containing byte code. Each class from the java file is compiled into a separate .class file.
The compiler searches the current directory and directories specified in the CLASSPATH environment variable to find other classes explicitly referenced by name in each source code file. If the file you’re compiling depends on other, non-compiled files the compiler will try to find them and compile them as well. The compiler is quite smart and can handle circular dependencies as well as methods that are used before they’re declared. It also can determine whether a source code file has changed since the last time it was compiled.
More importantly, classes that were unknown to a program when it was compiled can still be loaded into it at runtime. For example, a web browser can load applets of differing classes that it’s never seen before without recompilation.
Furthermore, Java .class files tend to be quite small, a few kilobytes at most. It is not necessary to link in large runtime libraries to produce a (non-native) executable. Instead, the necessary classes are loaded from the user’s CLASSPATH.
Java is Garbage Collected #
You do not need to allocate or deallocate memory in Java explicitly. Memory is given as needed, both on the stack and the heap, and reclaimed by the garbage collector when it is no longer needed. There are no malloc(), free(), or destructor methods.
There are constructors and these do allocate memory on the heap, but this is transparent to the programmer.
The exact algorithm used for garbage collection varies from one virtual machine to the next. The most common approach in most VMs is generational garbage collection for short-lived objects, followed by mark and sweep for longer-lived objects.