Held Monday, August 28, 2000
Summary
Today we begin our studies of Java. Our main focus today will be a
high-level overview of the language along with some associated concepts
from object-oriented design.
Notes
- If you haven't done so already, please fill out the
introductory survey.
- Assignments:
- Reread lab J1 for tomorrow.
- Every Monday at noon, the department hosts a brown-bag film festival
at noon. See interesting talks by great computer scientists. (Okay,
sometimes you see mediocre talks by great computer scientists, but then
we all make sarcastic comments.) I'm pretty sure that you can pick
up a bag lunch if you're on meal plan.
- My wife managed to break one of her toes yesterday. Hence, it is likely that
I'll have to head home at about 1 p.m. today.
Overview
- An introduction to object-oriented programming
- An introduction to Java
- Your first Java program
- As you might guess, object-oriented languages concern themselves with
objects.
- You might have a sense of what a ``real world'' object is.
- In computerese, an object is a collection of information
(sometimes called attributes or fields) and
operations that the object can perform (called methods).
- For example, we might say that the textbook for our class
- has title ``Java Structures'' (attribute)
- is published in hardback (attribute)
- was written by Dwayne Bailey (attribute)
- contains text that you can extract (the text is an attribute, the extracting
of the text might be considered a method)
- We often categorize objects into classes. A class specifies
common aspects of a set of objects. These aspects are often
generic attributes and specifications of methods (E.g., ``all objects in
this class have a color, a size, and can draw themselves''.)
- Almost every book has a title
- Almost every book has one or more authors
- Almost every book has text that you can extract
- Most object-oriented languages support inheritance, in which
classes can be based upon other classes. For example, we might say that
library books are a subclass of books and inherit all the
attributes of books. Library books also extend books.
- Since books have titles, library books also have titles
- Since books have authors, library books also have authors
- Library books also have ``loan records'': information on who has
the book.
- Some notes on subclasses:
- A subclass inherits the attributes and methods of its
parent class.
- The parent class is often called a superclass.
- Most object-oriented languages support polymorphism, in which
a member of a subclass can be used in place of a member of a superclass.
- If we know how to read a book, we know how to read a library book.
- Many object-oriented languages are event-driven. That is,
you can specify ``when this event happens, call this method
of this object''.
- A key aspect of object-orientation (and good program design in any
language) is information hiding. In general, objects
should know what other objects do, but not how they
do it.
- For example, you might know that a
Menu
object can
draw menus on the screen, but it's not important to you how it
does it.
- This property is also called encapsulation.
- Polymorphism, inheritance, and encapsulation are three key aspects
of object-oriented programming.
- Java is an object-oriented language developed by Sun Microsystems.
- It was ostensibly developed for building large programs.
- Many of these programs were to run on autonomous systems, like
toasters.
- With the popularization of the World Wide Web, it has also
become popular as a language for creating Web
applications.
- It is also a reasonably good introductory language (or so say
many computer science educators).
- Java looks a lot like C and C++, two popular languages. It has many
differences from the two.
- It is object-oriented (which C is not but C++ is)
- It has a different view of object-orientation than C++.
- It provides automatic memory management (yay!).
- We'll see some others as time goes on.
- Sun says that Java is ``a simple, object-oriented, distributed,
interpreted, robust, secure, architecture neutral,
portable, high-performance, multithreaded, and dynamic
language.''
- Simple. Compared to many modern languages, there is not
too much to the core of Java. However, a great
deal of functionality exists in the standard libraries,
and you will eventually need to learn many parts of those libraries.
In addition, the core is relatively large compared to many other
languages (as my co-authors and I have found as we tried to
focus on just the essentials). Java is also much larger than Scheme
or Pascal.
- Object-oriented. Java focuses on the creation of classes
and objects. Every Java program begins with a central
class or object.
- Distributed. Java permits you to place different portions
of a program on different servers, to segment
processing between servers, and so on and so forth.
- Interpreted. Although a compiler translates Java code
to another format (byte codes), an interpreter is
required to execute or further translate the byte
codes.
- Robust. Java provides a number of facilities for
ensuring that things don't go wrong or that,
when they do, the programmer provides ways to resole
problems.
- Secure. The Java interpreter will often limit what a
program can do (from accessing the file system to
reading memory outside of a valid range). We often
say that Java gives each program a sandbox
to play in. Note that our HP implementation is somewhat less
secure, and it is possible to crash it in some situations.
- Architecture neutral. A Java program will run
identically (except for speed) on every platform.
Unlike most languages, which can have a different implementation
on every platform, Java has specific requirements for
essentially every part of the language (from the
amount of storage used for various data types to the
algorithms used for numeric computation).
- Portable. See the sections above on architecture neutrality and
distributed programming.
- Multithreaded. A program can have separate threads of
computation, and it is relatively easy to manage
those threads.
- Dynamic. New objects can easily be created, and new
classes can be loaded as the program runs.
- Unfortunately, Java is also a moving target. There have
been at least two major changes to Java since it has been released.
- The current version of Java is 1.2 (also called the Java 2 platform).
- When last I checked, most Macintoshes ran only Java 1.1.
- Many versions of Netscape Navigator seem to run Java 1.0.
- When you put together a complete Java program, you'll use
a number of different classes. One class, which we might call
the driver (or conductor or director)
provides the
main
method that directs everything
else.
- The
main
method generally
- Creates objects
- Tells them to do things.
- Here is a simple Java program that prints a greeting message.
You will also find this program in lab J1. We'll talk about
the parts in class.
import SimpleOutput; // Load library
/** // Introductory comment
* Your first Java program.
*/ // End of introductory comment
public class FirstProgram // Class declaration
{ // Start body of class
public static void main(String[] args) // Declare main method
{ // Start body of main method
SimpleOutput output; // Declare variable
output = new SimpleOutput(); // Create object
output.println("That's it!"); // Call object method
} // main(String[] args) // End body of main method
} // class FirstProgram // End body of class
- Here, the main method creates one object,
output
,
and tells it to do one thing, print a string.
- As you may know, computers work with a number of different
``languages''.
- Each computer really understands only one language: its underlying
machine language.
- We make computers understand another language by either
- writing programs that translate the other language to
the computer's machine language (compilation)
- writing programs that read programs written in the other
language and then directly executing those programs
(interpretation)
- Java uses an interesting combination of compilation and
interpretation.
- You must first compile your Java program to Java Virtual Machine
(JVM) code.
- You then interpret the JVM code.
- To compile Java programs, write
- That is, run the program called
javac
(Java compiler).
- To interpret compiled Java programs, write
- That is, run the program called
java
(Java interpreter).