sams teach yourself java 6 in 21 days 5th edition

721 2K 0
sams teach yourself java 6 in 21 days 5th edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info in 21 Days Java6 Sams Teach Yourself Rogers Cadenhead Laura Lemay 800 East 96th Street, Indianapolis, Indiana 46240 www.it-ebooks.info Sams Teach Yourself Java6 in 21 Days Copyright © 2007 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmit- ted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written per- mission 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 pub- lisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number-10: 0-672-32943-3 International Standard Book Number-13: 978-0-672-32943-2 Printed in the United States of America First Printing: May 2007 10090807 4321 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appro- priately capitalized. Sams 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. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no war- ranty or fitness is implied. The information provided is on an “as is” basis. The author(s) and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the CD or pro- grams accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearsoned.com Library of Congress Cataloging-in-Publication Data Cadenhead, Rogers. Sams teach yourself Java 6 in 21 days / Rogers Cadenhead, Laura Lemay. p. cm. Includes index. ISBN 978-0-672-32943-2 (pbk. w/cd) 1. Java (Computer program language) I. Lemay, Laura. II. Title. QA76.73.J38C333 2007 005.13'3 dc22 2007014568 Acquisitions Editor Mark Taber Development Editor Songlin Qiu Managing Editor Patrick Kanouse Project Editor Mandie Frank Copy Editor Karen Annett Indexer WordWise Publishing Services, LLC Proofreader Language Logistics, LLC Technical Editor Adam DeFields Publishing Coordinator Vanessa Evans Multimedia Developer Dan Scherf Designer Gary Adair Page Layout Bronkella Publishing, LLC www.it-ebooks.info Contents at a Glance Introduction 1 WEEK I: The Java Language 1 Getting Started with Java 9 2 The ABCs of Programming 35 3 Working with Objects 63 4 Lists, Logic, and Loops 89 5 Creating Classes and Methods 115 6 Packages, Interfaces, and Other Class Features 145 7 Exceptions, Assertions, and Threads 183 WEEK II: The Java Class Library 8 Data Structures 219 9 Working with Swing 247 10 Building a Swing Interface 273 11 Arranging Components on a User Interface 303 12 Responding to User Input 333 13 Using Color, Fonts, and Graphics 357 14 Developing Swing Applications 381 WEEK III: Java Programming 15 Working with Input and Output 405 16 Serializing and Examining Objects 433 17 Communicating Across the Internet 453 18 Accessing Databases with JDBC 485 19 Reading and Writing RSS Feeds 511 20 XML Web Services 535 21 Writing Java Servlets and Java Server Pages 555 Appendixes A Using the Java Development Kit 601 B Programming with the Java Development Kit 625 C This Book’s Web Site 649 Index 651 Bonus Material on the Companion CD-ROM CD1 Choosing Java CD2 Writing Java Applets CD3 Regular Expressions CD4 Where to Go from Here: Java Resources www.it-ebooks.info Table of Contents Introduction 1 How This Book Is Organized 2 Who Should Read This Book 4 Conventions Used in This Book 5 WEEK I: The Java Language DAY 1: Getting Started with Java 9 The Java Language 10 History of the Language 10 Introduction to Java 11 Selecting a Development Tool 11 Object-Oriented Programming 12 Objects and Classes 14 Attributes and Behavior 17 Attributes of a Class of Objects 17 Behavior of a Class of Objects 18 Creating a Class 19 Running the Program 21 Organizing Classes and Class Behavior 24 Inheritance 24 Creating a Class Hierarchy 26 Inheritance in Action 28 Single and Multiple Inheritance 29 Interfaces 30 Packages 30 Summary 31 Q&A 31 Quiz 32 Questions 32 Answers 32 Certification Practice 32 Exercises 33 www.it-ebooks.info DAY 2: The ABCs of Programming 35 Statements and Expressions 36 Variables and Data Types 36 Creating Variables 37 Naming Variables 39 Variable Types 39 Assigning Values to Variables 41 Constants 41 Comments 43 Literals 44 Number Literals 45 Boolean Literals 46 Character Literals 46 String Literals 47 Expressions and Operators 48 Arithmetic 49 More About Assignment 51 Incrementing and Decrementing 52 Comparisons 53 Logical Operators 54 Operator Precedence 55 String Arithmetic 57 Summary 58 Q&A 59 Quiz 60 Questions 60 Answers 60 Certification Practice 60 Exercises 61 DAY 3: Working with Objects 63 Creating New Objects 64 Using new 64 What new Does 66 A Note on Memory Management 66 www.it-ebooks.info Accessing and Setting Class and Instance Variables 67 Getting Values 67 Changing Values 68 Class Variables 69 Calling Methods 70 Nesting Method Calls 72 Class Methods 73 References to Objects 74 Casting and Converting Objects and Primitive Types 76 Casting Primitive Types 77 Casting Objects 78 Converting Primitive Types to Objects and Vice Versa 80 Comparing Object Values and Classes 82 Comparing Objects 82 Determining the Class of an Object 84 Summary 84 Q&A 85 Quiz 86 Questions 86 Answers 86 Certification Practice 86 Exercises 87 DAY 4: Lists, Logic, and Loops 89 Arrays 90 Declaring Array Variables 90 Creating Array Objects 91 Accessing Array Elements 92 Changing Array Elements 93 Multidimensional Arrays 95 Block Statements 96 if Conditionals 97 switch Conditionals 98 for Loops 104 vi Sams Teach Yourself Java 6 in 21 Days www.it-ebooks.info while and do Loops 107 while Loops 107 do-while Loops 109 Breaking Out of Loops 109 Labeled Loops 110 The Conditional Operator 110 Summary 112 Q&A 112 Quiz 112 Questions 112 Answers 113 Certification Practice 113 Exercises 114 DAY 5: Creating Classes and Methods 115 Defining Classes 116 Creating Instance and Class Variables 116 Defining Instance Variables 116 Class Variables 117 Creating Methods 117 Defining Methods 118 The this Keyword 120 Variable Scope and Method Definitions 121 Passing Arguments to Methods 122 Class Methods 123 Creating Java Applications 124 Helper Classes 125 Java Applications and Command-line Arguments 126 Passing Arguments to Java Applications 126 Handling Arguments in Your Java Application 127 Creating Methods with the Same Name, Different Arguments 128 Constructor Methods 132 Basic Constructor Methods 133 Calling Another Constructor Method 133 Overloading Constructor Methods 134 Contents vii www.it-ebooks.info Overriding Methods 136 Creating Methods That Override Existing Methods 136 Calling the Original Method 137 Overriding Constructors 138 Finalizer Methods 140 Summary 141 Q&A 141 Quiz 142 Questions 142 Answers 143 Certification Practice 143 Exercises 144 DAY 6: Packages, Interfaces, and Other Class Features 145 Modifiers 146 Access Control for Methods and Variables 146 Static Variables and Methods 152 Final Classes, Methods, and Variables 154 Variables 155 Methods 155 Classes 156 Abstract Classes and Methods 156 Packages 157 Using Packages 157 Full Package and Class Names 158 The import Declaration 159 Class Name Conflicts 160 A Note About Classpath and Where Classes Are Located 161 Creating Your Own Packages 162 Picking a Package Name 162 Creating the Folder Structure 162 Adding a Class to a Package 163 Packages and Class Access Control 163 Interfaces 164 The Problem of Single Inheritance 164 Interfaces and Classes 164 viii Sams Teach Yourself Java 6 in 21 Days www.it-ebooks.info Implementing and Using Interfaces 165 Implementing Multiple Interfaces 165 Other Uses of Interfaces 166 Creating and Extending Interfaces 166 New Interfaces 166 Methods Inside Interfaces 167 Extending Interfaces 168 Creating an Online Storefront 169 Inner Classes 175 Summary 177 Q&A 178 Quiz 179 Questions 179 Answers 179 Certification Practice 179 Exercises 181 DAY 7: Exceptions, Assertions, and Threads 183 Exceptions 184 Exception Classes 186 Managing Exceptions 187 Exception Consistency Checking 188 Protecting Code and Catching Exceptions 188 The finally Clause 190 Declaring Methods That Might Throw Exceptions 193 The throws Clause 193 Which Exceptions Should You Throw? 194 Passing On Exceptions 195 throws and Inheritance 196 Creating and Throwing Your Own Exceptions 197 Throwing Exceptions 197 Creating Your Own Exceptions 198 Combining throws, try, and throw 199 Contents ix www.it-ebooks.info [...]... programs, and you also can use Sun’s Java Development Kit The kit, which is available for free on the Web at http:/ /java. sun.com, is a set of command-line tools for writing, compiling, and testing Java programs www.it-ebooks.info 2 Sams Teach Yourself Java 6 in 21 Days In Sams Teach Yourself Java 6 in 21 Days, you are introduced to all aspects of Java software development using the most current version of... 597 Appendixes APPENDIX A: Using the Java Development Kit Choosing a Java Development Tool 60 1 60 2 Installing the Java Development Kit 60 3 www.it-ebooks.info xvii xviii Sams Teach Yourself Java 6 in 21 Days Configuring the Java Development Kit ... used in its place such as “letdown,” “waste,” and “major disappointment to your mother and I.” As you develop your skills during the 21 one-day tutorials in Sams Teach Yourself Java 6 in 21 Days, Fifth Edition, you’ll be in a good position to judge whether the language has lived up to a decade of hype You’ll also become a Java programmer with a lot of potential www.it-ebooks.info 10 DAY 1: Getting Started... By the time you’re finished, you’ll be well acquainted with the reasons Java has become the most widely adopted programming language of the past decade How This Book Is Organized Sams Teach Yourself Java 6 in 21 Days teaches you about the Java language and how to use it to create applications for any computing environment and servlets that run on web servers By the time you have finished the book, you’ll... user interface using Swing, an extensive set of classes for interfaces, graphics, and user interactions n Day 10 covers more than a dozen interface components that you can use in a Java program, including buttons, text fields, sliders, scrolling text areas, and icons n Day 11 explains how to make a user interface look good using layout managers, a set of classes that determine how components on an interface... CD1 Choosing Java CD2 Writing Java Applets CD3 Regular Expressions CD4 Where to Go from Here: Java Resources www.it-ebooks.info About the Authors Rogers Cadenhead is a web application developer and author He has written 22 books on Internet-related topics, including Sams Teach Yourself Java in 24 Hours He’s also a web publisher whose sites receive more than 24 million visits per year He maintains this... capabilities of Derby, the open source database that’s included for the first time in Java 6 www.it-ebooks.info 3 4 Sams Teach Yourself Java 6 in 21 Days n Day 19 covers how to read and write RSS documents using the XML Object Model (XOM), an open source Java class library RSS feeds, one of the most popular XML dialects in use today, enable millions of people to follow site updates and other new web... compile, and run Java programs that use the most up-to-date version of the language: Java 6 Several popular integrated development environments for Java support version 6, including Borland JBuilder, IntelliJ IDEA, and Eclipse These are each recommended by Java developers, but if you are learning to use these tools at the same time as you are learning Java, it can be a daunting task Most integrated development... readers preparing for Java certification Solutions to the exercises and the answer to the certification question can be found on the book’s official website at http://www .java2 1days. com www.it-ebooks.info 5 This page intentionally left blank www.it-ebooks.info WEEK 1: The Java Language 1 Getting Started with Java 2 The ABCs of Programming 3 Working with Objects 4 Lists, Logic, and Loops 5 Creating Classes... 60 5 Using a Command-line Interface 60 6 Opening Folders in MS-DOS 60 7 Creating Folders in MS-DOS 60 8 Running Programs in MS-DOS . www.it-ebooks.info in 21 Days Java ™ 6 Sams Teach Yourself Rogers Cadenhead Laura Lemay 800 East 96th Street, Indianapolis, Indiana 462 40 www.it-ebooks.info Sams Teach Yourself Java ™ 6 in 21 Days Copyright. Uses of Interfaces 166 Creating and Extending Interfaces 166 New Interfaces 166 Methods Inside Interfaces 167 Extending Interfaces 168 Creating an Online Storefront 169 Inner Classes 175 Summary . Inheritance 164 Interfaces and Classes 164 viii Sams Teach Yourself Java 6 in 21 Days www.it-ebooks.info Implementing and Using Interfaces 165 Implementing Multiple Interfaces 165 Other Uses of Interfaces

Ngày đăng: 24/04/2014, 16:04

Từ khóa liên quan

Mục lục

  • Sams Teach Yourself Java 6 in 21 Days

    • Table of Contents

    • Introduction

      • How This Book Is Organized

      • Who Should Read This Book

      • Conventions Used in This Book

      • WEEK I: The Java Language

        • DAY 1: Getting Started with Java

          • The Java Language

          • Object-Oriented Programming

          • Objects and Classes

          • Attributes and Behavior

          • Organizing Classes and Class Behavior

          • Summary

          • Q&A

          • Quiz

          • Exercises

          • DAY 2: The ABCs of Programming

            • Statements and Expressions

            • Variables and Data Types

            • Comments

            • Literals

            • Expressions and Operators

            • String Arithmetic

            • Summary

Tài liệu cùng người dùng

Tài liệu liên quan