Sun Microsystems, Inc.
MS BRM01-209
500 Eldorado Boulevard
Broomfield, Colorado 80021
U.S.A.
®
Java™ProgrammingLanguage
Revision D, April 2000
SL-275
StudentGuideWithInstructor Notes
Please
Recycle
Copyright 2000 Sun Microsystems, Inc., 901 San Antonio Road, California 94303 U.S.A. All rights reserved.
This product or document is protected by copyright and distributed under licenses restricting its use, copying,
distribution, and decompilation. No part of this product or document may be reproduced in any form by any means
without prior written authorization of Sun and its licensors, if any.
Parts of this product may be derived from Berkeley BSD systems, licensed from the University of California. UNIX is a
registered trademark in the U.S. and other countries, exclusively licensed through X/Open Company Ltd. Third-party
software, including font technology in this product, is protected by copyright and licensed from Sun’s suppliers.
RESTRICTED RIGHTS: Use, duplication, or disclosure by the U.S. Government is subject to restrictions of FAR 52.227-
14(g)(2)(6/87) and FAR 52.227-19(6/87), or DFAR 252.227-7015(b)(6/95) and DFAR 227.7202-1(a).
Sun, Sun Microsystems, the Sun logo, Solstice, Java, JavaBeans, JavaChip, Java HotSpot, JavaOS, JavaSoft, JDBC, JDK,
JVM, OpenWindows, Write Once, Run Anywhere and Solaris are trademarks or registered trademarks of Sun
Microsystems, Inc. in the U. S. and other countries.
UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open Company, Ltd.
All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc.
in the U. S. and other countries. Products bearing SPARC trademarks are based upon an architecture developed by Sun
Microsystems, Inc.
Netscape Navigator is a trademark of Netscape Communications Corporation.
The OPEN LOOK
®
and Sun™ Graphical User Interfaces were developed by Sun Microsystems, Inc. for its users and
licensees. Sun acknowledges the pioneering efforts of Xerox in researching and developing the concept of visual or
graphical user interfaces for the computer industry. Sun holds a non-exclusive license from Xerox to the Xerox Graphical
User Interface, which license also covers Sun’s licensees who implement OPEN LOOK GUIs and otherwise comply with
Sun’s written license agreements.
X Window System is a product of the X Consortium, Inc.
DOCUMENTATION IS PROVIDED “AS IS” AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS
AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
iii
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 2000, Revision D
Contents
About This Course.....................................................................................xix
Course Goal ....................................................................................... xix
Course Overview ............................................................................... xx
Course Map........................................................................................ xxi
Module-by-Module Overview ....................................................... xxii
Course Objectives........................................................................... xxvi
Skills Gained by Module.............................................................. xxvii
Guidelines for Module Pacing ................................................... xxviii
Topics Not Covered........................................................................ xxix
How Prepared Are You?................................................................. xxx
Introductions ................................................................................... xxxi
How to Use Course Materials ...................................................... xxxii
Course Icons and Typographical Conventions ....................... xxxiv
Course Icons.......................................................................... xxxiv
Typographical Conventions .................................................xxxv
Notes to the Instructor................................................................ xxxvii
Getting Started ...........................................................................................1-1
Objectives ........................................................................................... 1-1
Relevance............................................................................................ 1-2
Additional Resources ....................................................................... 1-3
What Is the Java Technology? ......................................................... 1-4
Primary Goals of the Java Technology ..................................1-5
A Basic Java Application.................................................................. 1-8
TestGreeting.java ................................................................1-8
Greeting.java.........................................................................1-8
TestGreeting Described ........................................................1-9
Greeting Described ...............................................................1-12
Compiling and Running TestGreeting.............................1-14
Troubleshooting the Compilation ........................................1-16
Java - Behind the Scenes................................................................. 1-18
The Java Runtime Environment............................................1-18
The Java Virtual Machine ......................................................1-19
iv Java Programming Language
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 2000, Revision D
Garbage Collection .................................................................1-22
Code Security...........................................................................1-24
Exercise: Performing Basic Tasks.................................................. 1-30
Preparation...............................................................................1-30
Tasks .........................................................................................1-30
Check Your Progress ...................................................................... 1-31
Think Beyond .................................................................................. 1-32
Object-Oriented Programming ...............................................................2-1
Objectives ........................................................................................... 2-1
Relevance............................................................................................ 2-2
What Is Object-Oriented Programming?....................................... 2-3
Analysis and Design.................................................................2-5
Analysis and Design Example ................................................2-7
Abstraction.................................................................................2-8
Classes as Blueprints for Objects ............................................2-9
Declaring Java Classes.................................................................... 2-10
Declaring Attributes ....................................................................... 2-11
Declaring Methods.......................................................................... 2-12
Accessing Object Members............................................................ 2-14
Information Hiding......................................................................... 2-15
The Problem.............................................................................2-15
The Solution.............................................................................2-16
Encapsulation .................................................................................. 2-17
Declaring Constructors .................................................................. 2-18
The Default Constructor ................................................................ 2-20
Source File Layout .......................................................................... 2-21
Software Packages .......................................................................... 2-23
The package Statement.................................................................. 2-24
The import Statement.................................................................... 2-26
Directory Layout and Packages .................................................... 2-28
Development ...........................................................................2-29
Deployment .............................................................................2-30
Terminology Recap......................................................................... 2-31
Using the Java Technology API Documentation........................ 2-32
Exercise: Using Objects and Classes............................................. 2-35
Preparation...............................................................................2-35
Tasks .........................................................................................2-35
Exercise Summary...................................................................2-36
Check Your Progress ...................................................................... 2-37
Think Beyond .................................................................................. 2-38
Identifiers, Keywords, and Types...........................................................3-1
Objectives ........................................................................................... 3-1
Relevance............................................................................................ 3-2
Comments .......................................................................................... 3-3
Semicolons, Blocks, and Whitespace.............................................. 3-4
v
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 2000, Revision D
Identifiers ........................................................................................... 3-7
Java Keywords................................................................................... 3-9
Basic Java Types .............................................................................. 3-10
Primitive Types .......................................................................3-10
Logical – boolean ...................................................................3-11
Textual – char and String ...................................................3-12
Integral – byte, short, int, and long.................................3-14
Floating Point – float and double......................................3-17
Variables, Declarations, and Assignments.................................. 3-19
Java Reference Types...................................................................... 3-20
Constructing and Initializing Objects .......................................... 3-21
Memory Allocation and Layout............................................3-22
Explicit Attribute Initialization .............................................3-23
Executing the Constructor .....................................................3-24
Variable Assignment ..............................................................3-25
This Is Not the Whole Story ..................................................3-26
Assignment of Reference Types.................................................... 3-27
Pass-by-Value .................................................................................. 3-29
The this Reference......................................................................... 3-32
Java Coding Conventions .............................................................. 3-35
Exercise: Using Objects .................................................................. 3-38
Preparation...............................................................................3-38
Tasks .........................................................................................3-38
Exercise Summary...................................................................3-39
Check Your Progress ...................................................................... 3-40
Think Beyond .................................................................................. 3-41
Expressions and Flow Control.................................................................4-1
Objectives ........................................................................................... 4-1
Relevance............................................................................................ 4-2
Expressions ........................................................................................ 4-3
Variables and Scope..................................................................4-3
Variable Scope Example...........................................................4-5
Variable Initialization...............................................................4-6
Operators....................................................................................4-7
Logical Operators......................................................................4-8
Short-Circuit Logical Operators..............................................4-9
Bitwise Logical Operators......................................................4-10
Right-Shift Operators >> and >>>........................................4-11
Left-Shift Operator (<<) .........................................................4-13
Shift Operator Examples........................................................4-14
String Concatenation With +.................................................4-15
Casting......................................................................................4-16
Promotion and Casting of Expressions................................4-18