Java is an object-oriented programming language developed by James Gosling and colleagues at Sun Microsystems in the early 1990s. Unlike conventional languages which are generally designed to be compiled to native code, Java is compiled to a bytecode which is then run (generally using JIT compilation) by a Java virtual machine.
The language itself borrows much syntax from C and C++ but has a much simpler object model and does away with low-level tools like programmer-manipulated pointers.
Java is only distantly related to JavaScript, though they have similar names and share a C-like syntax.
As with other parts of the Java platform, the Java language has evolved over the years while largely maintaining backwards compatibility.
strictfp keyword
int) and primitive wrapper classes (such as ). (Specified by JSR 201.)
enum keyword creates a typesafe, ordered list of values (such as Day.MONDAY, Day.TUESDAY, etc.). Previously this could only be achieved by non-typesafe constant integers or manually constructed classes (typesafe enum pattern). (Specified by JSR 201.)
void drawtext(String... lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method.
for loop — the for loop syntax is extended with special syntax for iterating over each member of either an array or any , such as the standard classes, using a construct of the form:
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 JDK 1.0 to over three thousand in J2SE 5.0. Entire new APIs, such as Swing and Java2D, have been introduced, and many of the original JDK 1.0 classes and methods have been deprecated.
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.
Another primary goal of OO programming is to develop more generic objects so that software can become more reusable between projects. A generic "customer" object, for example, should in theory have roughly the same basic set of behaviors between different software projects, especially when these projects overlap on some fundamental level as they often do in large organizations. In this sense, software objects can hopefully be seen more as pluggable components, helping the software industry build projects largely from existing and well tested pieces, thus leading to a massive reduction in development times. Software reusability has met with mixed practical results, with two main difficulties: the design of truly generic objects is poorly-understood, and a methodology for broad communication of reuse opportunities is lacking. Some open source communities want to help ease the reuse problem, by providing authors with ways to disseminate information about generally reusable objects and object libraries.
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. 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's 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 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". 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. In response, 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 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. More sophisticated VMs use dynamic recompilation, in which the VM can analyze the behavior of the running program and selectively recompile and optimise 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. 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.
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 objects' 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. On the whole, Java's automatic garbage collection makes creation and deletion of objects in Java simpler, potentially safer, and often faster than in C++.
Like many comparisons between 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), but doing so requires extra development time and adds considerable complexity to an application. 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.
The syntax of Java is largely derived from C++. But unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built from the ground up to be fully object-oriented. Everything in Java is an object (with a few exceptions), and everything in Java is written inside a class.
The above example merits a bit of explanation.
class which is declared public is required to follow this convention. (In this case, the class is Hello, therefore the source must be stored in a file called Hello.java).
0.
main() method.
void indicates that the main method does not return anything.
args although any other legal identifier name can be used.
static indicates that the method is a class method, associated with the class rather than object instances. Main methods must be static.
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. Main methods must also be public.
out object is an instance of the class and provides the method for displaying data to the screen (standard out).
java -cp . Hello will start the above program (compiled into Hello.class) from the current directory. The name of the class whose main method is to be invoked can also be specified in the MANIFEST of a Java archive (Jar) file.
Java applets are programs that are embedded in other applications, typically in a Web page displayed in a Web browser.
// Hello.java import java.applet.Applet; import java.awt.Graphics;
public class Hello extends Applet { public void paint(Graphics gc) { gc.drawString("Hello, world!", 65, 95); } }
<!-- Hello.html -->
The import statements direct the Java compiler to include the and classes in the compilation. The import statement allows these classes to be referenced in the source code using the simple class name (i.e. Applet) instead of the fully-qualified class name (i.e. java.applet.Applet).
The Hello class extends (subclasses) the Applet class; the Applet class provides the framework for the host application to display and control the lifecycle of the applet. The Applet class is an Abstract Windowing Toolkit (AWT) , which provides the applet with the capability to display a graphical user interface (GUI) and respond to user events.
The Hello class overrides the method inherited from the superclass to provide the code to display the applet. The paint() method is passed a Graphics object that contains the graphic context used to display the applet. The paint() method calls the graphic context method to display the "Hello, world!" string at a pixel offset of (65, 95) in the applet's display.
An applet is placed in an HTML document using the HTML element. The applet tag has three attributes set: code="Hello" specifies the name of the Applet 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, although support for these elements by Web browsers is inconsistent.**)
// Hello.java import java.io.*; import javax.servlet.*;
public class Hello extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("Hello, world!"); pw.close(); } }
The import statements direct the Java compiler to include all of the public classes and interfaces from the and packages in the compilation.
The Hello class extends the 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 method defined by the interface to provide the code for the service request handler. The service() method is passed a object that contains the request from the client and a object used to create the response returned to the client. The service() method declares that it throws the exceptions and if a problem prevents it from responding to the request.
The method in the response object is called to set the MIME content type of the returned data to "text/html". The method in the response returns a object that is used to write the data that is sent to the client. The method is called to write the "Hello, world!" string to the response and then the 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.
// Hello.java import javax.swing.*;
public class Hello extends JFrame { Hello() { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); add(new JLabel("Hello, world!")); pack(); }
public static void main(String* args) { new Hello().setVisible(true); } }
The import statement directs the Java compiler to include all of the public classes and interfaces from the package in the compilation.
The Hello class extends the class; the JFrame class implements a window with a title bar with a close control.
The Hello() constructor initializes the frame by first calling the method inherited from JFrame to set the default operation when the close control on the title bar is selected to —this causes the JFrame to be disposed of when the frame is closed (as opposed to merely hidden), which allows the JVM to exit and the program to terminate. Next a new is created for the string "Hello, world!" and the method inherited from the superclass is called to add the label to the frame. The method inherited from the superclass is called to size the window and layout its contents.
The main() method is called by the JVM when the program starts. It instantiates a new Hello frame and causes it to be displayed by calling the method inherited from the superclass with the boolean parameter true. Note that once the frame is displayed, exiting the main method does not cause the program to terminate because the AWT event dispatching thread remains active until all of the Swing top-level windows have been disposed.
Java was intended to serve as a novel way to manage software complexity. Most consider Java technology to deliver reasonably well on this promise. However, Java is not without flaws, and it does not universally accommodate all programming styles, environments, or requirements (see Java criticisms for a thorough analysis of common criticisms about the language).
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 the Java Community Process program. Companies or individuals participating in this process can influence the design and development of the APIs. This process has been a subject of controversy.
In 2004, IBM and BEA publicly supported the notion of creating an official open source implementation of Java. As of 2006, Sun has agreed that they will eventually open source Java, the When and How are still to be defined.
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 programming language | Java platform | Java specification requests | C programming language family | Programming languages | Sun Microsystems | Concurrent programming languages
جافا | Java programski jezik | Java | Llenguatge Java | Java | Java (programmeringssprog) | Java (Programmiersprache) | Java | Java | Lenguaje de programación Java | Java | جاوا | Java (langage) | Linguaxe Java | 자바 프로그래밍 언어 | Java (programski jezik) | Java | Java (forritunarmál) | Java (linguaggio) | Java | Java პროგრამული ენა | Java (valoda) | Java (kalba) | Java programozási nyelv | Јава (програмски јазик) | Java (programmeertaal) | Java言語 | Java (programmeringsspråk) | Java | Java (linguagem de programação) | Java | Java | Programski jezik Java | Java | Java | Java (programspråk) | Java programming language | ஜாவா நிரலாக்க மொழி | ภาษาจาวา | Java (ngôn ngữ lập trình) | Java programlama dili | Мова програмування Java | Java
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Java programming language".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world