200 likes | 325 Views
Chapter 3. Programs and Packages. Java Virtual Machine (JVM). Java programs execute on the JVM. The JVM is a virtual rather than a physical machine, although the JVM has been implemented in silicon.
E N D
Chapter 3 Programs and Packages
Java Virtual Machine (JVM) • Java programs execute on the JVM. • The JVM is a virtual rather than a physical machine, although the JVM has been implemented in silicon. • The JVM is typically implemented as a run-time interpreter that translates Java bytecode instructions into native instruction codes, which the host platform executes.
Java Virtual Machine (JVM) • The java utility in the JDK provides the JVM as a run-time interpreter. • The JVM provides a run-time environment (runtime for short) that enables programs to execute on a host platform. • The Java runtime can work with a security manager to determine which operations a program can perform on the host platform.
Program types • Java has four program types, although the differences among them are shallow. • The four types are: • Application • Applet • Servlet • Bean
Application type • An application is a standalone program in the sense of requiring only the JVM to execute. • An application does not require a host program such as a browser. • An application has main as its entry point.
Applet type • An applet in the original sense is a small program typically downloaded from a server to a client machine. • A Web browser equipped with a JVM typically acts as the host program for an applet. • An applet is typically launched through an HTML or equivalent document.
Applet type • An applet typically operates under a strict security manager, which imposes sandbox security. Such security prevents an applet from performing potentially dangerous operations such as reading from or writing to the local disk. • An applet’s class descends from the standard Applet class.
Servlet type • A servlet is a program that executes on a server machine, typically to process a request submitted from a client machine. • A servlet’s host program is typically a Web server, which provides a JVM. • A servlet, like an applet, is typically launched from a Web browser but, unlike an applet, executes on the server.
Servlet type • A servlet commonly performs database operations and generates dynamic Web content to displayed on a client’s browser. • A servlet’s class either implements the standard Servlet interface or descends from a class that implements this interface.
Bean type • A bean is a software component, that is, a prebuilt software part that can be integrated with others to build an application. • A bean typically has a distinct, special purpose. Examples are calendar beans, login beans, email beans, and so forth. • A bean’s class or an ancestor implements the standard Serializable interface.
Summary of program types • A given piece of Java code could, in principle, belong to each program type. • Every applet is automatically a bean, for instance. • The key point is that the very same programming constructs are available throughout the program types.
Packages • Files with a .class extension are aggregated into packages, or collections of related classes. • Packages may contain subpackages to arbitrary levels. • The primary package is java and its main subpackage is lang.
Packages • All classes in the java.lang package are automatically imported into every program. • Programmers typically import classes from packages to avoid using fully qualified names. If a program imports the class java.util.Date, the programmer then can use Date instead of the fully qualified name.
Packages • The java and the javax packages are standard packages. • The javax packages are extensions to the earlier java packages. • The standard packages support string and text processing, numeric computation, networking, graphics, security, and so on.
Packages • Packages can be used to resolve name conflicts. • The java.awt package has a List class and the java.util package has a List interface. The fully qualified names java.awt.List java.util.List disambiguate.
Packages • Every class belongs to a package. • The package to which a class belongs can be explicitly declared with the package statement in a source file. • A class belongs to a default unnamed package if a containing package is not explicitly declared.
package statement • The package statement, if present, occurs as the first uncommented line in a source file. • The source file Hi.java could begin package hiPkg; // Note: 1st line import java.util.Date; class Hi { ...
CLASSPATH Variable • Utilities such as the compiler and the run-time interpreter can find standard packages and the classes contained therein. • The CLASSPATH environment variable can be set so that these utilities can find programmer-defined classes in their containing packages, whether explicitly named or default.
Subdirectories as subpackages • A hierarchical file system with directories and subdirectories can be used to implement packages and subpackages. • Suppose that directory MAIN contains a subdirectory SUB. A source file in MAIN could treat SUB as a subpackage and import all of the classes therein with the command import SUB.*;
Summary of packages • Packages are a convenient way to group related classes into software libraries. Standard packages such as java.math do precisely this. • For small programs and projects, default packages are typically sufficient. Explicitly named packages are especially useful for large projects with many programmers.