Java (programming language)
The Java language is an object-oriented programming language created by James Gosling and other engineers at Sun Microsystems. It was developed in 1991, as part of the Green Project, and officially announced on May 23, 1995, at SunWorld; being released in November. Gosling and friends initially designed Java, which was called Oak at first (in honour of a tree outside Gosling's office), to replace C++ (although the feature set better resembles that of Objective C). The Java specifications are community-maintained through the Sun-managed Java Community Process. Sun holds a trademark on the Java name.
Overview
There were four primary goals in the creation of the Java language:
- It is object-oriented.
- It is independent of the host platform (more or less).
- It contains language facilities and libraries for networking.
- It is designed to execute code from remote sources securely.
Object orientation
The first characteristic, object orientation ("OO"), refers to a method of programming and language design. The main idea of OO is to design software around the "things" (ie. objects) it manipulates, rather than the actions it performs. This is based on the notion that the former change less frequently and radically than the latter, making such objects (actually the entities containing data) the more stable foundation for a software system's design. The intent is to make large software projects easier to manage, thus improving quality and reducing the number of failed projects.
Roles
The Java software plays three crucial roles or identities in the field of software:
- as a programming language.
- as middleware.
- as a platform.
A crucial component of Java is the Java Virtual Machine (JVM). The availability of the JVM on almost all types of device, chips and software package enables Java to function both as middleware and a computing platform. The JVM enables Java to be "platfom independent" since the JVM on each device/chip interprets the standard Java bytecode. This means you can develop code on your PC and expect it to run on Java-enabled phones as well as on mainframes equipped with Java without any adjustments. Hence the expression "Write once, run anywhere." This also represents a huge amount of savings for software developers as code can be developed locally and cheaply.
Platform independence
The second characteristic, platform independence, means that programs written in the Java language must run similarly on diverse hardware. One should be able to write a program once and run it anywhere. This is achieved by most compilers by compiling the Java language code "halfway" to bytecode—simplified machine instructions specific to the Java platform. The code is then run on a virtual machine (VM), a program written in native code on the host hardware that translates generic Java bytecode into usable code on the hardware. Further, standardized libraries are provided to allow access to features of the host machines (such as graphics, threading and networking) in unified ways.
There are also implementations of Java compilers that compile to native object code, such as GCJ, removing the intermediate bytecode stage, but the output of these compilers can only be run on a single architecture.
Sun's license for Java insists that all implementations be "compatible". Some vendors, notably Microsoft, have insisted on adding platform-specific features (for example, in Microsoft's case, for Windows) and Sun has reacted strongly. After Microsoft insisted on retaining its incompatible variations, Sun sued and won both damages (some $20 million dollars) and a court order enforcing the terms of the license from Sun.
Microsoft has taken the position that a compliant Java, meeting the terms of the license as ordered by the court, is inadequate and has decided to leave out any Java system at all in future versions of Microsoft Windows. This means that stock versions of Internet Explorer in such versions of Windows will break for Web sites using Java applets. Sun and others have made available Java run-time systems at no cost for those versions of Windows without Java. The necessity to download them is a consequence of the litigation and Microsoft's decision to comply with the court's order by leaving out Java support.
The first implementations of the language used an interpreted virtual machine to achieve portability, and many implementations still do. These implementations produce programs that run more slowly than the fully-compiled programs created by the typical C++ compiler and some later Java-language compilers, so the language suffered a reputation for producing slow programs. More recent implementations of the Java VM produce programs that run much faster, using multiple techniques. There is still a speed difference, but it is much smaller.
The first technique is to simply compile directly into native code like a more traditional compiler, skipping bytecodes entirely. This achieves great performance, but at the expense of portability. Another technique, known as just-in-time compilation (JIT), compiles the Java bytecodes into native code at the time that the program is run. More sophisticated VMs even use dynamic recompilation, in which the VM can analyze the behavior of the running program and selectively recompile and optimize critical parts of the program. Both of these techniques allow the program to take advantage of the speed of native code without losing portability.
Portability is a technically difficult goal to achieve, and Java's success at that goal is a matter of some controversy. Although it is indeed possible to write programs for the Java platform that behave consistently across many host platforms, the large number of available platforms with small errors or inconsistencies led some to parody Sun's "Write once, run anywhere" slogan as "Write once, debug everywhere".
Platform-independent Java is, however, very successful with server-side applications, such as web services, servlets, or Enterprise Java Beans.
Secure execution of remote code
The Java platform was one of the first systems to provide wide support for the execution of code from remote sources. An applet could run within a user's browser, executing code downloaded from a remote HTTP server. The remote code runs in a highly restricted "sandbox", which protects the user from misbehaving or malicious code; publishers could apply for a certificate that they could use to digitally sign applets as "safe", giving them permission to break out of the sandbox and access the local filesystem and network, presumably under user control.
Automatic garbage collection
One major problem with languages such as C++ is the need for manual memory management. In C++, memory must be allocated by the programmer to create an object, then deallocated to delete the object. Often a programmer forgets or is unsure when to deallocate, leading to a memory leak, where a program consumes more and more memory without cleaning up after itself. Even worse, if a region of memory is deallocated twice, the program can become unstable and crash.
In Java, this problem is solved by automatic garbage collection. Objects are created and placed at an address on the heap. The program or other objects can reference an object by holding a reference to its address on the heap. When no references to an object remain, the Java garbage collector autmatically deletes the object, freeing memory and preventing a memory leak. Memory leaks, however, can still occur if a programmer's code holds a reference to an object that is no longer needed—in other words, they still occur but at higher conceptual levels. But on the whole, Java's automatic garbage collection makes creation and deletion of objects in Java much easier and safer than in C++..
Evaluation
Most consider Java technology to deliver reasonably well on all these promises. The language is not, however, without drawbacks.
Java tends to be more high-level than similar languages (such as C++), which means that the Java language lacks features such as hardware-specific data types and low-level pointers to arbitrary memory. Although these features are frequently abused or misused by programmers, and make it harder (though far from impossible) to write a reliable, secure program, they are also powerful tools. However, the Java Native Interface (JNI) does provide a way for a Java program to call non-Java code in order to leverage the capabilities of other languages when necessary.
Some also see a shortcoming in its lack of multiple inheritance, a powerful feature of several object-oriented languages such as C++, Eiffel, and CLOS. Java subclasses have only one chance to inherit implementation by choosing one superclass, which can lead to false dilemmas (should HouseBoat inherit House or Boat?) and rules out certain implementation techniques such as mixins. However, proponents believe Java's ability for classes to implement multiple interfaces is a good trade-off, providing most of the benefits of full multiple inheritance while sidestepping thorny situations in which a class inherits multiple conflicting implementations.
Java's paradigm of bytecode interpretation is widely seen to provide portability only at the expense of performance degradation relative to native code. Dynamic compilation helps tremendously, but still typically cannot beat statically-compiled code. This is because when compilation occurs at run-time and thus counts against the performance of the program, the compiler must use its powerful, expensive optimizations very sparingly. However, precisely because dynamic compilers are present at run-time, they can take advantage of dynamic program behaviour, and can even recompile portions of the program as conditions change. Therefore, dynamic compilers can perform optimizations that static compilers cannot; and there are some programs for which dynamically compiled code is faster than statically compiled. (For instance, long-running programs whose behaviour depends on input data may fall into this category.) It is not unreasonable to suppose that this set of programs will grow as dynamic-compilation technology improves.
Java's heavy use of heap-allocated objects causes programs to consume more memory than similar programs written in lower-level languages, where data storage can be optimized at a fine granularity. Because it treats almost everything as an object, Java encourages a style in which objects contain many references to other objects rather than raw data, making the objects larger and the data less localized. Both of these effects tend to work against modern memory hierarchies, whose caching schemes thrive on small working sets and good locality.
There are some who believe that, for certain projects, object orientation makes work harder instead of easier. This particular complaint is not unique to the Java language but applies to other object-oriented languages as well.
Language
An example of a hello world program in the Java language follows:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } }
Characters
Characters use the 16-bit Unicode encoding (namely UTF-16). It contains all of the usual characters, but also includes character sets for many languages other than English, including Greek, Cyrillic, Chinese, Arabic, etc. Java programs can use all of these characters, although most editors do not have built-in support for character sets other than the usual ASCII characters. Arrays and strings are not primitive types: they are objects.
Interfaces and Classes
One thing that Java lets one do is create an interface that classes will then implement. For example, you can create an interface like this:
public interface Deleteable { public void delete (); }
This interface says that anything that is Deleteable will have the exact method delete()
. There are many uses to this concept because you can have a class like the following:
public class Fred implements Deleteable { //Must include the delete () method }
Then, in another class, something like this can be said.
public void deleteAll (Deleteable[] list) { for (int i = 0; i < list.length; i++) list[i].delete(); }
The feature is a result of compromise. The designers of Java decided not to support multi-inheritance but interface
can be used to simulate multi-inheritance in some occasions.
Interfaces in Java work differently to the concept of the interface in other object-oriented programming languages - they behave much more like the concept of the Objective-C protocol.
Input/Output
Versions of Java prior to 1.4 only supported stream-based blocking IO. This required a thread per stream being handled, as no other processing could take place while the active thread blocked waiting for input or output. This was a major scalability and performance issue for anyone needing to implement any Java network service. Since the introduction of NIO (New IO) in Java 1.4, this scalability problem has been rectified by the introduction of a non-blocking IO framework (though there are a number of open issues in the NIO API as implemented by Sun).
The non-blocking IO framework, though considerably more complex than the original blocking IO framework, allows any number of "channels" to be handled by a single thread. The framework is based on the Reactor Pattern.
The following code snippet shows how user input is accepted from a keyboard and displayed back using stream-based blocking IO. Note that the main thread will block while waiting for input from standard in. If one wanted, for instance, to display a blinking cursor, another thread would have to be created to create the blinking effect.
public static void main(String[] args) throws java.io.IOException { char a; System.out.println("Welcome user, please enter a letter"); a = (char) System.in.read(); System.out.println("Your letter was " + a); }
Early history
The Java platform and language began as an internal project at Sun Microsystems in the December 1990 timeframe. Patrick Naughton, an engineer at Sun, had become increasingly frustrated with the state of Sun's C++ and C APIs and tools. While considering moving to NeXT, Patrick was offered a chance to work on new technology and thus the Stealth Project was started.
The Stealth Project was soon renamed to the Green Project with James Gosling and Mike Sheridan joining Patrick Naughton. They, together with some other engineers, began work in a small office on Sand Hill Road in Menlo Park, California to develop a new technology. The team originally considered C++ as the language to use, but many of them as well as Bill Joy found C++ and the available APIs problematic for several reasons.
Their platform was an embedded platform and had limited resources. Many members found that C++ was too complicated and developers often misused it. They found C++'s lack of garbage collection to also be a problem. Security, distributed programming, and threading support was also required. Finally, they wanted a platform that could be easily ported to all types of devices.
According to the available accounts, Bill Joy had ideas of a new language combining the best of MESA and C. He proposed, in a paper called Further, to Sun that its engineers should produce an object-oriented environment based on C++. James Gosling's frustrations with C++ began while working on Imagination, an SGML editor. Initially, James attempted to modify and extend C++, which he referred to as C++ ++ --, but soon abandoned that in favor of creating an entirely new language, called Oak named after the oak tree that stood just outside his office.
Like many stealth projects working on new technology, the team worked long hours and by the summer of 1992, they were able to demo portions of the new platform including the Green OS, Oak the language, the libraries, and the hardware. Their first attempt focused on building a PDA-like device having a highly graphical interface and a smart agent called Duke to assist the user.
The device was named Star7 after a telephone feature activated by *7 on a telephone keypad. The feature enabled users to answer the telephone anywhere. The PDA device itself was demonstrated on September 3, 1992.
In November of that year, the Green Project was spun off to become a wholly owned subsidiary of Sun Microsystems: FirstPerson, Inc. The team relocated to Palo Alto. The FirstPerson team was interested in building highly interactive devices and when Time Warner issued an RFP for a set-top box, FirstPerson changed their target and responded with a proposal for a set-top box platform. However, the cable industry felt that their platform gave too much control to the user and FirstPerson lost their bid to SGI. An additional deal with 3DO for a set-top box also failed to materialize. FirstPerson was unable to generate any interest within the cable TV industry for their platform. Following their failures, the company, FirstPerson, was rolled back into Sun.
Java meets the Internet
In June and July of 1994, after a 3 day brain storm session with John Gage, James Gosling, Bill Joy, Patrick Naughton, Wayne Rosing, and Eric Schmidt, the team re-targeted yet again its efforts, this time to use the technology for the Internet. They felt that with the advent of the Mosaic web browser, the Internet was on its way to evolving into the same highly interactive vision that they had had for the cable TV network. Patrick Naughton wrote a small web browser, WebRunner, as a prototype. WebRunner would later be renamed HotJava.
It was also in 1994, that Oak was renamed Java. An IP (intellectual property) search revealed that Oak had already been trademarked for another language so the team searched for a new name. The name Java was coined at a local coffee shop frequented by some of the members. It is not clear whether the name is an acronym or not. Most likely, it is not, however some accounts claim that it stands for the names of James Gosling, Arthur Van Hoff, and Andy Bechtolsheim. Others, that it is an acronym for Just Another Vague Acronym.
In October of 1994, HotJava and the Java platform was demoed for Sun executives. Java 1.0a was made available for download in 1994, but the first public release of Java and the HotJava web browser came on May 23, 1995, at the SunWorld conference. The announcement was made by John Gage, the Director of Science for Sun Microsystems. His announcement was accompanied by a surprise announcement by Marc Andreessen, Executive Vice President of Netscape, that Netscape would be including Java support in its browsers.
In January of 1996, the JavaSoft business group was formed by Sun Microsystems to develop the technology.
APIs
Sun has defined 3 platforms targeting different application environments and segmented many of its APIs so that they belong to one of the platforms. The platforms are:
- Java 2 Platform, Micro Edition - targeting environments with limited resources
- Java 2 Platform, Standard Edition - targeting workstation environments
- Java 2 Platform, Enterprise Edition - targeting large distributed enterprise or internet environments.
The classes in the Java APIs are organized into separate groups called packages. Each package contains a set of related interfaces, classes and exceptions. Refer to the separate platforms for a description of the packages available.
The set of APIs is controlled by Sun Microsystems in cooperation with others through its Java Community Process program. Companies or individuals participating in this process can influence the design and development of the APIs, but Sun retains ownership and control of the APIs. This process has been a subject of controversy.
Version history
The Java language has undergone several changes since JDK 1.0 as well as numerous additions of packages to the standard library:
- 1.0 (1996) - this initial release included the Java runtime (the virtual machine and the class libraries), and the development tools (such as the
javac
compiler). Later, Sun also provided a runtime-only package, called the Java Runtime Environment (JRE). - 1.1 (1997) - this added the concept of inner classes, which allow classes to be nested within other classes. This feature is particularly useful for writing brief, one-time implementations of interfaces, such as those used in event handling.
- 1.2 (1998) - the version marked the significant improvement in the language as well as the bundled library. To emphasize the radical change, with this version, Sun officially rechristened the Java platform Java 2.
- 1.3 (2000) - only minor changes and fixes were made.
- 1.4 (2002) - this is the most widely used version as of 2004. The release added the assert keyword, which helps to find errors that might otherwise be difficult to notice or track down.
- 5 - The 1.5 release was renamed to Java 2 version 5. This is the most recent release. It includes a number of new language features, some of which could be argued to have been modeled on Microsoft's C#, which was self-consciously modeled on earlier versions of Java:
- Generics - Provides compile-time type safety for collections and eliminates the need for most typecasts.
- Autoboxing/unboxing - Automatic conversions between primitive types (such as int) and wrapper types (such as Integer).
- Metadata - allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities
- Enumerations - the enum keyword creates a typesafe, ordered list of values (such as Day.monday, Day.tuesday, etc.). Prior to Java 1.5, non-typesafe integer constants were used for this purpose.
- Enhanced for loop - the for loop in Java 1.5 can easily iterate over each member of an array or collection, using a construct of the form:
for (Widget w : box) { System.out.println(w); }
.
- This example iterates over each item in box, assigning it in turn to the variable w, which is then printed to
System.out
.
- This example iterates over each item in box, assigning it in turn to the variable w, which is then printed to
In addition to the language changes, much more dramatic changes have been made to the Java class library over the years, which has grown from a few hundred classes in Java 1.0 to over three thousand in Java 5. Entire new APIs, such as Swing and Java2D, have been introduced, and many of the original 1.0 classes and methods have been deprecated.
Extensions and architectures closely tied to the Java programming language include:
- J2EE (Enterprise edition)
- J2ME (Micro-Edition for PDAs and cellular phones)
- JMF
- JNDI
- JSML
- JDBC
- JAIN
- JDMK
- Jini
- Jiro
- JXTA
- JavaSpaces
- JMI
- JMX
- JSP
- JSF
Interpreted version
There is an interpreted version of Java called beanshell which may be used as a shell scripting language. The interpreter may be embedded in a Java application to make it scriptable.
Related free software
- GCJ, a Java compiler that comes as part of GCC, the GNU Compiler Collection
- GNU Classpath, GNU's replacement for Sun's proprietary class libraries
- The Jakarta Project produces free software in Java, especially tools for building web applications
- Kaffe, a clean room implementation of the Java virtual machine with associated class libraries
- SableVM, (LGPLed) a Java Virtual Machine meant to be robust, efficient and portable
- Eclipse, (CPLed) a free Java IDE for developing in Java and other programming languages
- NetBeans, a free Java-based IDE for developing in Java and other programming languages
- SuperWaba, (LGPLed) A Java-like virtual machine, with a minimalistic design suitable for PDA programming
- JUnit, a widely used framework for creating automated unit tests
- JSwat, a standalone, graphical Java debugger
- JCreator, a Java Integrated development environment
See also
- Java API
- Java virtual machine
- Comparison of Java to C++
- Eclipse (Open source IDE)
- NetBeans (Another open source IDE)
- Optimization of Java
- Java Platform Debugger Architecture
- List of Java-programs
- Java User Group
- Artefaktur
- Java XML
- Java Servlet
- Java 2 Platform, Standard Edition (J2SE)
- Javapedia
- Java community
- JavaOS
External links
- Official Java Home Site
- Original Java whitepaper
- The Java Language Specification, 2nd edition Complete description of the entirity of the Java language
- beanshell Interpreted version
- Java newsgroup (Google's web interface)
- The above newsgroup's FAQ
- A pure java desktop
- Javapedia project
- Sun's tutorial on Java Programming
- Documentation for the latest Java 2 Standard Edition API, v1.4.2
- Vulnerabilities in Java Environments
- Java(TM) Technology: The Early Years
- A Brief History of the Green Project
- The Java Saga
- Java Basics Manual
- Thinking in Java, by Bruce Eckel
- Java: Cornerstone of the Global Network Enterprise
- A history of Java
- SuperWaba, a Java-like virtual machine
- Java: Slow, ugly and irrelevant (Salon)
- java sucks, by Jamie Zawinski
- How to Think Like a Computer Scientist Java version
- The Java Trap, by Richard Stallman
- The Dark Side of Java by Conrad Weisert
- The Java Wiki
- A Java glossary