Title: Java Programming Introduction
1Java Programming Introduction
- Introduction to Java
- Java is a new object-oriented language developed
at SunLabs. HotJava is a Web browser written in
Java. - In Java, classes are organized into groups called
packages. The standard release of Java comes with
a set of packages dealing with such things as
multithreading, file I/O, networking, and
graphics. These core classes should be regarded
as an essential part of the language the Java
language specification together with the core
class APIs define the basic semantics of Java
programs. - Netscape Navigator and other browsers can also
display Java applets. - The development environment for Java is a kind of
hybrid between the traditional compile-link-execut
e model typical of languages like C, and the
interepreted model characteristic of Lisp and
Basic. The Java compiler translates Java source
into an architecturally neutral intermediate form
known as bytecode.
2Java Programming Introduction
- Bytecode is interpreted by the Java runtime
system or interpreter, whose purpose is to
emulate a virtual machine the semantics of this
machine are given by the Java virtual machine
(VM) specification. - When HotJava downloads an applet, bytecode is
what actually comes over the net. - Java bytecode requires no linking or expressed
differently, classes are dynamically linked in as
needed. The Java interpreter performs various
runtime checks some of these can be done
statically when the bytecode is loaded. The
bytecode verifier performs these static checks.
(The bytecode verifier is actually a part of the
interpreter.) - Bytecode can also be translated into the machine
code of the host platform for better performance.
Just-in-time compilers have also been developed.
3Java Programming Introduction
- Java versus C
- Syntactically, Java is very much like C with a
few elements of Objective C. However, Java is not
an upwardly-compatible extension of C. All of
the constructs that were deemed complicated or
not useful in C were thrown out - Preprocessor directives, e.g., typedef, define.
- goto statements.
- Automatic coercions, and permissive casting in
general - Free-ranging C-style pointers
- Structures and unions.
- Non-member functions.
- Operator overloading.
- Multiple inheritance.
- Many of these features have been eliminated
because of security concerns others in a quest
for simplicity.
4Java Programming Introduction
- Some Java constructs serve as functional
replacements for deleted C features - Arrays as first-class objects, to replace
pointers. - Interfaces, as a substitute for multiple
inheritance. A class can extend one class while
importing an interface implemented by another
class. - Certain error-prone programming tasks receive
support at the language level - Memory management. Memory is allocated via the
new operator, as in C. Deallocation is no
longer the programmer's responsibility. The Java
interpreter uses an asynchronous mark-and-sweep
garbage collector, much like that found in LISP
development environments, to clean up unused
memory. - Multithreading. The Java language provides for
thread and synchronization support. Thread
creation and destruction is managed like any
other object. If a method is declared
synchronized, then a thread applying the method
to an object must first acquire the lock for that
object. The locks are managed by the
5Java Programming Introduction
- interpreter, invisibly to the programmer. (This
is essentially the monitor concept of Brinch
Hansen and Hoare). - There is a precisely specified interface for
calling C code from Java. A Java method whose
body is implemented in C is called a native
method. - Finally exception handling. Here Java adopts the
C approach. An exception is an object like any
other code causing an exception is said to throw
the exception. (For example, the new operator
will throw an OutOfMemoryException if the Java
interpreter cannot allocate the memory required.) - There are syntactic constructs enabling a
programmer to catch exceptions, i.e., pass them
to an exception handler. The exception handler
need not completely recover it can perform some
clean-up actions and then rethrow the exception.
6Java Programming Introduction
- Advantages over C
- The creators of Java claim several advantages
for it over C - Simplicity. The Java designers' spartan
philosophy has resulted in a economical language
that is easy to learn if one is familiar with
C. - Robustness. Automatic garbage collection
eliminates one of the most difficult classes of
bugs in standard compiled languages memory
leaks, segmentation faults, bus errors,
null-pointers, and the like. - The thread model of Java is based on the
well-known monitor paradigm, making it likely
that it will be used and well-understood by the
development community. Note that multithreading
is especially convenient for graphical user
interfaces such as HotJava. - Architecture neutrality, i.e., portability in the
strongest sense. (Note that this is key to the
Web applications of Java.) Java bytecode is
architecturally neutral and the Java environment
provides classes to support user interactivity
(the awt package). The inclusion of
multithreading support into the language also
contributes to portability.
7Java Programming Introduction
- An applet or full-scale program written in Java
will immediately run on any system that has a
Java interpreter, regardless of operating system
or CPU type. There is no longer a need to port to
different operating systems, different thread
packages, different endian machines, or different
windowing systems. - Extensibility. Provisions are made in Java for
extension of the capabilities of the language.
This flexibility can be seen in the
object-oriented nature of Java through the use of
inheritance, interfaces, and runtime binding.
The most striking example occurs when new classes
are dynamically downloaded over the net and
incorporated into a running Java system. - The ability to link with C code through the use
of native methods offers a quick way to leverage
an existing code base. This has its drawbacks
loss of portability and security. Under the right
circumstances however the trade-off may be
worthwhile. However, an applet cannot download C
code and link it in the native methods must be
installed locally by the local user or site
administrator.
8Java Programming Introduction
- Security. To the extent that breaches in security
exploit bugs in trusted software, enhanced
security should flow from enhanced robustness.
However, Java can stake a more specific claim to
enlarging the inventory of security mechanisms. - Core Java Class APIs
- The following core packages are available in
Java - java.lang.
- java.util.
- java.io.
- java.net.
- java.awt.
- Java.swing.
- java.applet.
9Java Programming Introduction
- HotJava and Applets
- The HotJava browser is simply a Java application.
One launches an instance of HotJava by having the
Java interpreter load and run a particular class,
browser.hotjava. The browser can run multiple
applets.
Applet
Applet
Hotjava Browser
Java Intrepeter
Host System
When HotJava loads a page containing an APPLET
tag, it loads the class specified in the tag
this is how applets get started. The APPLET tag
contains a attribute of the form
code"ltfilenamegt" telling it the name of the file
containing the bytecode for the class. The
default location for the file is in the same
directory, on the same Web server, as the HTML
page containing the APPLET tag. An optional
codebase attribute can specify an explicit URL
instead of this default.