Java is an object-oriented programming language that you can use to develop applets, servlets, and standalone applications. This chapter provides you an overview of Java and gets you started with a few simple programming examples.
By reading this chapter, you learned the following:
To write Java programs, you need a Java development environment. One freely available Java environment is the Java 2 Standard Edition Software Developers Kit (Java 2 SDK or J2SE SDK) from Sun Microsystems. You can download the Java 2 SDK from http://java.sun.com/j2se/downloads.html.
If you want to develop servlets, you also need the Java 2 Enterprise Edition (J2EE) SDK. You can download the J2EE SDK by following the links from http://java.sun.com/products/servlet/download.html.
The compiled Java code is known as Java byte code.
There are several types of Java programs-standalone Java applications, applets, and servlets. Standalone applications can be executed using the Java interpreter that comes with the Java 2 SDK. Java applets must be embedded in an HTML document and run under the appletviewer (a program that comes with the Java 2 SDK) or a Java-capable Web browser. Java servlets run on the Web server and typically perform tasks such as process HTML forms, access a database, and prepare dynamic HTML documents.
To embed a Java applet in an HTML document, you use the <applet> tag.
Java has a syntax that's similar to that of C and C++. Like C++, Java is an object-oriented programming language. Everything in Java must be an object. Unlike C and C++, Java does not allow any standalone functions.
Java supports both C-style comments (enclosed within /*...*/) and C++-style comments (comment begins with //). Additionally, Java also supports a third-style comment that begins with /** and ends with */ (this style of comment is called a doc comment).
A Java class defines an object type. The class is the template from which an object can be created (or instantiated). The class includes data and procedures that operate on the data. These procedures are referred to as methods.
A class can inherit from another class thereby establishing a parent-child relationship. The parent class is referred to as the superclass, and the child class is called the subclass. Unlike C++, Java supports single inheritance only-that means a subclass can inherit from at most one superclass.
Java supports the primitive C data types (such as int, char, float, double) as well as the basic programming C constructs (such as for, while, if...else). Java introduces two new primitive types: boolean and byte.
Java does not include any pointers. Unlike C and C++, you cannot manipulate pointers to access blocks of memory.
Java introduces the interface keyword, which is a set of methods that any class may implement. The interface defines a set of capabilities.
Java supports multiple threads, where each thread is an independent sequence of execution. That means a single application or applet may have multiple independent threads of execution-it's multitasking at the individual process level. You can have a thread download data from the server while another thread updates the applet's display.
Like the run-time library in C, Java comes with a number of class packages-these are the foundation classes on which you build a Java application. For example, Java applets are subclassed from the Applet class in the java.applet package. Applets display user interfaces (such as buttons and menus) and generate graphics output by using classes from the java.awt package.
Java Foundation Classes (JFC), included in the Java 2 SDK, includes the Swing classes that enable you to create more complex GUIs in Java.