Legality of Cannabis by U.S. Jurisdiction

{{FAC}} should be substituted at the top of the article talk page

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). More on the history of Java can be found in the article about the Java platform, which includes the language, the Java virtual machine, and the Java API. Sun controls the Java specification and 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.

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 compiling Java language code "halfway" to bytecode--simplified machine instructions that conform to a set standard. 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 and networking) in unified ways. The Java language also includes support for multi-threaded programs.

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 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, the just-in-time compiler or "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.


Evaluation

Most generally 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, 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 multiple inheritance of 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 runtime, 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!");
    }
}


Control structures

Loops

while (Boolean expression) {
    statement(s)
}


do {
    statement(s)
} while (Boolean expression);


for (initialisation ; termination condition ; incrementing expr) {
    statement(s)
}


Conditional statements

if (Boolean expression) {
    statement(s)
}


if (Boolean expression) {
    statement(s)
} else {
    statement(s)
}


With else if arbitrarily complex if-then-constructions may be built.

if (Boolean expression) {
    statement(s)
} else if (Boolean expression) {
    statement(s)
} else if (Boolean expression) {
    statement(s)
} else {
    statement(s)
}


switch (integer expression) {
    case constant integer expr:
         statement(s)
         break;
    ...
    default:
         statement(s)
         break;
}


Exception handling

try {
    statement(s)
} catch (exception type) {
    statement(s)
} catch (exception type) {
    statement(s)
} finally {
    statement(s)
}


Unstructured control flow

Java does not have goto statements, as their use is widely considered poor programming practice. There are, however, some uses for goto that are considered acceptable, and Java has facilities to duplicate such functionality. Note that goto is a reserved word and as such cannot be used for identifiers.

Early exit from loops

Java provides two statements to exit from a loop in the midst of an iteration. The statement

continue;

terminates the current iteration of a loop and starts the next one, behaving as a goto jumping to the top of the loop body. Similarly, the statement

break;

exits the loop, terminating the current iteration and executing no more iterations. The effect is that of a goto jumping just past the end of the loop.

Java's break and continue statements are more powerful than the C and C++ constructs of the same name, in that they are capable of exiting more than one loop-nesting level. Specifically, to jump out of a particular loop, one can label the loop, and then affix the label to the break or continue statement. To achieve similar functionality in C and C++ would require the use of a goto statement.

For instance:

outer: while (true) {
    inner: while (true) {
        break;             // Exits the innermost loop
        break inner;       // Also exits the innermost loop
        break outer;       // Exits the outermost loop
    }
}

Early exit from methods

The statement

return;

terminates a method.

With

return aValue;

aValue may be given back to the calling method.

Primitive data types

Variable Type Description
byte 8-bit signed (two's complement) integer
short 16-bit signed (two's complement) integer
int 32-bit signed (two's complement) integer
long 64-bit signed (two's complement) integer
float 32-bit single-precision floating point (IEEE 754 standard)
double 64-bit double-precision floating point (IEEE 754 standard)
char 16-bit single Unicode character
boolean true or false

Characters use the 16-bit Unicode encoding. 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();
}

Input/Output

Following code snippet shows how user input is accepted from a console and displayed back :

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);
}

Versions

Java was initially released as the Java Development Kit 1.0 (JDK 1.0). This included the Java runtime (the virtual machine and the class libraries), and the development tools (e.g. the Javac compiler). Later, Sun also provided a runtime-only package, called the Java Runtime Environment (JRE). With the dramatic changes that occurred with the release of JDK 1.2, Sun officially rechristened the Java platform Java 2. For example, the official name of Java 1.4 is The Java(TM) 2 Platform, Standard Edition version 1.4.

The Java language has undergone several changes since JDK 1.0:

Java 1.1 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.

Java 1.4 added the assert keyword, which helps to find errors that might otherwise be difficult to notice or track down.

Java 1.5, which is scheduled to be released in the second half of 2004, adds a number of new language features:

  • 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.

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 1.5. Entire new APIs, such as Swing (Java) 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:

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.

See also