Search This Blog

Wednesday, 5 November 2014

Java

Java is a computer language similar in structure to C++. Although Java is a general-purpose programming language, it is most often used for creating applications to run on the Internet, such as Web servers. A special type of Java pro-gram called an applet can be linked into Web pages and run on the user’s Web browser (see applet).

As an object-oriented language, Java uses classes that provide commonly needed functions including the creation of user interface objects such as windows and buttons (see class and object-oriented programming). A variety of sets of classes (“class frameworks”) are available, such as the AWT (Abstract Windowing Toolkit).

Program Structure

A Java program begins by importing or defining classes and using them to create the objects needed for the program’s functions. Code statements then create the desired output or interaction from the objects, such as drawing a picture or put-ting text in a window. Here is a simple Java applet program:

import java.applet.Applet; import java.awt.Graphics;
public class HelloWorld extends Applet { public void paint(Graphics g) {
g.drawString(“Hello world!”, 50, 25);
}
}

The first two lines import (bring in) standard classes. The applet class is the foundation on which applet programs are built. The AWT (Abstract Windowing Toolkit) is a set of classes that provide a graphical user interface.

The program then declares a new class called Hello-World and specifies that it is built on (extends) the applet class. Next is a declaration for a method (procedure for doing something) called paint. This method uses a graphics object g that includes various capabilities for drawing things on the screen. Finally, the program uses the graphic object’s predefined drawstring method to draw a string of text.

To develop this program, the programmer compiles it with the Java compiler. He or she then creates an HTML page that includes a tag that specifies that this code is to be run when the link is activated (see html).

Development of Java

Java was created by James Gosling (1955–  ). It began as an in-house project at Sun Microsystems to design a language that could be used to program “smart” consumer devices such as an interactive television. When this project was abandoned, Gosling, Bill Joy, and other developers realized that the language could be adapted to the rapidly growing Internet. Developers of Web pages needed an easier way to create programs that could run when the page was accessed by a user. By implementing user controls on Web pages, the designers could give Web users the ability to interact on-line in much the same way they interact with objects on the screen on a Macintosh or Windows PC.

Advantages

Java has largely fulfilled this promise for Web developers. C++ programmers have an easy learning curve to Java, since the two languages have very similar syntax and a similar use of classes and other object-oriented features. On the other hand, programmers who don’t know C++ benefit from Java being more streamlined than C++. For example, Java avoids the necessity to use pointers (see pointers and indirection) and uses classes as the consistent building block of program structure. Software powerhouses such as Microsoft (until recently) and IBM have joined Sun in pro-moting Java.

Another much-touted feature of Java is its platform independence. The language itself is separate from the vari-ous operating system platforms. For each platform, a Java Virtual Machine (JVM) is created, which interprets or com-piles the code generated by the Java compiler so it can run on that platform.

For security, Java applets run within a “sandbox” or restricted environment so the user is protected from mali-cious Java programs. (For example, programs are not allowed to access the user’s disk or to connect the user’s machine to another Web site.) Web browsers can also be set to disable the running of Java applets.

A Mature Technology

Sun Java comes in two basic “flavors”: the Java 2 Standard Edition (J2SE) for Microsoft Windows, Sun (Solaris), and Linux, and the Enterprise Editions (J2EE), which includes features needed in large, complex environments. Micro-soft developed its own dialect of Java for Windows, but effectively abandoned it as a result of legal action by Sun. (Companies are allowed to develop Java implementations for various platforms, so long as they pass Sun’s strict vali-dation process.)

Java has paid particular attention to building reusable software components. “JavaBeans” package a number of related objects (classes) into a unit that can be accessed through a standard set of methods and automatically que-ried for information about their contents.

Today powerful and well-documented Java program-ming interfaces are available for working with Web services. While client-side Java applets run in the Web browser, Java Server Pages (JSPs) embed code in an HTML page. The code is compiled into a server-side application or “servlet.” XML processing and database access is provided through the Java API for XML (JAX).

Java has largely fulfilled its promise of bringing main-stream object-oriented programming to a wide variety of platforms. The language is now often taught as a first lan-guage instead of C or C++. However, the idea of a single dominant language seems to be no longer applicable in the rapidly evolving world of software development.

In 2006 Sun Microsystems announced that it would make Java’s source code freely available (see open source). In part this may be an attempt to maintain Java’s posi-tion among programmers, some of whom have shifted their attention from Java to Microsoft’s own offshoot of C++ (see c#). However, Java’s greatest challenge seems to be in the Web programming area, where it faces increasing competi-tion from more agile languages (see, for example, Ruby) as well as a variety of scripting languages that may be easier to learn and quicker to use for many applications.

No comments:

Post a Comment