1 / 20

Chapter 3

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.

ivo
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Programs and Packages

  2. 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.

  3. 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.

  4. Program types • Java has four program types, although the differences among them are shallow. • The four types are: • Application • Applet • Servlet • Bean

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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 { ...

  18. 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.

  19. 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.*;

  20. 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.

More Related