Page protected with pending changes level 1

Java (programming language)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
"Java language" redirects here. For the natural language from the Indonesian island of Java, see Javanese language.
Not to be confused with JavaScript.
Java logo.png
Designed by James Gosling and
Sun Microsystems
Developer Oracle Corporation
Stable release Java Standard Edition 8 Update 60 (1.8.0_60)[2] / August 18, 2015; 54 days ago (2015-08-18)[2]
Preview release Java Standard Edition 9 Early Access b76 (1.9.0-ea-b76) / July 24, 2015; 2 months ago (2015-07-24)
Typing discipline Static, strong, safe, nominative, manifest
Implementation language C and C++
OS Cross-platform (multi-platform)
License GNU General Public License, Java Community Process
Filename extensions .java , .class, .jar

For Java Developers
Major implementations
OpenJDK, GNU Compiler for Java(gcj), many others
Generic Java, Pizza
Influenced by
Ada 2005, BeanShell, C#, Clojure, ECMAScript, Groovy, J#, JavaScript, Kotlin, PHP, Python, Scala, Seed7, Vala, Hack[11]

Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented,[12] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA),[13] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[14] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. As of 2015, Java is one of the most popular programming languages in use,[15][16][17][18] particularly for client-server web applications, with a reported 9 million developers.[citation needed] Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.


The latest version is Java 8, the only supported version, currently.


Duke, the Java mascot
James Gosling, the creator of Java (2008)
The TIOBE programming language popularity index graph from 2002 to 2015. Over the course of a decade Java (blue) and C (black) competing for the top position.

James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991.[19] Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time.[20] The language was initially called Oak after an oak tree that stood outside Gosling's office. Later the project went by the name Green and was finally renamed Java, from Java coffee.[21] Gosling designed Java with a C/C++-style syntax that system and application programmers would find familiar.[22]


In 1997, Sun Microsystems approached the ISO/IEC JTC 1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process.[24][25][26] Java remains a de facto standard, controlled through the Java Community Process.[27] 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.


Sun's vice-president Rich Green said that Sun's ideal role with regard to Java was as an "evangelist".[29] 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".[30] This did not prevent Oracle from filing a lawsuit against Google shortly after that for using Java inside the Android SDK (see Google section below). Java software runs on everything from laptops to data centers, game consoles to scientific supercomputers.[31] On April 2, 2010, James Gosling resigned from Oracle.[32]


There were five primary goals in the creation of the Java language:[14]

  2. It must be "robust and secure".
  4. It must execute with "high performance".


Main article: Java version history

Currently only Java 8 is supported ("publicly"). Major release versions of Java, along with their release dates:

  • JDK 1.0 (January 21, 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 platform[edit]

Java Control Panel, version 7



The use of universal bytecode makes porting simple. However, the overhead of interpreting bytecode into machine instructions makes interpreted programs almost always run more slowly than native executables. However, just-in-time (JIT) compilers that compile bytecodes 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 for it, which translates the Java bytecode into the platform's machine language.[33]


Oracle Corporation is the current owner of the official implementation of the Java SE platform, following their acquisition of Sun Microsystems on January 27, 2010. This implementation is based on the original implementation of Java by Sun. The Oracle implementation is available for Microsoft Windows (still works for XP, while only later versions currently "publicly" supported), Mac OS X, Linux and Solaris. Because Java lacks any formal standardization recognized by Ecma International, ISO/IEC, ANSI, or other third-party standards organization, the Oracle implementation is the de facto standard.



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". This resulted in a legal dispute with Microsoft after Sun claimed that the Microsoft implementation did not support RMI or JNI and had added platform-specific features of their own. 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.[34] As a result, Microsoft no longer ships Java with Windows.



Main article: Java performance

Programs written in Java have a reputation for being slower and requiring more memory than those written in C++.[35][36] However, Java programs' execution speed improved significantly with the introduction of just-in-time compilation in 1997/1998 for Java 1.1,[37] 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.

Some platforms offer direct hardware support for Java; there are microcontrollers that can run Java in hardware instead of a software Java virtual machine, and ARM based processors can have hardware support for executing Java bytecode through their Jazelle option (while its support is mostly dropped in current implementations of ARM).

Automatic memory management[edit]






Java contains multiple types of garbage collectors. By default,[citation needed] 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 garbage collector is sufficient.[40] Oracle aims to replace CMS with the Garbage-first collector (G1).


Main article: Java syntax

The syntax of Java is largely derived from C++. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language.[14] 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.

Unlike C++, Java does not support operator overloading[41] or multiple inheritance for classes, though multiple inheritance is supported for interfaces.[42] This simplifies the language and aids in preventing potential errors and anti-pattern design.





package fibsandlies;
import java.util.HashMap;

public class FibCalculator extends Fibonacci implements Calculator {

    public static void main(String[] args) {
        memoized.put(1, 1);
        memoized.put(2, 1);

     * Given a non-negative number FIBINDEX, returns
     * the Nth Fibonacci number, where N equals FIBINDEX.
     * @param fibIndex The index of the Fibonacci number
     * @return The Fibonacci number
    public static int fibonacci(int fibIndex) {
        if (memoized.containsKey(fibIndex)) {
            return memoized.get(fibIndex);
        } else {
            memoized.put(fibIndex, answer);
            return answer;


"Hello, world" program[edit]

The traditional "Hello, world!" program can be written in Java as:[43]

class HelloWorldApp {
    public static void main(String[] args) {











Comprehensive example[edit]

import javax.swing.JOptionPane;

public class OddEven {

    private int userInput; // a whole number("int" means integer)

     * is being created.
    public OddEven() {
         * In most Java programs constructors can initialize objects with default values, or create
         * other objects that this object might use to perform its functions. In some Java programs, the
         * constructor may simply be an empty function if nothing needs to be initialized prior to the
         * functioning of the object. In this program's case, an empty constructor would suffice.
         * A constructor must exist; however, if the user doesn't put one in then the compiler
         * will create an empty one.

     * @param args command line arguments (unused)
    public static void main(final String[] args) {
       OddEven number = new OddEven();

    public void showDialog() {
        try {
             * display either "Even" or "Odd."
            userInput = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number."));
        } catch (final NumberFormatException e) {

     * or the terminal (For *nix users).(Assuming it's open)
    private void calculate() {
        if ((userInput % 2) == 0) {
            JOptionPane.showMessageDialog(null, "Even");
        } else {
            JOptionPane.showMessageDialog(null, "Odd");
  • The import statement imports the JOptionPane class from the javax.swing package.
  • The OddEven class declares a single private field of type int named userInput. Every instance of the OddEven class has its own copy of the userInput field. The private declaration means that no other class can access (read or write) the userInput field.
  • The calculate() method is declared without the static keyword. This means that the method is invoked using a specific instance of the OddEven class. (The reference used to invoke the method is passed as an undeclared parameter of type OddEven named this.) The method tests the expression userInput % 2 == 0 using the if keyword to see if the remainder of dividing the userInput field belonging to the instance of the class by two is zero. If this expression is true, then it prints Even; if this expression is false it prints Odd. (The calculate method can be equivalently accessed as this.calculate and the userInput field can be equivalently accessed as this.userInput, which both explicitly use the undeclared this parameter.)
  • userInput = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number")); is a statement that converts the type of String to the primitive data type int by using a utility function in the primitive wrapper class Integer.

Special classes[edit]


Main article: Java applet


import javax.swing.JApplet;
import java.awt.Graphics;

public class Hello extends JApplet {
    public void paintComponent(final Graphics g) {

The import statements direct the Java compiler to include the javax.swing.JApplet and java.awt.Graphics classes in the compilation. The import statement allows these classes to be referenced in the source code using the simple class name (i.e. JApplet) instead of the fully qualified class name (FQCN, i.e. javax.swing.JApplet).


The Hello class overrides the paintComponent(Graphics) method (additionally indicated with the annotation, supported as of JDK 1.5, Override) inherited from the Container superclass to provide the code to display the applet. The paintComponent() method is passed a Graphics object that contains the graphic context used to display the applet. The paintComponent() method calls the graphic context drawString(String, int, int) method to display the "Hello, world!" string at a pixel offset of (65, 95) from the upper-left corner in the applet's display.

<!-- Hello.html -->
        <title>Hello World Applet</title>
        <applet code="Hello.class" width="200" height="200">

An applet is placed in an HTML document using the <applet> HTML element. The applet tag has three attributes set: code="Hello" specifies the name of the JApplet class and width="200" height="200" sets the pixel width and height of the applet. Applets may also be embedded in HTML using either the object or embed element,[44] although support for these elements by web browsers is inconsistent.[45] However, the applet tag is deprecated, so the object tag is preferred where supported.

The host application, typically a Web browser, instantiates the Hello applet and creates an AppletContext for the applet. Once the applet has initialized itself, it is added to the AWT display hierarchy. The paintComponent() method is called by the AWT event dispatching thread whenever the display needs the applet to draw itself.


Main article: Java Servlet


import javax.servlet.*;

public class Hello extends GenericServlet {
    public void service(final ServletRequest request, final ServletResponse response)
    throws ServletException, IOException {
        final PrintWriter pw = response.getWriter();
        try {
            pw.println("Hello, world!");
        } finally {

The import statements direct the Java compiler to include all the public classes and interfaces from the and javax.servlet packages in the compilation. Packages make Java well suited for large scale applications.

The Hello class extends the GenericServlet class; the GenericServlet class provides the interface for the server to forward requests to the servlet and control the servlet's lifecycle.

The Hello class overrides the service(ServletRequest, ServletResponse) method defined by the Servlet interface to provide the code for the service request handler. The service() method is passed: a ServletRequest object that contains the request from the client and a ServletResponse object used to create the response returned to the client. The service() method declares that it throws the exceptions ServletException and IOException if a problem prevents it from responding to the request.

The setContentType(String) method in the response object is called to set the MIME content type of the returned data to "text/html". The getWriter() method in the response returns a PrintWriter object that is used to write the data that is sent to the client. The println(String) method is called to write the "Hello, world!" string to the response and then the close() method is called to close the print writer, which causes the data that has been written to the stream to be returned to the client.

JavaServer Pages[edit]

Main article: JavaServer Pages


Swing application[edit]

Main article: Swing (Java)



// (Java SE 5)
import javax.swing.*;

public class Hello extends JFrame {
    public Hello() {
        super.add(new JLabel("Hello, world!"));

    public static void main(final String[] args) {
        new Hello();






Main article: Generics in Java



Main article: Criticism of Java

Criticisms directed at Java include the implementation of generics,[46] speed,[47] the handling of unsigned numbers,[48] the implementation of floating-point arithmetic,[49] and a history of security vulnerabilities in the primary Java VM implementation HotSpot.[50]

Use on unofficial software platforms[edit]

The Java programming language requires the presence of a software platform in order for compiled programs to be executed. A well-known unofficial Java-like software platform is the Android software platform, which allows the use of Java 6 and some Java 7 features, uses a different standard library (Apache Harmony reimplementation), different bytecode language and different virtual machine, and is designed for low-memory devices such as smartphones and tablet computers.

The Android operating system makes extensive use of Java-related technology


Google and Android, Inc. have chosen to use Java as a key pillar in the creation of the Android operating system, an open source mobile operating system. Although the Android operating system, built on the Linux kernel, was written largely in C, the Android SDK uses the Java language as the basis for Android applications. However, Android does not use the Java virtual machine, instead using Java bytecode as an intermediate step and ultimately targeting Android's own Dalvik virtual machine or more recently Android Runtime which actually compiles applications to native machine code upon installation.

Android also does not provide the full Java SE standard library, although the Android class library does include an independent implementation of a large subset of it. This 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.[51] District Judge William Haskell Alsup ruled on May 31, 2012, that APIs cannot be copyrighted,[52] but this was reversed by the United States Court of Appeals for the Federal Circuit in May 2014.[53][54][55]

Class libraries[edit]

Main article: Java Class Library


  • The core libraries, which include:
  • The integration libraries, which allow the application writer to communicate with external systems. These libraries include:
  • 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
    • JavaFX
  • 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






See also[edit]

Comparison of Java with other languages[edit]


  1. ^ a b "The History of Java Technology". Retrieved October 6, 2012. 
  2. ^ a b "Java 7 and Java 8 Releases by date". Retrieved March 3, 2015. 
  4. ^ Gosling, James; and McGilton, Henry (May 1996). "The Java Language Environment". 
  5. ^ Gosling, James; Joy, Bill; Steele, Guy; and Bracha, Gilad. "The Java Language Specification, 2nd Edition". 
  6. ^ "The A-Z of Programming Languages: Modula-3". Retrieved 2010-06-09. 
  7. ^ Niklaus Wirth stated on a number of public occasions, e.g. in a lecture at the Polytechnic Museum, Moscow in September, 2005 (several independent first-hand accounts in Russian exist, e.g. one with an audio recording: Filippova, Elena (September 22, 2005). "Niklaus Wirth's lecture at the Polytechnic Museum in Moscow". ), that the Sun Java design team licenced the Oberon compiler sources a number of years prior to the release of Java and examined it: a (relative) compactness, type safety, garbage collection, no multiple inheritance for classes – all these key overall design features are shared by Java and Oberon.
  9. ^ TechMetrix Research (1999). "History of Java" (PDF). Java Application Servers Report. The project went ahead under the name "green" and the language was based on an old model of UCSD Pascal, which makes it possible to generate interpretive code 
  10. ^ "A Conversation with James Gosling – ACM Queue". 2004-08-31. Retrieved 2010-06-09. 
  11. ^ "Facebook Q&A: Hack brings static typing to PHP world". InfoWorld. 2014-03-26. Retrieved 2015-01-11. 
  12. ^ Gosling et al. 2014, p. 1.
  14. ^ a b c "1.2 Design Goals of the Java™ Programming Language". Oracle. 1999-01-01. Retrieved 2013-01-14. 
  16. ^ RedMonk Index on (Stephen O'Grady, January 2015)
  17. ^ "Programming Language Popularity". 2013-10-25. Retrieved 2015-04-02. Normalized Comparison: 1st C, 2nd Java, 3rd PHP 
  24. ^ "JSG – Java Study Group". 
  26. ^ "What is ECMA—and why Microsoft cares". 
  27. ^ "Java Community Process website". 2010-05-24. Retrieved 2010-06-09. 
  29. ^ "Sun's Evolving Role as Java Evangelist". O'Reilly Media. 
  30. ^ "Oracle and Java". Oracle Corporation. Retrieved 2010-08-23. Oracle has been a leading and substantive supporter of Java since its emergence in 1995 and takes on the new role as steward of Java technology with a relentless commitment to fostering a community of participation and transparency. 
  31. ^ "Learn About Java Technology". Oracle. Retrieved 21 November 2011. 
  32. ^ Gosling, James (April 9, 2010). "Time to move on...". On a New Road. Retrieved 2011-11-16. 
  34. ^ Niccolai, James (January 23, 2001). "Sun, Microsoft settle Java lawsuit". JavaWorld (International Data Group). Retrieved 2008-07-09. 
  35. ^ Jelovic, Dejan. "Why Java will always be slower than C++". Retrieved 2008-02-15. 
  38. ^ "NullPointerException". Oracle. Retrieved 2014-05-06. 
  39. ^ "Exceptions in Java". Retrieved 2010-08-10. 
  41. ^ "Operator Overloading (C# vs Java)". C# for Java Developers. Microsoft. Retrieved 10 December 2014. 
  42. ^ "Multiple Inheritance of State, Implementation, and Type". The Java™ Tutorials. Oracle. Retrieved 10 December 2014. 
  44. ^ "Using applet, object and embed Tags". Retrieved 2010-10-14. 
  45. ^ "Deploying Applets in a Mixed-Browser Environment". Retrieved 2010-10-14. 
  46. ^ Arnold, Ken. "Generics Considered Harmful". Retrieved 4 July 2011. 
  47. ^ Jelovic, Dejan. "Why Java Will Always Be Slower than C++". Retrieved 17 October 2012. 
  48. ^ Owens, Sean R. "Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)". Archived from the original on 2004-11-09. Retrieved 2011-07-04. 
  50. ^ "Have you checked the Java?". 
  51. ^ Mullin, Joe. "Google guilty of infringement in Oracle trial; future legal headaches loom". Law & Disorder. Ars Technica. Retrieved 8 May 2012. 
  53. ^
  55. ^ "ORACLE AMERICA, INC., Plaintiff – Appellant, v. GOOGLE INC., Defendant – Cross – Appellant." (PDF). Court of Appeals for the Federal Circuit. May 9, 2014. Retrieved 2014-05-10. 
  56. ^ "Collections Framework Overview". Java Documentation. Oracle. Retrieved 18 December 2014. 
  57. ^ "Java™ Security Overview". Java Documentation. Oracle. Retrieved 18 December 2014. 
  58. ^ "Trail: Internationalization". The Java™ Tutorials. Oracle. Retrieved 18 December 2014. 
  59. ^ "How to Write Doc Comments for the Javadoc Tool". Oracle Technology Network. Oracle. Retrieved 18 December 2014. 
  60. ^ "Java Card Overview". Oracle Technology Network. Oracle. Retrieved 18 December 2014. 
  61. ^ "Java Platform, Micro Edition (Java ME)". Oracle Technology Network. Oracle. Retrieved 18 December 2014. 
  62. ^ "Java SE". Oracle Technology Network. Oracle. Retrieved 18 December 2014. 
  63. ^ "Java Platform, Enterprise Edition (Java EE)". Oracle Technology Network. Oracle. Retrieved 18 December 2014. 


External links[edit]