0% found this document useful (0 votes)
87 views

Java Training

training

Uploaded by

Ma Godfrey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Java Training

training

Uploaded by

Ma Godfrey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Ji

/.:
!

,i,t, *i,

{?, :'-*-;
'

'*,Jii!{'
.^

-.2'!;'

\S?;*""
.,G

'

' ;-:.. {r"*b

\ \

'

>

**,,
e-

*r*

'

,"

I,

\.,r

.
.

._,"1,1..

\-

\-*

Ir

!':.: ':4*;r'

J!'o
t'

sum
se$-wfittrffi
Ilus ic' Tt'uc,k [ .]A

fA

.tcc- 043-V020

,.
{

a
F

-a
I

--___________

l-

-fe

--)
)
)
)

)
)
)
)
)
)
)
)

h
!.

Basic Track

JAVA

Revision B
StudentGuide

dall,tffi
Sun Microsystems, [rc.
UBRMO5-104
500 Eldorado Blvd.
Broomfield, CO 8002L

u.s.e.
Revision B

Table of Contents
-

,-

Technology............
Objectives
Progress Check
Relevance
Additional Resources...............

Explaining JavarM

...1"-4

...1-5
Key Concepts of the Java Programming Language ........... ........... l-6
...................1,-7

Object-Oriented
Distributed ..............
............. 1-8
Simple
.................I-9
Multithreaded.........
........... 1-10
Secure......
..........1-10
Platform-Independent.............
........... 1-11
Technology
Product
Groups....
..........1-18
fava
java
Identifying
Technology Product Groups
.. 1-18

Choosing the Correctlava Technology Product Group.... t-L9


Using the Java 2 Platform, Standard Edition SDK
Components............
........7-20
Product Life Cycle (PLC) Stages
................L-22
Analysis Sta9e........
............7-23
Design Sta9e........
...............1-24
Development Stage
...........1-25
Testing Sta9e........
..............1-26
Implementation Stage........
.................L-27
Maintenance Stage
........... 1-28
End-of-Life (EOL) Stage
...1-29
Why Should You Follow the PLC?........
.............7-29

Started...
Objectives

Getting

Relevance

Additional

............1-1
................... 1-1
...........I-2

Resources...............
.............

What Is the JavarM Technology?

...............2-1
................... 2-7
') _)

...2-3
.....2-4

Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

Technology
Machine
Collection

Primary Goals of the Java

.......2-5
The Java Virtual
................... 2-6
Garbage
.............2-7
The Java Runtime Environment............ ................2-8
.............. 2-I0
JVMT"'
The Class
..........2-1,0
The Bytecode
.......... 2-1.1.
A Simple java
..2-1,2
.......... 2-13
The restcreeting Application
......... 2-15
The Greet.ing
Compiling and Running the Testcreeting Prograrn..... 2-76
Troubleshooting the
... 2-77

Tasks........
Loader.....
Verifier..............
Application......
..............
Class

Compilation
Object-Oriented Programming ..........
Objectives
Relevance
Software Engineering............
The Analysis and Design Phase
Analysis and Design Example
Abstraction...............
Classes as Blueprints for Objects
Declaring java Technology Classes....
Declaring Attributes
Declaring Methods..
Example
Accessing Object Members
Information Hiding..
Encapsulation ..........
Declaring Constructors ...........
The Default Constructor ..........
Source File Layout ...............
Software Packages
The package Statement............
The import Statement..............
Directory Layout and Packages...........
Development...........
Compiling Using the -d Option
Deployment .............
Terminology Recap.

.............3-1
................... 3-1
...3-2
......3-3
......... 3-5
........... 3-6
........... 3-7
........3-7
.........3-8
....................3-9
.3-10
........... 3-11
.......3-72
..................3-13
.3-15
..3-'1,6

..........3-77
......3-18

...3-I9
.3-20
.3-2I
......3-23
......... 3-24
.......3-24

..........3-25
.3-26
java
Technology API Documentation....... .................. 3-27
Using the

ldentifiers, Keywords, and

Types.....

Objectives
Relevance
Comments...............

Vf

JavarM Programming Language


Copyright2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

..............4-1
................... 4-7
...4-2
....4-3

Identifiers

=
=
-

Space

-........4-4
....................4-6
...4-7
Java Programming Language
......-.-...4-8
BasicJavaProgrammingLanguageTypes.......
..................-4-8
Primitive
...............4-8
Logical .-.4-9
Textual ..........-.-...4-9
Textual ......4-L0
Integral - byte, short, int, and
.-..4-17
Floating Point - f loat and
Variables, Declarations, and Assignments............ ...-.4-72
.4-I3
Java Reference
.......4-I4
Constructing and Initializing
4-15
.........
Memory Allocation and
................4-15
Explicit Attribute
.............-...4-1'6
Excuting the
.........4-16
Assigning a
..............4-1,6
This Is Not the Whole
.4-17
Assigning

Semicolons, Blocks, and White

Keywords

Types
boolean
char
string.

1ong.........
doub1e.......'..'.....

Types................
Objects
Layout..'........'....
Initialization...........
Constructor
Variable
Story
References................
Pass-by-Va1ue.........
The this Reference

fava Programming Language Coding Conventions

..4-20

............. ...... 4-21,

Obiects

............5-1
................... 5-1
Objectives
......-....5-2
Progress Check
...5-4
Relvance
...5-5
Resources...............
Additional
Declaring Object References, Instantiating Objects, and Initializing
.......5-6
Object References...............
..................5-8
Declaring Object Reference Variables
.......5-8
an
Object
Instantiating
...............5-9
Initializing Object Reference Variables.
Data. 5-l-0
to
Manipulate
Variable
Reference
an
Object
Using
Storing Object Reference Variables in Memory.................. 5-1 1
Assigning a Reference From One Variable to Another ....-5-12
Exercise 1: Using the ObjectTool to Create and Manipulate Objects
5-r4
Task - Instantiating Objects With the ObjectTool......... .....5-1'4
..5-15
Task - Initializing Object Instances
.....5-16
Task - Manipulating Object Data
....
to
Another
From
One
Object
Reference
a
Task - Assigning
5-76
....5-17
Exercise 1: Summary..............
................5-18
Exercise 2: Creating a Test Class
Another
Class...'..5-18
to
Test
Test
Class
a
Task - Developing
...'5-I9
Exercise 2: Summary..............
........5-20
Using the String Class.....
Creating a String Object With the newKeyword ..........".5-20

Creating and Using

..4-I8

Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

.t

Creating a String Object Without the new Keyword .......5-20


Storing Strins Objects in Memory............... .....5-27
Using Reference Variables for String Objects.... ............... 5-22
Exercise 3: Using the String Class
...........5-23
Task - Creating and Manipulating String Objects...........5-23
Exercise 3: Summary..............
....5-24
Exercise 4: Examining String Objects With the ObjectTool.....5-25
Task - Viewing the Contents of a String Object...............5-25
....5-26
Exercise 4: Summary..............
.......5-27
Investigating the Java Class Libraries
Java Class Library Specification............. .............5-27
Using the Java Class Library Specification to Learn About a

Method....

.......5-29
Exercise 5: Using the Class Llbrary Specification........................5-31
Task - Examining the I2SETN Class Library Specification 5-31
....5-32
Exercise 5:

Summary..............
.......6-1
Expressions and Flow Control
................... 6-1
Objectives
...6-2
Relevance
Variables
....6-3
Variables and Scope
............6-3
Variable Initialization.............
............. 6-6
Initialization Before Use Principle........... ............ 6-7
Operators
...6-8
Operator Precedence ...............
.............6-8
Logical Operators
............... 6-9
Bitwise Logical Operators
..................6-10
Right-Shift Operators >> and
Left-Shift Operator
Shift Operator Examples..........
String Concatenation With +...............

Casting

..........6-12
............... 6-73
..............6-73
.......6-74

Promotion and Casting of Expressions.......


Branching Statements...............
.6-75
Simple if, else Statements................ .................6-15
Complex if, else Statements................ .............6-15
The switch Statement.............
.......... 6-77
Looping Statements...............
....6-79
The for Loops
.6-19

Loop........
Loop
Control
Developing and Using Methods
Objectives
Progress Check
The while
The do/while
Special Loop Flow

viii

JavarM Programming Language


Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

_,,il)tl

.........6-20
..........6-20
................6-27

.....7-1
................... 7-7
...........7-2

Relevance

...7-4
.....7-5
.......-.7-6
.-.7-7
......-.-.7-8
-..-.....7-1'I
............-...7-L2

Creating and Invoking Methods.............


Basic Form of a Method ..............
Invoking a Method From a Different Class......
Invoking a Method in the Same C1ass........
Guidelines for Invoking Methods .............
Passing Arguments and Returning Values
Declaring Methods With Arguments.............. ...7-t3
Invoking Methods With Arguments.'....'.'..... ....7-14
..-.-. 7-76
Declaring Methods With Return Values.....
.............7-16
Returning a Value
.....-........ 7-I7
Receiving Return Values.....
...............7-L8
Advantages of Method Use
....-7-19
Exercise 1: Using Arguments and Return Values
Return
Task - Writing a Class That Uses Arguments and

............7-19
Values
....7-21,
Exercise 1: Summary..............
Creating static Methods and Variables......'... .........7-22
...............7-22
Declaring static Methods
...............7-22
Invoking static Methods..
..............7-24
Declaring static Variables
..............7-24
Accessing static Variables

-l
t

J
,

Static Methods and Variables in the fava API ................... 7-25


When to Declare a static Method or Variable............... -.7-26
...........7-28
Using Method
.. 7-30
Method Overloading and the Java API
Uses for Method Overloading............. ................7-3I
Exercise 2: Using Overloaded Methods................ ......7-34
Task - Developing a Class With an Overloaded Method .7-34
....7-35
Exercise 2:

Over1oading.............
..........

Summary..............
..........8-1
lmplementing Encapsulation and Constructors..
8-1
...................
Objectives
...........8-2
Progress Check
...8-3
Relevance
..........8-4
Using Encapsulation..........
.............8-4
Visibility Modifiers
............8-5
The public Modifier................
private
............8-7
Modifier..............
The
...........8-10
Interface and Implementation
..............8-11
............
Methods
and
Set
Get
..........8-1'6
Encapsulated E1evator..............
...8-27
Classes...............
Encapsulated
1:
Writing
Exercise
.............8-21'
Task - Creating an Encapsulated Class
..8-23
Encapsulated
Class.
Another
Task - Creating
....8-24
Exercise 1: Summary..............
........8-25
Scope
Describing Variable
lx
Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

F,i',,,r;,,ri,l

!i, r\i,',-<

( )i-f-i,'-.:

How Instance Variables and Local Variables Appear in

Memory
Creating Constructors...........
Default Constructor .............
.Overloading Constructors
Exercise 2: Using Constructors............

.........8-26
....8-28

............... 8-30
................8-32
.......8-35
Task - Creating and Using Constructors............................. 8-35
Task - Creating Constructors to Initialize Objects............ 8-36
Exercise 2:
....8-38

Summary..............

Arrays....
Objectives
Relevance
Declaring Arrays
Creating Arrays
Creating Reference

............9-1

Arrays.....

Initializing Arrays
Multidimensional Arrays.....
Array Bounds
Using the Enhanced
Array Resizing..
Copying Arrays

for

Loop

Design
Objectives
Relevance
Subclassing...............
Single Inheritance................
Access Control
Overriding Methods...............

Class

................... 9-7
...9-2
........9-3
..........9-4
..............9-5
......9-6
.......9-7
.............9-8
............9-8
..........9-9
........9-70

..10-1
................. 10-1

.10-2
10-3
................ 10-4
..........10-6
.

...10-7
Overuidden Methods Cannot Be Less Accessible............... 10-B
Invoking Overridden Methods............... ........... 10-9
Polymorphism
........10-10
Virtual Method Invocation
10-10
Heterogeneous Collections.......
.......10-11
Polymorphic Arguments .........
........70-72
The instanceof Operator.
.....10-13
Casting Objects
10-74
Overloading Methods .............
10-15
Methods Using Variable Arguments.............. .1.0-16
Overloading Constructors
......10-77
Constructors Are Not Inherited..........
10-18
Invoking Parent Class Constructors............ .....10-18
Constructing and Initializing Objects: A Slight Reprise........... 10-20
Constructor and Initialization Examp1es............. 70-20

JavarM Programming Language


Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

l-.
;

Jt,.'
,t
I

t
I

-t
t

The Object Class


The equals Method.
The toSrrins Method ..............
Wrapper Classes....
Autoboxing of Primitive Types

...L0-22
.......10-22
.......70-Zs
.L0-26
.......1.0-27

Features.
.............11-1
Objectives
................. 11-1
Relevance
.I1,-2
The static Keyword...............
.11-3
Class Attributes
.................11-3
Class Methods
.71-4
Static Initializers...............
.71-6
The final Keyword
..................11-8
Final Classes....
.11-8
Final Methods
..11-8
Final Variables
.11-8
Enumerated Types
.11-10
Old-Style Enumerated Type Idiom
11-11
The New Enumerated Type
77-74
Advanced Enumerated Types.
....... lI-I7
Static Imports...
.......-.........
.......71-Ig
Abstract Classes.....
.17-20
The Prob1em............
II-22
The So1ution.............
........1.L-22
Interfaces
1l-24
The Flyer Example
17-25
Multiple Interface Example
71-29
Uses of Interfaces
11-30
Exceptions and Assertions.............
..............12-1
Objectives
................. I2-I
Relevance
.72-z
Exceptions and Assertions................
..........12-g
Exceptions ...............
..I2-4
Exception Example
...........72-s
The try-catch Statement
........12-6
Call Stack Mechanism.............
...........72-g
The finallyClause
.........I2-g
Exception Categories...............
12-70
Common Exceptions................
72-12
The Handle or Declare Rule
...I2-I3
Method Overriding and Exceptions................
lZ-Is
Creating Your Own Exceptions...............
72-16
Throwing a User-Defined Exception
72-77
Handling a User-Defined Exception
I2-Ig
4ssertions................
12-Ig
Advanced Glass

Recommended Uses of

Assertions...............

....L2-19
XI

--

Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

Controlling Runtime Evaluation of Assertions

Applicatons
Objectives
Relevance

Text-Based

........... ..... 12-22

...............13-1
................. 13-1
.1,3-2
......... 13-3

..............
.....73-4
Properties
..............13-5
Class
....73-7
l/O..........
..............13-7
Output
..........13-8
Input
13-10
Output....
13-11
Input.......
...L3-12
I/O
13-72
Object
13-13
Utilities............
......13-1,4
I/O..........
.......13-18
API..........
t3-20
Example..
13-21,
A List Example
13-21'
Collections in fDKrM Version 1.1 ............
.13-23
Generics
........13-25
Compiler Warnings
73-26
Iterators...
Enhanced for Loop
........73-27
Building Java GUls
........14-1
................. 1,4-I
Objectives
Relevance
.1,4-2
Abstract Window Toolkit
.........I4-3
The java.awt Packa9e...........
...I4-4
Building Graphical User Interfaces
...........14-5
Containers ...............
........... 14-5
Positioning Components..........
..........1.4-5
Sizing Components............
.................1,4-6
Frames.....
.1,4-7
Panels
.......I4-9
1.4-1.'1.
Container Layouts...
Layout Managers
74-11.
Default Layout Managers
74-tz
A Simple Flowlayout Examp1e..............
14-13
Command-Line Arguments

System
The eroperties
Console
Writing to Standard
Reading From Standard
Simple Formatted
Simple Formatted
Files and File
Creating a New File
The File Tests and
File Stream
The Collections
A Set

xil

JavarM Programming Language


Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

t
t

Layout Managers
The FlowlayouL Manager
The Borderlayout Manager..
The Gridlayout Manager
Creating Paneis and Complex Layouts ..............
Drawing in AWT...

...L4-16
14-'l'6
L4-1,8

L4-22
......L4-25
.14-27

...........15-1
Hand1in9.............
................. 15-1
Objectives
.L5-2
Relevance
........15-3
What Is an Event? ..............
.................15_3
Event Sources....
.................15_3
Event Handlers.
.I5-4
J2SErr' Event Model
.........'.15-4
Delegation Model...
.........15-5
A Listener Example..
..I5-7
GUI Behavior..........
..................1'5-7
Event Categories ...............
.......... 15-9
Complex Example..
1.5-L2
Multiple Listeners
..15-13
Developing Event Listeners..
15-13
Event Adapters.
1.5-14
Event Handling Using Inner Classes
...15-15
Event Handling Using Anonymous Classes
..........16-1
GUI-Based Applications.......
................. 16-1
Objectives
.16-2
Relevance
.........16-3
AWT Components............
................. 16-4
AWT Listeners.
..............1'6-5
How to Create a Menu
.......16-5
Help Menu
.......... L6-6
Creating a MenuBar
................ 16-8
Creating a Menu
.........76-9
Creating a Menurtem
.......76-1,0
a
Creating Checldco>Ienultem
..76-12
Controlling Visual Aspects...
........16-12
Colors......
.t6-I3
f.F.C./Swing Techno1ogy.......
..........17-1
Threads..
................. I7-l
Objectives
.I7-2
Relevance
....L7-3
Threads
..........17-4
Creating the Thread
...........L7-5
Starting the Thread
...............77-6
Thread Schedu1in9...............
..........77-8
Terminating a Thread...............
17-10
Basic Control of Threads................

GUI Event

-I
--
-
--
-
-
--

xilt
Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

Testing Threads
Accessing Thread Priority
Putting Threads on Hold
Other Ways to Create Threads...
Selecting a Way to Create Threads

17-10
17-70
17-10
17-73
17-74
........17-15
17-15
17-77
L7-79
Putting It Together ................ ..... 17 -20

synchronizedKeyword
The Prob1em............
The Object Lock F1ag.........
Releasing the Lock F1ag..........

Usingthe

Using synchroni zed


Thread States

Deadlock.

.17-27
........17-21.
......17-22
17-22
17-22
........L7-22

- wait and notify...............


Scenario
The Prob1em............
The So1ution.............
Thread Interaction...............
.....17-23
The wait and notifyMethods ...............
17-23
Thread States
.17-24
Monitor Model for Synchronization
L7-25
Putting It Together................
'.17-26
The Producer Thread ..............
....... 77-27
The Consumer Thread
....17-28
The S1mcStackClass........
17-29
The SlmcTest Examp1e............
........17-33
Networking .............
........18-1
Objectives
................. 18-1
Relevance
.18-2
Networking.............
..18-3
Sockets
..............18-3
Setting Up the Connection...............
..18-3
Thread Interaction

Addressing the Connection............... ..................18-5


Port Numbers.........
...........18-5
...............18-6
]ava Networking Model......
Minimal TCP/IP Server
............18-7
Minimal TCP/IP Client.......
......18-8

XIV

JavarM Programming Language


Copyright 2005 Sun Microsystems, lnc. All Rights Reserved. Sun Services, Revision F

You might also like