Introduction to Java


(来源:http://www.artima.com)

Objects and Java Seminar by Bill Venners
Lecture Handout

Agenda

  • Describe this course
  • Talk about Java
  • Talk about OOP
  • Touch on each fundamental topic
  • Look at the Java API
  • Talk about Java compiler and virtual machine

About this Seminar

  • Assume you are familiar with C syntax
  • Course roughly follows presentation order of:
    Bruce Eckel's Thinking in Java
  • Focused on JDK 1.3, some 1.0 through 1.2
  • Three lectures per day
  • Following each lecture: guided lab exercises

Seminar Overview

 
1. Introduction to Java
2. Classes and Objects
3. Initialization and Cleanup

4. Packages and Access Specifiers
5. Composition and Inheritance
6. Polymorphism and Interfaces

7. Collections and Inner Classes
8. Exceptions
9. Threads
 
10. Applets and Graphics
11. AWT and Swing Components
12. Events and GUIs

13. I/O and Object Serialization
14. Network Programming
15. Remote Method Invocation (RMI)

Why Java?

  • It all started with a hockey game
  • Better hardware, bigger software, network
  • VM: memory safety, security, portability
  • Developers get better productivity
  • Java is: language, VM, class file, API, "execution environment"

Major Features

  • Is very object-oriented
  • Supports multi-threading
  • Enforces type safety at run-time
  • Uses garbage collection
  • Supports exceptions
  • Is dynamically linked
  • Is dynamically extensible

Java is Object-Oriented

  • Object: chunk of memory and set of services
  • Objects send each other "messages"
  • Objects have class
  • Objects of the same class accept the same messages
  • Java application: objects in one VM interacting by sending messages

Abstraction

  • OOP: a higher level of abstraction
  • Bjarne Stroustrup: Higher level of abstraction allows details to be hidden, which allows code to be shorter. The more details, the more mistakes. Plus, the more code you have, the harder it is to maintain. And, code at higher levels of abstraction is amenable to tools that analyze and optimize.
  • Can think in terms of problem, not solution
  • Vocabulary facilitates communication: A bank has Accounts, Customers, Balances, etc...

Encapsulation


Objects Expose an Interface

Interface: the messages an object will accept



Creating an Object: Account acct = new Account();
Sending a Message: acct.deposit(15000);


Interface vs. Implementation

  • Implementation: state and behavior
  • Want to separate them
  • Access Levels: private, package, protected, public
  • Designers "hide the implementation" from clients
  • Allows designers to:
    1. Keep control of internal data
    2. Change implementation without breaking code

Composition

Composition: enlisting the help of other objects


Inheritance



Subclass is-a superclass


Subclass accepts same messages as superclass


Polymorphism

Code can treat subclasses as if they are superclasses:

1 void makeMoney() {
2     Account a = new Account();
3     OverdraftAccount oa = new OverdraftAccount(50000);
4     makeDeposit(a, 100);
5     makeDeposit(oa, 200);    // "upcasting"
6 }
7 void makeDeposit(Account account, int amount) {
8     account.deposit(amount); // "dynamic binding"
9 }

Java Applications

Objects in one VM sending messages via interfaces

The Lifetime of an Object

  • All objects reside on the heap
  • Can create an object with new
  • new returns a "reference" to the object
  • Garbage collector recycles memory occupied by "unreachable" objects

Exceptions

  • Error handling that's "structured"
  • Like a "goto" from error to handler
  • Built into the Java language
  • Java compiler forces client programmers to deal with potential errors

Multi-threading

  • Multi-threading built into Java
  • Like multiple processes in same address space
  • Synchronization built into Java
  • Helps you do several things at once:
    responsive UIs, animations, server apps, ...
  • Designed to work cross-platform

Java APIs

  • Sun's goal: abstract OS interface
  • I/O: datat to/from files, strings, sockets, ...
  • GUI: AWT, 1.1 event model, Swing
  • Software Components: JavaBeans
  • Net programming: Applets, java.net, RMI
  • Accessing SQL databases: JDBC

Compiling and Running


Linking, Optimization, and Execution