Java Basics
Topics in this section include:
• What makes Java programs portable, secure, and robust
• The structure of Java applets and applications
• How Java applications are executed
• How applets are invoked and executed
• The Java Language, Part I
• Comments
• Declarations
• Expressions
• Statements
• Garbage collection
• Java Semantics
Portability
Java programs are portable across operating systems and hardware environments.
Portability is to your advantage because:
• You need only one version of your software to serve a broad market.
• The Internet, in effect, becomes one giant, dynamic library.
• You are no longer limited by your particular computer platform.
Three features make Java String programs portable:
1. The language. The Java language is completely specified; all data-type sizes and
formats are defined as part of the language. By contrast, C/C++ leaves these
"details" up to the compiler implementor, and many C/C++ programs therefore
are not portable.
. Java Basics -1
,Java Basics
2. The library. The Java class library is available on any machine with a Java
runtime system, because a portable program is of no use if you cannot use the
same class library on every platform. Window-manager function calls in a Mac
application written in C/C++, for example, do not port well to a PC.
3. The byte code. The Java runtime system does not compile your source code
directly into machine language, an inflexible and nonportable representation of
your program. Instead, Java programs are translated into machine-independent
byte code. The byte code is easily interpreted and therefore can be executed on
any platform having a Java runtime system. (The latest versions of the Netscape
Navigator browser, for example, can run applets on virtually any platform).
Security
The Java language is secure in that it is very difficult to write incorrect code or
viruses that can corrupt/steal your data, or harm hardware such as hard disks.
There are two main lines of defense:
• Interpreter level:
• No pointer arithmetic
• Garbage collection
• Array bounds checking
• No illegal data conversions
• Browser level (applies to applets only):
• No local file I/O
• Sockets back to host only
• No calls to native methods
Robustness
The Java language is robust. It has several features designed to avoid crashes
during program execution, including:
• No pointer arithmetic
• Garbage collection--no bad addresses
• Array and string bounds checking
Java Basics -2 .
, Java Basics
• No jumping to bad method addresses
• Interfaces and exceptions
Java Program Structure
A file containing Java source code is considered a compilation unit. Such a
compilation unit contains a set of classes and, optionally, a package definition to
group related classes together. Classes contain data and method members that
specify the state and behavior of the objects in your program.
Java programs come in two flavors:
• Standalone applications that have no initial context such as a pre-existing main
window
• Applets for WWW programming
The major differences between applications and applets are:
• Applets are not allowed to use file I/O and sockets (other than to the host
platform). Applications do not have these restrictions.
• An applet must be a subclass of the Java Applet class. Aplications do not need to
subclass any particular class.
• Unlike applets, applications can have menus.
• Unlike applications, applets need to respond to predefined lifecycle messages
from the WWW browser in which they're running.
Java Program Execution
The Java byte-code compiler translates a Java source file into
machineindependent byte code. The byte code for each publicly visible class is
placed in a separate file, so that the Java runtime system can easily find it. If your
program instantiates an object of class A, for example, the class loader searches
the directories listed in your CLASSPATH environment variable for a file called
A.class that contains the class definition and byte code for class A.
There is no link phase for Java programs; all linking is done dynamically at
runtime.
. Java Basics -3