Held Tuesday, January 25, 2000
Overview
Today, we begin to consider the Java language with a high-level
overview of the language.
Notes
- Not all of you seem to have filled in the
introductory survey. Please
do so as soon as possible.
- Initial responses
are now available.
- No, I am not really a mean grader. I just have high
standards. One of my standards is that ``correct is
satistfactory and satisfactory deserves a B''. Students still
find ways to get As in my classes. (Last semester, only one student
earned a C in CS152 and no one earned below a C. Soemthing like 9
students earned A's or A-'s.)
- Yes, it will be a lot of work. Let me know when it gets overwhelming.
- If you feel you've missed something, let me know. Someone else has
probably missed it too, and I'd be happy to go back and go over it.
- I forgot to mention yesterday that I'm holding a meeting on
summer opportunities in computer science at noon on Thursday in
Science 2424 (down the hall). There is a good chance that I will
take at least one first year student from this class (assuming,
of course, that some of you apply and indicate an interest in
majoring in computer science).
- Assignments:
Contents
Summary
- Introduction to data structures
- Introduction to object-oriented programming
- An introduction to Java
- Java vs. Scheme
- Due:
- Handouts:
- This semester, we'll talk about data structures and
abstract data types. Many computer scientists treat
them as equivalent terms.
- An abstract data type (ADT) is a collection of values and
operations on those values. ADTs specify the what of data.
- A data structure is a structure designed to organize data.
- When we distinguish the two, we sometimes say that data structures
implement abstract data types.
- Those of you coming from the Scheme-based 151 may already be familiar
with two basic ADTs: the list and the vector.
- Both lists and vectors gather data into a sequence.
- More importantly, they provide facilities for manipulating the
sequence.
- You can extract an element from the sequence.
- You can change an element in the sequence.
- (Sometimes) you can insert or remove an element from the
sequence.
- Vectors and lists differ in the operations they provide and the
costs associated with each operation.
- In designing and building your own ADTs, you will be concerned with
- The specification -- the description of the data structure
and the operations it provides.
- The implementation -- how you actually provide those
operations (and how you store the data to provide those
operations).
- The algorithms used in the implementation.
- The efficiency of your implementation -- how much it costs
to provide those operations.
- The applications of the data structure.
- Note that we want a clear barrier between specification and implementation,
so that a client of one of your data structures need only know
what you do and not how you do it. We often call this separation
encapsulation or information hiding.
- This term, we will be looking at each of these aspects of a number of
the key data structures in CS.
- 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
- currently has title ``Java Plus Data Structures'' (attribute)
- is under development (attribute)
- is or will be written by Sam Rebelsky, Chip Weems, and Nell Dale (attribute)
- permits you to search it, at least in the electronic form (method)
- 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.
- Java is an object-oriented language developed by Sun Microsystems.
- It was ostensibly developed for developing 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.
- 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).
- 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 for
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 ``Java 2'').
- The HPs in the MathLAN (and most Macintoshes) still run 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
% /home/rebelsky/bin/jc file.java
- That is, run the program called
jc (Java compiler)
that is in my bin directory.
- To interpret compiled Java programs, write
% /home/rebelsky/bin/ji file.class
- That is, run the program called
ji (Java interpreter)
that is in my bin directory.
Monday, 17 January 2000
- Created as a blank outline.
Tuesday, 18 January 2000
- Added section on ``Compiling vs. Interpereting'' from
outline 3
of CS152 99F.
- Added sections on object-oriented programming and an overview
of Java from
outline 2
of CS152 99F.
- Some small updates to those sections.
- Added charts comparing Scheme and Java (new).
- Added sample Java programs (new).
Monday, 24 January 2000
- Added section on data structures.
Tuesday, 25 January 2000
- Added introductory notes.
Wednesday, 26 January 2000
- Moved selected sections to subsequent outlines.
Back to Introduction to the Course.
On to Lab: Getting Started with Java.