Sunday, May 6, 2007

Java (programming language)



Java is an object-oriented applications programming language developed by Sun Microsystems in the early 1990s. Java applications are typically compiled to bytecode, although compilation to native machine code is also possible. At runtime, bytecode is usually either interpreted or compiled to native code for execution, although direct hardware execution of bytecode by a Java processor is also possible.
The language itself derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. JavaScript, a scripting language, shares a similar name and has similar syntax, but is not directly related to Java.
Sun Microsystems provides a GNU General Public License implementation of a Java compiler and Java virtual machine, in compliance with the specifications of the Java Community Process, although the class library that is required to run Java programs is not free software.



History

Java started as a project called "Oak" (The name came from an oak tree that stood outside the Sun Microsystems office) by James Gosling in June 1991 for use in a set top box project.[3] Gosling's goals were to implement a virtual machine and a language that had a familiar C/C++ style of notation[4]. The first public implementation was Java 1.0 in 1995. It promised "Write Once, Run Anywhere" (WORA), providing no-cost runtimes on popular platforms. It was fairly secure and its security was configurable, allowing network and file access to be restricted. Major web browsers soon incorporated the ability to run secure Java "applets" within web pages. Java became popular quickly. With the advent of "Java 2", new versions had multiple configurations built for different types of platforms. For example, J2EE was for enterprise applications and the greatly stripped down version J2ME was for mobile applications. J2SE was the designation for the Standard Edition. In 2006, new "J2" versions were renamed Java EE, Java ME, and Java SE, respectively.
In 1997, Sun approached the ISO/IEC JTC1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process.[5][6][7] Java remains a proprietary de facto standard that is controlled through the Java Community Process.[8] Sun makes most of its Java implementations available without charge, with revenue being generated by specialized products such as the Java Enterprise System. Sun distinguishes between its Software Development Kit (SDK) and Runtime Environment (JRE) which is a subset of the SDK, the primary distinction being that in the JRE the compiler is not present.
On November 13, 2006, Sun released parts of Java as free/open source software, under the GNU General Public License (GPL). The release of the complete source code under GPL is expected in the first half of 2007.
Releases
Main article: Java version history
The Java project has seen many release versions. Since 1997 they are:
JDK 1.1.4 (Sparkler) September 12, 1997
JDK 1.1.5 (Pumpkin) December 3, 1997
JDK 1.1.6 (Abigail) April 24, 1998
JDK 1.1.7 (Brutus) September 28, 1998
JDK 1.1.8 (Chelsea) April 8, 1999
J2SE 1.2 (Playground) December 4, 1998
J2SE 1.2.1 (none) March 30, 1999
J2SE 1.2.2 (Cricket) July 8, 1999
J2SE 1.3 (Kestrel) May 8, 2000
J2SE 1.3.1 (Ladybird) May 17, 2001
J2SE 1.4.0 (Merlin) February 13, 2002
J2SE 1.4.1 (Hopper) September 16, 2002
J2SE 1.4.2 (Mantis) June 26, 2003
J2SE 5.0 (1.5.0) (Tiger) September 29, 2004
Java SE 6 (1.6.0) (Mustang) December 11, 2006
Java SE 7 (1.7.0) (Dolphin) anticipated for 2008
Primary goals
There were five primary goals in the creation of the Java language:
It should use the object-oriented programming methodology.
It should allow the same program to be executed on multiple operating systems.
It should contain built-in support for using computer networks.
It should be designed to execute code from remote sources securely.
It should be easy to use by selecting what was considered the good parts of other object-oriented languages.
To achieve the goals of networking support and remote code execution, Java programmers sometimes find it necessary to use extensions such as CORBA, Internet Communications Engine, or OSGi.
Platform independence
One characteristic, platform independence, means that programs written in the Java language must run similarly on any supported hardware/operating-system platform. One should be able to write a program once, compile it once, and run it anywhere.
This is achieved by most Java compilers by compiling the Java language code "halfway" to bytecode (specifically Java 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 interprets and executes generic Java bytecode. (In some JVM versions, bytecode can also be compiled to native code, resulting in faster execution.) Further, standardized libraries are provided to allow access to features of the host machines (such as graphics, threading and networking) in unified ways. Note that, although there is an explicit compiling stage, at some point, the Java bytecode is interpreted or converted to native machine instructions by the JIT compiler.
There are also implementations of Java compilers that translate the Java language code 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". This resulted in a legal dispute with Microsoft after Sun claimed that the Microsoft implementation did not support the RMI and JNI interfaces and had added platform-specific features of their own. Sun sued and won both damages (some $20 million) and a court order enforcing the terms of the license from Sun. As a result, Microsoft no longer ships Java with Windows, and in recent versions of Windows, Internet Explorer cannot support Java applets without a third-party plugin. However, Sun and others have made available Java run-time systems at no cost for those and other versions of Windows.
The first implementations of the language used an interpreted virtual machine to achieve portability. These implementations produced programs that ran more slowly than programs compiled to native executables, for instance written in C or C++, so the language suffered a reputation for poor performance. More recent JVM implementations produce programs that run significantly faster than before, using multiple techniques.
The first technique is to simply compile directly into native code like a more traditional compiler, skipping bytecodes entirely. This achieves good performance, but at the expense of portability. Another technique, known as just-in-time compilation (JIT), translates the Java bytecodes into native code at the time that the program is run which results in a program that executes faster than interpreted code but also incurs compilation overhead during execution. More sophisticated VMs 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. Dynamic recompilation can achieve optimizations superior to static compilation because the dynamic compiler can base optimizations on knowledge about the runtime environment and the set of loaded classes, and can identify the "hot spots" (parts of the program, often inner loops, that take up most of execution time). JIT compilation and dynamic recompilation allow Java programs 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 has been mixed. 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, and Enterprise JavaBeans, as well as with Embedded systems based on OSGi, using Embedded Java environments.
Automatic garbage collection
One of the ideas behind Java's automatic memory management model is that programmers be spared the burden of having to perform manual memory management. In some languages the programmer allocates memory for the creation of objects stored on the heap and the responsibility of later deallocating that memory thus resides with the programmer. If the programmer forgets to deallocate memory or writes code that fails to do so, a memory leak occurs and the program can consume an arbitrarily large amount of memory. Additionally, if the program attempts to deallocate the region of memory more than once, the result is undefined and the program may become unstable and may crash. Finally, in non garbage collected environments, there is a certain degree of overhead and complexity of user-code to track and finalize allocations. Often developers may box themselves into certain designs to provide reasonable assurances that memory leaks will not occur [5].
In Java, this potential problem is avoided by automatic garbage collection. The programmer determines when objects are created, and the Java runtime is responsible for managing the object's lifecycle. The program or other objects can reference an object by holding a reference to it (which, from a low-level point of view, is its address on the heap). When no references to an object remain, the Java garbage collector automatically deletes the unreachable object, freeing memory and preventing a memory leak. Memory leaks may still occur if a programmer's code holds a reference to an object that is no longer needed—in other words, they can still occur but at higher conceptual levels.
The use of garbage collection in a language can also affect programming paradigms. If, for example, the developer assumes that the cost of memory allocation/recollection is low, they may choose to more freely construct objects instead of pre-initializing, holding and reusing them. With the small cost of potential performance penalties (inner-loop construction of large/complex objects), this facilitates thread-isolation (no need to synchronize as different threads work on different object instances) and data-hiding. The use of transient immutable value-objects minimizes side-effect programming.
Comparing Java and C++, it is possible in C++ to implement similar functionality (for example, a memory management model for specific classes can be designed in C++ to improve speed and lower memory fragmentation considerably), with the possible cost of adding comparable runtime overhead to that of Java's garbage collector, and of added development time and application complexity if one favors manual implementation over using an existing third-party library. In Java, garbage collection is built-in and virtually invisible to the developer. That is, developers may have no notion of when garbage collection will take place as it may not necessarily correlate with any actions being explicitly performed by the code they write. Depending on intended application, this can be beneficial or disadvantageous: the programmer is freed from performing low-level tasks, but at the same time loses the option of writing lower level code.
Java does not support pointer arithmetic as is supported in for example C++. This is because the garbage collector may relocate referenced objects, invalidating such pointers. Another reason that Java forbids this is that type safety and security can no longer be guaranteed if arbitrary manipulation of pointers is allowed.
Syntax
Main article: Java syntax
The syntax of Java is largely derived from C++. However, unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built exclusively as an object oriented language. As a result, almost everything is an object and all code is written inside a class. The exceptions are the intrinsic data types (ordinal and real numbers, boolean values, and characters), which are not classes for performance reasons.
An example that better demonstrates object-oriented programming:// OddEven.java
import javax.swing.JOptionPane;
public class OddEven {
private int input;
public OddEven() {
input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number"));
}

public void calculate() {
if (input % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
public static void main(String[] args) {
OddEven number = new OddEven();
number.calculate();
}
}
Extensions and related architectures
The extensions to standard Java are typically in javax.* packages. They are not included in the JDK or JRE. Extensions and architectures closely tied to the Java programming language include:
Java EE (previously J2EE) (Java Platform, Enterprise Edition—for distributed enterprise applications)
Java ME (previously J2ME) (Java Platform, Micro Edition—for PDAs and cellular phones)
JMF (Java Media Framework)
JNDI (Java Naming and Directory Interface)
JSML (Java Speech API Markup Language)
JDBC (Java DataBase Connectivity)
JDO (Java Data Objects)
JAI (Java Advanced Imaging)
JAIN (Java API for Integrated Networks)
JDMK (Java Dynamic Management Kit)
Jini (a network architecture for the construction of federated distributed systems)
Jiro
Java Card (Java for smart cards)
JavaSpaces
JML (Java Modeling Language)
JMI (Java Metadata Interface)
JMX (Java Management Extensions)
JSP (JavaServer Pages)
JSF (JavaServer Faces)
JNI (Java Native Interface)
JXTA (Open Protocols for Peer-to-Peer (P2P) Virtual Network)
Java 3D (A high level API for 3D graphics programming)
JOGL (Java OpenGL—A low level API for 3D graphics programming, using OpenGL)
LWJGL (Light Weight Java Game Library—A low level API providing access to OpenGL, OpenAL and various input devices)
OSGi (Dynamic Service Management and Remote Maintenance)
JMonkey Engine (High performance scene graph based 3D engine)