Java.pdf

(5898 KB) Pobierz
Teach Yourself Java in 21 Days
abcd
Teach Yourself
JAVA
in 21 Days
Laura Lemay
Charles L. Perkins
201 West 103rd Street
Indianapolis, Indiana 46290
v
76131401.157.png 76131401.168.png 76131401.179.png 76131401.190.png 76131401.001.png 76131401.012.png 76131401.023.png 76131401.034.png 76131401.045.png 76131401.056.png 76131401.067.png 76131401.078.png 76131401.089.png 76131401.100.png 76131401.111.png 76131401.118.png 76131401.119.png 76131401.120.png 76131401.121.png
abcd
About This Book
This book teaches you all about the Java language and how to use it to create
applets and applications. By the time you get through with this book, you’ll know
enough about Java to do just about anything, inside an applet or out.
Who Should Read This Book
This book is intended for people with at least some basic programming back-
ground, which includes people with years of programming experience or people
with only a small amount of experience. If you understand what variables, loops,
and functions are, you’ll be just fine for this book. The sorts of people who might
want to read this book include you, if
n You’re a real whiz at HTML, understand CGI programming (in perl,
AppleScript, Visual Basic, or some other popular CGI language) pretty
well, and want to move on to the next level in Web page design.
n You had some Basic or Pascal in school and you have a basic grasp of
what programming is, but you’ve heard Java is easy to learn, really
powerful, and very cool.
n You’ve programmed C and C++ for many years, you’ve heard this Java
thing is becoming really popular and you’re wondering what all the fuss
is all about.
n You’ve heard that Java is really good for Web-based applets, and you’re
curious about how good it is for creating more general applications.
What if you know programming, but you don’t know object-oriented program-
ming? Fear not. This book assumes no background in object-oriented design. If
you know object-oriented programming, in fact, the first couple of days will be
easy for you.
How This Book Is Structured
This book is intended to be read and absorbed over the course of three weeks.
During each week, you’ll read seven chapters that present concepts related to the
Java language and the creation of applets and applications.
i
76131401.122.png 76131401.123.png 76131401.124.png 76131401.125.png 76131401.126.png 76131401.127.png 76131401.128.png 76131401.129.png 76131401.130.png 76131401.131.png 76131401.132.png 76131401.133.png 76131401.134.png 76131401.135.png 76131401.136.png 76131401.137.png 76131401.138.png 76131401.139.png 76131401.140.png 76131401.141.png 76131401.142.png 76131401.143.png 76131401.144.png
21
Teach Yourself JAVA in 21 Days
Conventions
Note: A Note box presents interesting pieces of information related to the surround-
ing discussion.
Technical Note: A Technical Note presents specific technical information related to
the surrounding discussion.
Tip: A Tip box offers advice or teaches an easier way to do something.
Caution: A Caution box alerts you to a possible problem and gives you advice to
avoid it.
Warning: A Warning box advises you about potential problems and helps you steer
clear of disaster.
TER *
New terms are introduced in New Term boxes, with the term in italics.
A type icon identifies some new HTML code that you can type in yourself.
An Output icon highlights what the same HTML code looks like when viewed by
either Netscape or Mosaic.
An analysis icon alerts you to the author’s line-by-line analysis.
Type
Output
Analysis
ii
NEW
76131401.145.png 76131401.146.png 76131401.147.png 76131401.148.png 76131401.149.png 76131401.150.png 76131401.151.png 76131401.152.png 76131401.153.png 76131401.154.png 76131401.155.png 76131401.156.png 76131401.158.png 76131401.159.png 76131401.160.png 76131401.161.png 76131401.162.png 76131401.163.png 76131401.164.png 76131401.165.png 76131401.166.png 76131401.167.png 76131401.169.png 76131401.170.png 76131401.171.png 76131401.172.png 76131401.173.png 76131401.174.png 76131401.175.png 76131401.176.png 76131401.177.png 76131401.178.png 76131401.180.png 76131401.181.png 76131401.182.png 76131401.183.png 76131401.184.png 76131401.185.png 76131401.186.png 76131401.187.png 76131401.188.png 76131401.189.png 76131401.191.png 76131401.192.png 76131401.193.png 76131401.194.png 76131401.195.png 76131401.196.png 76131401.197.png 76131401.198.png 76131401.199.png 76131401.200.png 76131401.002.png 76131401.003.png 76131401.004.png 76131401.005.png 76131401.006.png 76131401.007.png 76131401.008.png 76131401.009.png 76131401.010.png 76131401.011.png 76131401.013.png 76131401.014.png 76131401.015.png 76131401.016.png 76131401.017.png 76131401.018.png 76131401.019.png 76131401.020.png 76131401.021.png 76131401.022.png 76131401.024.png 76131401.025.png 76131401.026.png 76131401.027.png 76131401.028.png 76131401.029.png 76131401.030.png 76131401.031.png 76131401.032.png 76131401.033.png 76131401.035.png 76131401.036.png 76131401.037.png 76131401.038.png 76131401.039.png 76131401.040.png 76131401.041.png 76131401.042.png 76131401.043.png 76131401.044.png 76131401.046.png 76131401.047.png 76131401.048.png 76131401.049.png 76131401.050.png 76131401.051.png 76131401.052.png 76131401.053.png 76131401.054.png 76131401.055.png 76131401.057.png 76131401.058.png 76131401.059.png 76131401.060.png 76131401.061.png 76131401.062.png 76131401.063.png 76131401.064.png 76131401.065.png 76131401.066.png 76131401.068.png 76131401.069.png
21
Teach Yourself JAVA in 21 Days
To Eric, for all the usual reasons
(moral support, stupid questions, comfort in dark times).
LL
For RKJP, ARL, and NMH
the three most important people in my life.
CLP
Acquisitions Editor
Mark Taber
Development Editor
Fran Hatton
Software Development
Specialist
Merle Newlon
Production Editor
Nancy Albright
Technical Reviewer
Patrick Chan
Editorial Coordinator
Bill Whitmer
Technical Edit
Coordinator
Lynette Quinn
Formatter
Frank Sinclair
Editorial Assistant
Carol Ackerman
Cover Designer
Tim Amrhein
Book Designer
Alyssa Yesh
Production Team
Supervisor
Brad Chinn
Production
Michael Brumitt
Jason Hand
Cheryl Moore
Ayanna Lacey
Nancy Price
Bobbi Satterfield
Tim Taylor
Susan Van Ness
Mark Walchle
Todd Wente
Indexer
Tim Griffin
Copyright ©1996 by Sams.net
Publishing and its licensors
FIRST EDITION
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from
the publisher. No patent liability is assumed with respect to the use of the
information contained herein. Although every precaution has been taken in
the preparation of this book, the publisher and authors assume no
responsibility for errors or omissions. Neither is any liability assumed for
damages resulting from the use of the information contained herein. For
information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis,
IN 46290.
International Standard Book Number: 1-57521-030-4
Library of Congress Catalog Card Number: 95-78866
99 98 97 96 4 3 2 1
Interpretation of the printing code: the rightmost double-digit number is
the year of the book’s printing; the rightmost single-digit, the number of
the book’s printing. For example, a printing code of 96-1 shows that the
first printing of the book occurred in 1996.
Composed in AGaramond and MCPdigital by Macmillan Computer
Publishing
Printed in the United States of America
All terms mentioned in this book that are known to be trademarks or
service marks have been appropriately capitalized. Sams.net Publishing
cannot attest to the accuracy of this information. Use of a term in this book
should not be regarded as affecting the validity of any trademark or service
mark.
President, Sams Publishing: Richard K. Swadley
Publisher, Sams.net Publishing: George Bond
Publishing Manager: Mark Taber
Managing Editor: Cindy Morrow
Marketing Manager: John Pierce
vi
76131401.070.png 76131401.071.png 76131401.072.png 76131401.073.png 76131401.074.png 76131401.075.png 76131401.076.png 76131401.077.png 76131401.079.png 76131401.080.png 76131401.081.png 76131401.082.png 76131401.083.png 76131401.084.png 76131401.085.png 76131401.086.png 76131401.087.png 76131401.088.png 76131401.090.png 76131401.091.png 76131401.092.png 76131401.093.png 76131401.094.png
abcd
Overview
Introduction
xxi
Week 1 at a Glance
Day
An Introduction to Java Programming
3
2
Object-Oriented Programming and Java
19
3
Java Basics
41
4
Working with Objects
61
5
Arrays, Conditionals, and Loops
79
6
Creating Classes and Applications in Java
95
7
More About Methods
111
Week 2 at a Glance
Day
Java Applet Basics
129
9
Graphics, Fonts, and Color
149
10
Simple Animation and Threads
173
11
More Animation, Images, and Sound
195
12
Managing Simple Events and Interactivity
217
13
User Interfaces with the Java Abstract Windowing Toolkit
237
14
Windows, Networking, and Other Tidbits
279
Week 3 at a Glance
Day
Modifiers
305
16
Packages and Interfaces
323
17
Exceptions
341
18
Multithreading
353
19
Streams
375
20
Native Methods and Libraries
403
21
Under the Hood
421
Appendixes
A
Language Summary
473
B
The Java Class Library
483
C
How Java Differs from C and C++
497
D
How Java Differs from C and C++
507
Index
511
vii
1
8
15
76131401.095.png 76131401.096.png 76131401.097.png 76131401.098.png 76131401.099.png 76131401.101.png 76131401.102.png 76131401.103.png 76131401.104.png 76131401.105.png 76131401.106.png 76131401.107.png 76131401.108.png 76131401.109.png 76131401.110.png 76131401.112.png 76131401.113.png 76131401.114.png 76131401.115.png 76131401.116.png 76131401.117.png
Zgłoś jeśli naruszono regulamin