A Java Session - Java Programming Language
Java Links
|
Home
|
Java Programming Language
A Java Session
Presented by Wick Gankanda
to
IBM - xSeries Server Group
October 7, 2005 - Research Triangle Park, North Carolina
and
Los Angeles County - Museum of Natural History
July 13, 2001 - Los Angeles, California
Updated: October 2005
|
|
|
Topics:
|
|
Introduction to Java
|
-
Java Programming Language
-
Lets you write powerful, enterprise-worthy programs
that run in the browser, from the desktop, on a server, or on a consumer device (cell phone).
-
Java programs are run on -- interpreted by -- another program called the Java
Virtual Machine (Java VM or JVM).
-
Rather than running directly on the native operating
system, the program is interpreted by the Java VM for the native operating system.
-
This means that any computer system with the Java VM installed can run a Java
program regardless of the computer system on which the application was originally developed.
The concept of "write once, run anywhere"...
-
Java is a complete object oriented (OO) programming language derived from C++.
-
It has all of the OO advantages of C++, but does away with a number of the more unpleasant
aspects like pointers and memory allocation in the name of sanity, robustness and security.
Java Concepts...
-
Comes with a wide-ranging collection of libraries (also known as packages) that extend the language
-
A comprehensive introduction with examples.
Java vs C++
-
Java does not use pointers. Does away with explicit pointers. Pointers
in the form of memory addresses, are still there under the surface.
-
No overloaded operators. Cannot redefine operators such as +, * and = in Java.
Java versus C++: Object Oriented Programming
Java
|
C++
|
Pure object oriented language - enforces the object oriented approach.
|
Hybrid between procedural and object oriented. Does not enforce
the object oriented approach.
|
All functions (methods) are part of a class.
|
Can have stand-alone functions.
|
No multiple inheritance.
|
Multiple inheritance.
|
Formal interface specifications.
|
No formal interface specifications.
|
No parameterized type.
|
Templates as a parameterized type.
|
All methods (except final methods) are dynamically bound.
|
Virtual functions are dynamically bound.
|
Java versus C++: Special characteristics
Java
|
C++
|
Specifically attuned to network and Web processing. Java API
has specific support for network communication. Links to Java
applets can be embedded in HTML documents, then retrieved
and executed using Web browsers.
|
No relatiionship to networks or the Web.
|
Automatic garbage collection - no memory leaks. The garbage
collector is a daemon thread. An object is marked as a candidate
for garbage collection after the last reference to it is removed.
|
No automatic garbage collection - possible memory leaks. The
programmer must perform explicit dynamic memory management,
releasing objects and other dynamically allocated data space
when it is no longer needed.
|
Combination of compiled and interpreted.
Source code compiled into bytecode, a low-level representation
that is not tied to any particular processor. The bytecode can then
be executed on any platform (Windows, Mac or Unix) that has
a Java interpreter.
|
Compiled. Compilers are specific to each type of processor.
|
Slower execution when interpreted.
|
Fast execution.
|
Archetecture neutral.
|
Archetecture specific.
|
Supports multithreading with synchronization mechanisms.
|
No built-in multithreading.
Therefore must make calls to the OS multithreading primitives.
|
Automatic generation of documentation in HTML format.
|
No automatic documentation generation.
|
No global variables.
|
Global variables.
|
"Write once, run anywhere": Windows, Unix and Mac
From source code to Execution
Graphics by Wick Gankanda
|
-
Create a Java source file. A source file contains text, written in the Java
programming language, that you and other programmers can
understand. You can use any text editor to create and edit source files.
-
Compile the source file into a bytecode file. The Java compiler, javac,
takes your source file and translates its text into instructions that the
Java Virtual Machine (Java VM) can understand. The compiler puts
these instructions into a bytecode file.
-
Run the program contained in the bytecode file. The Java VM is
implemented by a Java interpreter, java. This interpreter takes your
bytecode file and carries out the instructions by translating them into
instructions that your computer can understand.
|
Java Concepts
-
Three essential features of an object-oriented programming (OOP)
-
Object Oriented Design (OOD)
-
Model real-world objects,
-
their attributes,
-
their behavior &
-
communication between objects.
|
|
-
Object Oriented Analysis and Design (OOAD)
-
Follow detailed process for obtaining an analysis of your project's
requirements and
developing a design for satisfying those requirements.
|
|
-
Model
-
A model is an abstraction of the underlying problem.
|
|
-
Object
-
A real world item modeled in software
-
Objects communicate through interfaces
|
|
-
Attributes or data and behaviors or methods of objects
-
Attributes or data AKA variables or values: such as size, shape, color - define the state of an object
-
Behaviors or methods: a car accelerates, brakes and turns - define the behavior of an object
|
|
-
Class
-
Collection of objects with same characteristics.
-
Classes are to objects as blueprints are to houses.
-
In other words, classes are the blueprints for objects.
|
|
-
Package
-
A group of related classes or reusable components
-
Creating packages encourages others to reuse software because it makes it
convenient to import many related classes at once
-
When you create a number of classes that inherit from each other, you will
often find it convenient to place these classes in a package
-
Include a package statement at the beginning of the class file to place compiled code in the indicated folder
-
The package statement must appear outside the class definition
|
|
-
Encapsulation
-
Implement every aspect (properties and behaviors) of a real world object of interest inside the software object.
-
The services it offers to its clients is the contract.
-
Contracts, internal representation of data and information hiding
-
Only the contract defined by the object is available to the client and not the implementation details.
-
In other words, the class is implemented in such a way that the internal representation of data is not exposed to the user of the object.
-
This is information hiding - where implementation details are hidden within objects.
-
It is possible to change the implementation of the object without implication for the clients & reduces complexity.
-
The changes to the implementation are localized to the class.
|
|
-
Preserve Encapsulation
-
All instance variables should be declared private and should be accessible only via set and get methods of the class.
|
|
-
Inheritance
-
A new class of objects get characteristics of existing classes + add unique characteristics of their own.
-
No multiple-inheritance in Java.
(Parents have many children vs. children have many parents [legal, biological, step, surrogate])
-
Using inheritance to achieve good software design:
-
Subclass creators save development time
-
Subclass creators save testing time
-
Programmers who create or use new subclasses already understand how the superclass
works, so the time it takes to learn the new class features is reduced
-
When you create a new subclass in Java, neither the superclass source code
nor the superclass bytecode is changed; the superclass maintains its integrity
|
|
-
Polymorphism
-
Objects having many forms, though derived from a generic superclass
-
Design and implement systems that are more easily extensible
-
Three types: overloading, overriding
and dynamic method binding
-
Can eliminate the need for switch logic
-
Programs take on a simplified appearance, contains less branching logic
|
|
-
-
|
The Project: Nuclear Power Plant with Reactors
|
Using the following concepts in Java:
- Object Oriented Design (OOD): Model real-world objects, their attributes, their behavior &
communication between objects.
-
Object Oriented Analysis and Design (OOAD): follow detailed process for obtaining an analysis
of your project's requirements and developing a design for satisfying those requirements.
-
Java Networking
- client - server Archetecture
- socket-based communications: stream sockets: TCP (& IP)
-
Object Serialization & Deserialization
-
Multithreading
-
GUI Design
-
File I/O
-
Exception Handling
-
Event logging
Documentation available at the Java site at http://java.sun.com
Documentation available at the Java site at http://java.sun.com
Documentation available at the Java site at http://java.sun.com
Documentation available at the Java site at http://java.sun.com
Documentation available at the Java site at http://java.sun.com
Object Serialization & Deserialization
Object Serialization allows an object to be transformed into a sequence of
bytes that can later be re-created (deserialized) into the original object.
Allows reading & writing objects from and to streams
Documentation available at the Java site at http://java.sun.com
Exception Handling: how Java takes care of errors in programs at runtime
Documentation available at the Java site at http://java.sun.com
|
Polymorphism, superclasses, subclasses, dynamic method binding, method overloading and method overriding
|
Superclasses, subclasses and polymorphism
|
Overriding method toString with the exact same signature in class Employee & class Boss
Superclass method: Abstract method that must be implemented for each derived class of Employee
from which objects are instantiated
public abstract double earnings();
Subclass method:
public double earnings() { return weeklySalary; }
The toString() method of the class Boss:
public String toString() {
return "Boss: [Base Pay = " +
weeklySalary +
", Bonus= " + bonus + "] " +
super.toString();
}
|
Employee employee; // superclass reference
Boss boss; // instantiate subclass object class Boss
boss = new Boss(); // new object is assigned to Boss reference boss
employee = boss; // Employee reference to Boss: assigns to superclass Employee reference
// employee a reference to the subclass Boss object to which boss refers.
The following method call invokes the toString()
method of object referenced by employee
, system determines
that the referenced object is a Boss
and invokes subclass Boss
's toString
method,
which is polymorphic behavior.
... employee.toString()
...
Dynamic Method Binding (also called late binding or virtual binding)
Java determines the type of reference object before invoking a method.
-
At execution time the interpreter detemines the type of object to which the superclass object refers to
-
Every object in Java knows its own data type
-
Therefore superclass object refers to subclass object
-
The interpreter calls the method of the subclass object
The following method call invokes the earnings method of the object to which employee
refers, system determines that the object is a Boss
and invokes subclass
Boss
's earnings
method rather than the superclass's
earnings
method, this is equivalent to boss.earnings()
... employee.earnings()
...
Method overloading
You can think of overloading as expanding the versions of a method along the
horizontal axis. For example, here we show class A with several overloaded
versions of the do() method:
|
public class A {
...
void do ()
{..}
void do (int i)
{..}
void do (float x)
{..}
void do (float x, float y)
{..}
} // END class A
|
Call these methods like this:
do();
or do(53);
or do(25.27);
or do(36.45, 84.254);
Method overriding
A subclass may want to provide a new version of a method in the superclass. In fact,
this is usually the whole reason for creating a subclass.
When a subclass method matches in name and in the number and type of arguments to the
method in the super-class (that is, the method signatures match), the subclass is
said to override that method.
In the code below, we see that subclass B
overrides the method doSomething()
in class A:
|
public
class A {
int i = 0;
void doSomething (int k) {
i = k;
}
}
class B extends A {
int j = 0;
void doSomething (int k) {
j = 10;
i = 2 * k;
}
}
|
Abstract class
-
A class from which you cannot create any concrete objects, but from which you can inherit
-
You can only extend abstract classes
-
Use the keyword abstract
-
You cannot use the keyword new
-
In other programming languages, such as C++, abstract classes are known as virtual classes
-
Nonabstract classes from which objects can be instantiated are called concrete classes
|
|
Multithreading
-
Threads — lightweight processes that can run concurrently
-
Platform dependent
-
Solaris Java platform: runs thread to completion or preemption.
-
Windows - 32-bit: threads are timesliced, equaly (time quantum),
threads of equal priority run in round-robin fashion
|
|
|
Reference:
|
Java Links
|
Home