← Teach Yourself Java in 21 Days 1996

Teach Yourself Java in 21 Days 1996

From the originals

13 scans

Machine-transcribed text and image descriptions. Check the linked original for uncertain words or fine print.

These are transcribed pages from the documents, not the full publications. Open the original for the remaining pages.

Image 1

NATIONAL BESTSELLER! Laura Lemay and Charles L. Perkins LEARN the basics of Sun's new programming language DISCOVER how to create dynamic, interactive Web presentations with Java applets EXPLORE advanced Java concepts and techniques teach yourself JAVA™ in 21 days CD-ROM Java Developer's Kit v1.0 from Sun Microsystems Over 30 ready-to-use applets Sample source code Example Java-enhanced HTML Web pages sams.net

A purple book cover with a gray computer monitor showing a web page, a film reel, filmstrip, musical staff with green notes, and a CD-ROM disc, with gold banners and large gold and white title lettering.

Reading notes: The small text on the filmstrip and monitor screen is not legible and was not transcribed. Reading order of the three LEARN/DISCOVER/EXPLORE blocks is inferred from top-to-bottom layout. The 'sams.net' logo text is partly stylized and may be imprecise.

Printed product names
Java Developer's Kit v1.0 · Java
Printed dates
21
Languages
English
PDF page 1

NATIONAL BESTSELLER! Laura Lemay and Charles L. Perkins LEARN the basics of Sun's new programming language DISCOVER how to create dynamic, interactive Web presentations with Java applets EXPLORE advanced Java concepts and techniques teach yourself JAVA™ in 21 days CD-ROM - Java Developer's Kit v1.0 from Sun Microsystems - Over 30 ready-to-use applets - Sample source code - Example Java-enhanced HTML Web pages sams.net

A purple book cover with a gray computer monitor showing a web page, a film reel, a filmstrip, musical notes, a CD-ROM disc, and large gold and white title lettering.

Reading notes: The small printed text on the monitor screen and filmstrip is too small to transcribe and was omitted. The word 'NATIONAL BESTSELLER!' is in red letters on a gold banner at the top and is cropped at the left edge. The 'sams.net' logo is in the lower left corner and is partially cut off.

Printed product names
Java Developer's Kit v1.0
Languages
English
PDF page 2

An Introduction Object-Oriented Java Basics Working with Arrays, Condition- Creating Classes More About to Java Pro- Programming • Java statements Objects als, and Loops and Applications Methods gramming and Java and expressions • Testing and mod- • Conditional tests in Java • Overloading •Platform • Objects and • Variables and ifying instance •Iteration • Defining con- methods independence classes data types variables • Block statements stants, instance • Constructor •The Java com- • Encapsulation • Comparisons and • Converting and class variables, methods piler and the Java • Modularity logical operators objects and methods • Overriding interpreter methods Java Applet Basics Graphics, Fonts, Simple Anima- More Animation, Managing Simple The Java Abstract Wmdows, Net- • Including an ap- and Color tion and Threads Images, and Events and Inter- Wmdowing working, and pier on a Web •Graphics • paint ( ) and Sound activity Toolkit Other Tidbits page primitives repaint () • Scaling options, • MouseDown • Canvases, text • Programming • Passing para- • The Color class • Reducing anima- executing sound and MouseUp components, wid- menus and meters tion flicker effectively •The J;iva event gets, and window creating links • stop and start • Double-buffering handler construction inside applets components Modifiers Packages and Exceptions Multithreading Streams Native Methods Under the Hood •Method and Interfaces • Paper throw • Synchronization •Input and and Libraries •Java bytecodes variable access • Hiding Classes statements problems output, flush() •Built-in • Security and control • Design versus •Using the • More about Point and close() optimizations consistency implementation finally clause • Generating checking inheritance header and stub •Garbage files collection

Reading notes: Column-wise table of contents layout makes reading order across columns uncertain Some words hyphenated across line breaks (e.g., 'Pro-gramming', 'mod-ifying') may affect exact spelling

Languages
English
PDF page 3

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 D 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. D 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. D 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. D 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.

Reading notes: Source includes stray OCR-like characters such as 'D' bullets and '{' in the 'How This Book Is Structured' heading.

Languages
English
PDF page 4

Conventions ~ ;~ ./ \, • NE\V.- TERM 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. 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.

Reading notes: The source text is a conventions/legend page from a technical book; no advertised products, prices, or headline are present. The leading symbols (~ ;~ ./ \, •) and 'NE\V.-' appear to be damaged or stray characters of uncertain meaning.

Languages
English
PDF page 5

Teach Yourself JAVA in 21 Days

Teach Yourself JAVA in 21 Days

A narrow dark gray vertical panel with a thin black border on a white page carries white serif title text split across the panel, with 'Teach Yourself' and 'JAVA' above a horizontal white rule and 'in 21 Days' below it.

Reading notes: '21 Days' is a duration phrase in the title rather than a calendar date; listed under printedDates only because it is the visible numeric copy. No body copy, prices, or fine print are visible in the image.

Printed product names
JAVA
Printed dates
21 Days
Languages
English
PDF page 6

Teach Yourself JAVA in 21 Days

Teach Yourself JAVA in 21 Days Laura Lemay Charles L. Perkins sams net 201 West 103rd Street Indianapolis, Indiana 46290

Book cover with bold serif title between two horizontal rules, italic author names, a sams.net logo, and a faded calendar grid with day letters M T W T F S S in the background, with a gray band on the right edge.

Reading notes: Faint ghosted text near the top of the page and along the right gray band is not legible and was not transcribed. The day letters in the background calendar grid are partially obscured and were not transcribed as text.

Printed product names
JAVA
Languages
English
PDF page 7

vi To Eric, for all the usual reasons (moral support, stupid questions, comfort in dark times}. LL For RK]P, ARL, and NMH the three most important peopk in my life. CLP Copyright © 1996 by Sams.net Publishing and its licensors FIRST EDITION All rights reserved. No pan of this book shall be reproduced, stored in a reuieval 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 Interpretation of the printing code: the rightmost double-digit number is the year of the book's printing; the righunost single-digit, the number of the book's printing. For example, a printing code of96-1 shows that the first printing of the book occurred in 1996. Composed in A Garamond and MCPdigital by Macmillan Computer Publishing Printed in the United States ofAmerica 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: Publisher, Sams.net Publishing: Publishing Manager: Managing Editor: Marketing Manager: Richard K Swadley George Bond Mark Taber Cindy Morrow john Pierce 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 TeaID 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

Reading notes: Garbled OCR in dedication and copyright lines (e.g., 'RK]P, ARL, and NMH', 'reuieval', 'righunost') may affect exact spelling. Printing code example 'a printing code of96-1' appears as written in source; no other ambiguity noted.

Printed dates
1996 · 95-78866
Languages
English
PDF page 8

Overview Introduction XXl Week 1 at a Glance Day 1 An Introduction to Java Programming 3 2 Object-Oriented Programming and Java 19 3 Java Basics 4 Working with Objects 5 Arrays, Conditionals, and Loops 6 Creating Classes and Applications in Java 7 More About Methods Week2 Day 8 9 10 11 12 13 14 15 Modifiers 16 Packages and Interfaces 17 Exceptions 18 M ultithreading 19 Streams 20 Native Methods and Libraries 21 Under the Hood Appendixes A Language Summary B Class Hierarchy Diagrams c The Java Class Library D How Java Differs from C and C++ Index

Reading notes: Some characters in the table of contents appear spaced or irregular, possibly from OCR extraction.

Languages
English
PDF page 9

Contents Introduction xxi Week I at a Glance I 3 Day 1 An Introduction to Java Programming Day 2 Creating a Class .......... ............................................ ............ <.:' ..••..•...••. 24 Inheritance, Interfaces, and Packages ......... ..................... ,, .. '.".'.': .... .... ...... ... 28 Inheritance .. ....... .......... ................. ..... ........ ............ <. ......................... 29 Creating a Class Hierarchy..................... ..... ........................ ...... ......... 30 How Inheritance Works ...................... /.: .......... ............ .... ..... ........ .. 32 .... Single and Multiple Inheritance....... .... ....... ................................... .... 4 "" Interfaces and Packages ....... ................................................................ 34 Creating a Subclass ...... ............ ............ ................. ....................... C... ....... 35 Day 3 ~;:.~~.:::::::::::::::::::::::::::::::::::::::::::::::::::::. : : ::: \ :::::::?.~::::: :: ::: : ::: :: :: :::: ;~ !::=:~<> =d Exp,,«iom u :\ •••••••• !~ Variables and Data Types .... ................... ...... ......... ......... ......... ......... .. ...... 43 Declaring Variables ........................ .... ....... ......... ............. .. ................... 43 Notes on Variable Names ..... .......... ........ ................................ / ....... 4,4 ix

Reading notes: Table of contents with OCR noise such as stray punctuation and broken characters (e.g., '3 Day', '!::=:~<> =d Exp,,«iom'). Reading order of entries is uncertain due to scattered OCR fragments.

Languages
English
PDF page 10

Teach Yourself JAVA in 21 Days

Teach Yourself JAVA in 21 Days Variable Types ..................................................................... .' ............... 45 Assigning Values to Variables ...... .. ... ....... ............................................. 46 Comments ... ........ .................. ............. ............. .... .. .................. ................. 47 Literals ... .. ................................. .. ........................... .... ................. .............. 4 7 Number Literals ........................... ..... ............... ..... .. ...... ....................... 47 Boolean Literals .................................. ....... ...... .. .. ........... .... ..... ..... ....... 48 Character Literals ... .............................................. ....... ......................... 48 String Literals .................. ...................... ... ...... ..................................... 49 Expressions and Operators .................. ...................................................... 50 Arithmetic ........................ .................. .......... ............ ........................... 50 More About Assignment .................................... .................................. 52 Incrementing and Decrementing ................................................... ...... 52 Comparisons ......... ......................... ...................................................... 54 Logical Operators ... ........................... .................................................. 55 Bitwise Operators .......... .......................... .............. .. ............................ 55 Operator Precedence ...... ............ .......................................................... 56 String Arithmetic ...................................................................................... 57 Summary ........ ..... .. ......... ........ ... ......:......... .................... ........... ........ ...... .. 58 Q&A ........................................................................................................ 60 Day 4 Working with Objects 61 Creating New Objects ....................... ......... ..... ......................................... 62 Using new ... ... ... ............ ..... .... .... ... ............ .. ...... ......... ................. ......... 63 What new Does ... ............ ........... ........ ..... ...... .............. .... ................ .... 64 A Note on Memory Management ..................... .................. ................. 64 Accessing and Setting Class and Instance Variables ................................... 65 Getting Values .............................................. ....................................... 65 Changing Values ... ....... .......... ... .. ................. ....... ... .... ......................... . 65 Class Variables ................................................. .................................... 66 Calling Methods ............................... ... ....... .............................................. 67 Class Methods .............. ...... ...... ........ ...... .... ........ ............. .................... 69 References to Objects .................................. ... .......... ................................ 70 Casting and Converting Objects and Primitive Types ..... .... ..................... 71 Casting Primitive Types ..... ... .... .. ........................................... ............. 71 Casting Objects .................. ... ... ............ ..... ..... ......... ...... ... ....... ..... .. .... 72 Converting Primitive Types to Objects and Vice Versa ................................................................. 73 Odds and Ends ....... ........ ........... ............................. .... ........... ................... 73 Comparing Objects ... ..... ................... .............. ......... ...................... ..... 74 Copying Objects ..... ....................................................... ...................... 75 Determining the Class of an Object ..... ... ........ ..... ...... ........... ............... 76 The Java Class Libraries ..... .. ............. ....... ........... ...... ............. ....... ............ 76 Summary ..... ........ ...................... ............................................................... 77 Q&A ...... ..................................................... ............................................. 78 x

Reading notes: Table of contents page numbers and some heading spacing are irregular due to OCR artifacts.

Printed product names
Teach Yourself JAVA
Languages
English
PDF page 11

Day 5 Arrays, Conditionals, and Loops 79 Arrays .......................................... ............. ... .... ................... .... ......... ..... .... 80 Declaring Array Variables ....... .... ......... ....... .... .. ............ ................. ...... 80 Creating Array Objects .................... ................ ........ .......... ..... .. .. ..... .... 81 Accessing Array Elements ............................................ ........ ................. 81 Changing Array Elements ...... ......... ......... ....... ............ ...... .......... ......... 82 Multidimensional Arrays ... ........... .. .. ......... ................... .... .................... 83 Block Statements ...................................... ..... ......... ................ ........... ....... 83 if Conditionals ... ... ... ......... ........ ... ....... ...... .............. ......... ....... .......... ........ 83 The Conditional Operator ..... ............... .. ....................... ........... .. .... ..... 84 switch Conditionals ..... ..... ................ .... .... ....... ..... .................................... . 85 for Loops ...... ....... ................ ... .. ..... .......... ... .. ......................... .... .......... ..... 86 while and do Loops ........ ..... .. .. ..... ............................. .. ...................... ....... . 88 while Loops ..... ......... ...... ............ ....... ...... ......... .... ..... .............. ............. 88 do ... while Loops .. .. ......... ............. ........ .................. ................. .............. 89 Breaking Out of Loops .... ...... ..................................... .......... ......... ......... .. 89 Labeled Loops .. ....... ....... ...... .......... ................. ......................... ............ 90 Summary ......................................................................... ......... ..... ...... ..... 91 Q&A ...................... ................. .. ..... .. ................................. ....................... 92 Day 6 Creating Classes and Applications in Java 95 Defining Classes ................... ...... .. ................ ... ......................................... 96 Creating Instance and Class Variables ... .... .... ............. .... .... ....................... 96 Defining Instance Variables ....... ...... ...................... ............. ... ........... ... 97 Constants ..... .......... ............ ...... ...... ............ ... .............. ... ....... ............... 97 Class Variables ...... ...... ........... .................. ....... .. ............ ....................... 98 Creating Methods ..... ............. ....... ... ....... .. ..... ............ .. ....... .. .............. ...... 99 Defining Methods .. ... .............. ............... ............ ................................. 99 The this Keyword ..... .............. ........................ ........... ........ ......... ........ 101 Variable Scope and Method Definitions .. ......... ................................. 101 Passing Arguments to Methods .. ............... .............. .............. ............. 102 Class Methods . .. .. ... ... . ..... .. ... . ... .. ... .. .. .. .. ... .. .. .. ... .. .. ..... .. .. .. ... .. .. .. ..... .. . 104 Creating Java Applications .... ..... ....... ..... .... .............................. ............... 105 Java Applications and Command-Line Arguments ................. ...... .......... 106 Passing Arguments to Java Programs .... .............................................. 106 Handling Arguments in Your Java Program ...................... ........... ... ... 106 Summary ................. ...... ........... ................ ..... ...................................... ... 108 Q&A ............. .. ... ........ ................ ...... ...................................................... 109 Day 7 More About Methods 111 Creating Methods with the Same Name, Different Arguments ......... ...... 112 Constructor Methods ........ ....... .......... ............. .. .... ... ............ ................. . 115 Basic Constructors ....... .... ..... ..... ...... ... .......... .... ..... ..... ................ ....... 116 Calling Another Constructor ............ .............. ................................... 117 Overloading Constructors ........ .... ... .................. .................. ........ ... ... . 117 xi

Reading notes: Table of contents page numbers are dotted-leader text with some broken spacing; page numbers are recorded as printed.

Languages
English