Java (programming language)
This article is about the programming language.
For the software platform, see Java (software platform).
For the software package downloaded from java.com, see Java Platform, Standard Edition.
For other uses, see Java (disambiguation).
"Java language" redirects here.
It is not to be confused with Javanese language.
|Paradigm||Multi-paradigm: generic, object-oriented (class-based), imperative, reflective|
|Designed by||James Gosling|
|Developer||Oracle Corporation et al.|
|First appeared||May 23, 1995; 25 years ago (1995-05-23)|
|Stable release||Java SE 15
/ September 15, 2020; 2 months ago (2020-09-15)
|Typing discipline||Static, strong, safe, nominative, manifest|
It is a general-purpose programming language intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
The Java runtime provides dynamic capabilities (such as reflection and runtime code modification) that are typically not available in traditional compiled languages.
Oracle offers its own HotSpot Java Virtual Machine, however the official reference implementation is the OpenJDK JVM which is free open source software and used by most developers including the Eclipse IDE and is the default JVM for almost all Linux distributions.
The latest version is Java 15, released in September 2020, with Java 11, a currently supported long-term support (LTS) version, released on September 25, 2018; Oracle released for the legacy Java 8 LTS the last zero-cost public update in January 2019 for commercial use, although it will otherwise still support Java 8 with public updates for personal use indefinitely.
Other vendors have begun to offer zero-cost builds of OpenJDK 8 and 11 that are still receiving security and other upgrades.
Oracle (and others) highly recommend uninstalling older versions of Java because of serious risks due to unresolved security issues.
Since Java 9, 10, 12 and 13 are no longer supported, Oracle advises its users to immediately transition to the latest version (currently Java 15) or an LTS release.
See also: Java (software platform) § History
Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time.
Sun Microsystems released the first public implementation as Java 1.0 in 1996.
It promised Write Once, Run Anywhere (WORA) functionality, providing no-cost run-times on popular platforms.
Fairly secure and featuring configurable security, it allowed network- and file-access restrictions.
With the advent of Java 2 (released initially as J2SE 1.2 in December 1998 – 1999), new versions had multiple configurations built for different types of platforms.
J2EE included technologies and APIs for enterprise applications typically run in server environments, while J2ME featured APIs optimized for mobile applications.
The desktop version was renamed J2SE.
At one time, Sun made most of its Java implementations available without charge, despite their proprietary software status.
Sun generated revenue from Java through the selling of licenses for specialized products such as the Java Enterprise System.
On May 8, 2007, Sun finished the process, making all of its JVM's core code available under free software/open-source distribution terms, aside from a small portion of code to which Sun did not hold the copyright.
Sun's vice-president Rich Green said that Sun's ideal role with regard to Java was as an evangelist.
Following Oracle Corporation's acquisition of Sun Microsystems in 2009–10, Oracle has described itself as the steward of Java technology with a relentless commitment to fostering a community of participation and transparency.
On April 2, 2010, James Gosling resigned from Oracle.
In January 2016, Oracle announced that Java run-time environments based on JDK 9 will discontinue the browser plugin.
There were five primary goals in the creation of the Java language:
As of September 2020, Java 8 and 11 are supported as Long Term Support (LTS) versions, and one later non-LTS version is supported.
Major release versions of Java, along with their release dates:
|JDK1.0||January 23, 1996|
|JDK 1.1||February 19, 1997|
|J2SE 1.2||December 8, 1998|
|J2SE 1.3||May 8, 2000|
|J2SE 1.4||February 6, 2002|
|J2SE 5.0||September 30, 2004|
|Java SE 6||December 11, 2006|
|Java SE 7||July 28, 2011|
|Java SE 8||March 18, 2014|
|Java SE 9||September 21, 2017|
|Java SE 10||March 20, 2018|
|Java SE 11||September 25, 2018|
|Java SE 12||March 19, 2019|
|Java SE 13||September 17, 2019|
|Java SE 14||March 17, 2020|
|Java SE 15||September 15, 2020|
Sun has defined and supports four editions of Java targeting different application environments and segmented many of its APIs so that they belong to one of the platforms.
The platforms are:
- Java Card for smart-cards.
- Java Platform, Micro Edition (Java ME) – targeting environments with limited resources.
- Java Platform, Standard Edition (Java SE) – targeting workstation environments.
- Java Platform, Enterprise Edition (Java EE) – targeting large distributed enterprise or Internet environments.
Sun also provided an edition called Personal Java that has been superseded by later, standards-based Java ME configuration-profile pairings.
Java JVM and bytecode
One design goal of Java is portability, which means that programs written for the Java platform must run similarly on any combination of hardware and operating system with adequate run time support.
Java bytecode instructions are analogous to machine code, but they are intended to be executed by a virtual machine (VM) written specifically for the host hardware.
The use of universal bytecode makes porting simple.
Just-in-time (JIT) compilers that compile byte-codes to machine code during runtime were introduced from an early stage.
Java itself is platform-independent and is adapted to the particular platform it is to run on by a Java virtual machine (JVM) for it, which translates the Java bytecode into the platform's machine language.
Main article: Java performance
Programs written in Java have a reputation for being slower and requiring more memory than those written in C++ .
However, Java programs' execution speed improved significantly with the introduction of just-in-time compilation in 1997/1998 for Java 1.1, the addition of language features supporting better code analysis (such as inner classes, the StringBuilder class, optional assertions, etc.), and optimizations in the Java virtual machine, such as HotSpot becoming the default for Sun's JVM in 2000.
With Java 1.5, the performance was improved with the addition of the java.util.concurrent package, including lock free implementations of the ConcurrentMaps and other multi-core collections, and it was improved further with Java 1.6.
Some platforms offer direct hardware support for Java; there are micro controllers that can run Java bytecode in hardware instead of a software Java virtual machine, and some ARM-based processors could have hardware support for executing Java bytecode through their Jazelle option, though support has mostly been dropped in current implementations of ARM.
Automatic memory management
The programmer determines when objects are created, and the Java runtime is responsible for recovering the memory once objects are no longer in use.
Once no references to an object remain, the unreachable memory becomes eligible to be freed automatically by the garbage collector.
Something similar to a memory leak may still occur if a programmer's code holds a reference to an object that is no longer needed, typically when objects that are no longer needed are stored in containers that are still in use.
If methods for a non-existent object are called, a null pointer exception is thrown.
One of the ideas behind Java's automatic memory management model is that programmers can be spared the burden of having to perform manual memory management.
In the latter case, the responsibility of managing memory resides with the programmer.
If the program does not deallocate an object, a memory leak occurs.
If the program attempts to access or deallocate memory that has already been deallocated, the result is undefined and difficult to predict, and the program is likely to become unstable or crash.
This can be partially remedied by the use of smart pointers, but these add overhead and complexity.
Note that garbage collection does not prevent logical memory leaks, i.e. those where the memory is still referenced but never used.
Garbage collection may happen at any time.
Ideally, it will occur when a program is idle.
It is guaranteed to be triggered if there is insufficient free memory on the heap to allocate a new object; this can cause a program to stall momentarily.
Explicit memory management is not possible in Java.
Java does not support C/C++ style pointer arithmetic, where object addresses can be arithmetically manipulated (e.g. by adding or subtracting an offset).
This allows the garbage collector to relocate referenced objects and ensures type safety and security.
As in C++ and some other object-oriented languages, variables of Java's primitive data types are either stored directly in fields (for objects) or on the stack (for methods) rather than on the heap, as is commonly true for non-primitive data types (but see escape analysis).
This was a conscious decision by Java's designers for performance reasons.
Java contains multiple types of garbage collectors.
By default, HotSpot uses the parallel scavenge garbage collector.
However, there are also several other garbage collectors that can be used to manage the heap.
For 90% of applications in Java, the Concurrent Mark-Sweep (CMS) garbage collector is sufficient.
Oracle aims to replace CMS with the Garbage-First Collector (G1).
Having solved the memory management problem does not relieve the programmer of the burden of handling properly other kinds of resources, like network or database connections, file handles, etc., especially in the presence of exceptions.
Paradoxically, the presence of a garbage collector has faded the necessity of having an explicit destructor method in the classes, thus rendering the management of these other resources more difficult.
Main article: Java syntax
Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language.
All code is written inside classes, and every data item is an object, with the exception of the primitive data types, (i.e. integers, floating-point numbers, boolean values, and characters), which are not objects for performance reasons.
Java reuses some popular aspects of C++ (such as the printf method).
Java uses comments similar to those of C++.
There are three different styles of comments: a single line style marked with two slashes (//), a multiple line style opened with /* and closed with */, and the Javadoc commenting style opened with /** and closed with */.
The Javadoc style of commenting allows the user to run the Javadoc executable to create documentation for the program and can be read by some integrated development environments (IDEs) such as Eclipse to allow developers to access documentation within the IDE.
Hello world example
The traditional Hello world program can be written in Java as:
All source files must be named after the public class they contain, appending the suffix .java, for example, HelloWorldApp.java.
It must first be compiled into bytecode, using a Java compiler, producing a file with the .class suffix (HelloWorldApp.class, in this case).
Only then can it be executed or launched.
The Java source file may only contain one public class, but it can contain multiple classes with a non-public access modifier and any number of public inner classes.
When the source file contains multiple classes, it is necessary to make one class (introduced by the class keyword) public (preceded by the public keyword) and name the source file with that public class name.
A class that is not declared public may be stored in any .java file.
The compiler will generate a class file for each class defined in the source file.
The name of the class file is the name of the class, with .class appended.
For class file generation, anonymous classes are treated as if their name were the concatenation of the name of their enclosing class, a $, and an integer.
The keyword public denotes that a method can be called from code in other classes, or that a class may be used by classes outside the class hierarchy.
The class hierarchy is related to the name of the directory in which the .java file is located.
This is called an access level modifier.
Other access level modifiers include the keywords private (a method that can only be accessed in the same class) and protected (which allows code from the same package to access).
If a piece of code attempts to access private methods or protected methods, the JVM will throw a SecurityException
The keyword static in front of a method indicates a static method, which is associated only with the class and not with any specific instance of that class.
Only static methods can be invoked without a reference to an object.
Static methods cannot access any class members that are not also static.
Methods that are not designated static are instance methods and require a specific instance of a class to operate.
The keyword void indicates that the main method does not return any value to the caller.
If a Java program is to exit with an error code, it must call System.exit() explicitly.
The method name main is not a keyword in the Java language.
It is simply the name of the method the Java launcher calls to pass control to the program.
Java classes that run in managed environments such as applets and Enterprise JavaBeans do not use or need a main() method.
A Java program may contain multiple classes that have main methods, which means that the VM needs to be explicitly told which class to launch from.
The main method must accept an array of objects.
By convention, it is referenced as args although any other legal identifier name can be used.
Since Java 5, the main method can also use variable arguments, in the form of public static void main(String... args), allowing the main method to be invoked with an arbitrary number of String arguments.
The effect of this alternate declaration is semantically identical (to the args parameter which is still an array of String objects), but it allows an alternative syntax for creating and passing the array.
The Java launcher launches Java by loading a given class (specified on the command line or as an attribute in a ) and starting its public static void main(String) method.
Stand-alone programs must declare this method explicitly.
The String args parameter is an array of objects containing any arguments passed to the class.
The parameters to main are often passed by means of a command line.
Printing is part of a Java standard library: The class defines a public static field called .
The out object is an instance of the class and provides many methods for printing data to standard out, including which also appends a new line to the passed string.
The string "Hello World!"
is automatically converted to a String object by the compiler.
Example with methods
Main article: Criticism of Java
Criticisms directed at Java include the implementation of generics, speed, the handling of unsigned numbers, the implementation of floating-point arithmetic, and a history of security vulnerabilities in the primary Java VM implementation HotSpot.
Main article: Java Class Library
Companies or individuals participating in this process can influence the design and development of the APIs.
This process has been a subject of controversy during the 2010s.
The class library contains features such as:
- The core libraries, which include:
- (NOTE: new since Java 11)
- Functional programming (Lambda, Streaming)
- Collection libraries that implement data structures such as lists, dictionaries, trees, sets, queues and double-ended queue, or stacks
- XML Processing (Parsing, Transforming, Validating) libraries
- Internationalization and localization libraries
- The integration libraries, which allow the application writer to communicate with external systems. These libraries include:
- The Java Database Connectivity (JDBC) API for database access
- Java Naming and Directory Interface (JNDI) for lookup and discovery
- RMI and CORBA for distributed application development
- JMX for managing and monitoring applications
- User interface libraries, which include:
- The (heavyweight, or native) Abstract Window Toolkit (AWT), which provides GUI components, the means for laying out those components and the means for handling events from those components
- The (lightweight) Swing libraries, which are built on AWT but provide (non-native) implementations of the AWT widgetry
- APIs for audio capture, processing, and playback
- A platform dependent implementation of the Java virtual machine that is the means by which the bytecodes of the Java libraries and third party applications are executed
- Plugins, which enable applets to be run in web browsers
- Java Web Start, which allows Java applications to be efficiently distributed to end users across the Internet
- Licensing and documentation
Main article: Javadoc
Javadoc is a comprehensive documentation system, created by Sun Microsystems.
It provides developers with an organized system for documenting their code.
Javadoc comments have an extra asterisk at the beginning, i.e. the delimiters are /** and */, whereas the normal multi-line comments in Java are set off with the delimiters /* and */, and single-line comments start off the line with //.
See also: Free Java implementations
This implementation is based on the original implementation of Java by Sun.
The Oracle implementation is packaged into two different distributions: The Java Runtime Environment (JRE) which contains the parts of the Java SE platform required to run Java programs and is intended for end users, and the Java Development Kit (JDK), which is intended for software developers and includes development tools such as the Java compiler, Javadoc, , and a debugger.
Oracle has also released GraalVM, a high performance Java dynamic compiler and interpreter.
OpenJDK is another notable Java SE implementation that is licensed under the GNU GPL.
The implementation started when Sun began releasing the Java source code under the GPL.
As of Java SE 7, OpenJDK is the official Java reference implementation.
The goal of Java is to make all implementations of Java compatible.
Historically, Sun's trademark license for usage of the Java brand insists that all implementations be compatible.
Sun sued in 1997, and, in 2001, won a settlement of US$20 million, as well as a court order enforcing the terms of the license from Sun.
As a result, Microsoft no longer ships Java with Windows.
Platform-independent Java is essential to Java EE, and an even more rigorous validation is required to certify an implementation.
This environment enables portable server-side applications.
Use outside the Java platform
The Java programming language requires the presence of a software platform in order for compiled programs to be executed.
Oracle supplies the Java platform for use with Java.
Although Android, built on the Linux kernel, is written largely in C, the Android SDK uses the Java language as the basis for Android applications but does not use any of its standard GUI, SE, ME or other established Java standards.
Android does not provide the full Java SE standard library, although the Android SDK does include an independent implementation of a large subset of it.
It supports Java 6 and some Java 7 features, offering an implementation compatible with the standard library (Apache Harmony).
See also: Oracle America, Inc. v. Google, Inc.
The use of Java-related technology in Android led to a legal dispute between Oracle and Google.
On May 7, 2012, a San Francisco jury found that if APIs could be copyrighted, then Google had infringed Oracle's copyrights by the use of Java in Android devices.
District Judge William Haskell Alsup ruled on May 31, 2012, that APIs cannot be copyrighted, but this was reversed by the United States Court of Appeals for the Federal Circuit in May 2014.
On May 26, 2016, the district court decided in favor of Google, ruling the copyright infringement of the Java API in Android constitutes fair use.
In March 2018, this ruling was overturned by the Appeals Court, which sent down the case of determining the damages to federal court in San Francisco.
Google filed a petition for writ of certiorari with the Supreme Court of the United States in January 2019 to challenge the two rulings that were made by the Appeals Court in Oracle's favor.
- Dalvik, used in old Android versions, replaced by non-JIT Android Runtime
- Deterministic Parallel Java
- List of Java virtual machines
- List of Java APIs
- List of JVM languages
Comparison of Java with other languages
Credits to the contents of this page go to the authors of the corresponding Wikipedia page: en.wikipedia.org/wiki/Java (programming language).