Class 02: Introduction to Java
Held Tuesday, January 26
- Today is primarily a lab day. After a short discussion of Java
and object-oriented programming, we'll dig right in to the lab.
- Thanks to those of you who filled out the
- Those of you who didn't need to do so ASAP.
- If you care about my answers to your questions, you can find them
- As you might guess, object-oriented languages concern themselves with
- 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
- is currently 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
- Some notes on subclasses:
- A subclass inherits the attributes and methods of its
- 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
- For example, you might know that a
Menu object can
draw menus on the screen, but it's not important to you how 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
- With the popularization of the World-Wide Web, it has also
become popular as a language for creating web
- 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
- 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 those libraries.
- 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
- Robust. Java provides a number of facilities for
ensuring that things don't go wrong or that,
when they do, the programmer provides for
- 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.
- 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 neutral and
- Multithreaded. A program can have separate threads of
computation, and it is relatively easy to manage
- 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.
- Created Monday, January 11, 1999
- Filled in some details on Monday, January 18, 1999. Those details
were primarily based on materials I had written for a prior session
- Added the section on object-oriented programming on Tuesday, January