Sie sind auf Seite 1von 14

SCJP

Sun Certified Programmer


for Java Platform, SE6
®

Study Guide

Richard F. Raposa
Acquisitions Editor: Jeff Kellum Disclaimer: This eBook does not include ancillary media
Development Editor: Jennifer Leland
Technical Editor: James Nuzzi that was packaged with the printed version of the book.
Production Editor: Christine O’Connor
Copy Editor: Elizabeth Welch
Production Manager: Tim Tate
Vice President and Executive Group Publisher: Richard Swadley
Vice President and Publisher: Neil Edde
Media Project Manager 1: Laura Moss-Hollister
Media Associate Producer: Shawn Patrick
Media Quality Assurance: Angie Denny
Book Designer: Judy Fung, Bill Gibson
Proofreader: Nancy Bell
Indexer: Robert Swanson
Project Coordinator, Cover: Lynsey Stanford
Cover Designer: Ryan Sneed
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-41797-3
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under
Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the
Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center,
222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher
for permission should be addressed to the Permission Department, John Wiley & Sons, Inc., 111 River Street,
Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties
with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties,
including without limitation warranties of fitness for a particular purpose. No warranty may be created or
extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for
every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal,
accounting, or other professional services. If professional assistance is required, the services of a competent
professional person should be sought. Neither the publisher nor the author shall be liable for damages arising
herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential
source of further information does not mean that the author or the publisher endorses the information the
organization or Web site may provide or recommendations it may make. Further, readers should be aware that
Internet Web sites listed in this work may have changed or disappeared between when this work was written and
when it is read.
For general information on our other products and services or to obtain technical support, please contact our
Customer Care Department within the U.S. at (877) 762-2974, outside the U.S. at (317) 572-3993 or fax (317)
572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
Library of Congress Cataloging-in-Publication Data
Raposa, Richard F.
SCJP Sun certified programmer for Java platform, SE6, study guide / Richard F. Raposa. — 1st ed.
p. cm.
ISBN 978-0-470-41797-3 (paper/cd-rom)
1. Electronic data processing personnel — Certification. 2. Operating systems (Computers) —
Examinations — Study guides. 3. Java (Computer program language) — Examinations — Study guides.
I. Title.
QA76.3.R357 2009
005.13'3—dc22
2008054906

TRADEMARKS: Wiley, the Wiley logo, and the Sybex logo are trademarks or registered trademarks of John
Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without
written permission. Java is a registered trademark of Sun Microsystems, Inc. All other trademarks are the
property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor
mentioned in this book.
10 9 8 7 6 5 4 3 2 1
Dear Reader,

Thank you for choosing SCJP: Sun Certified Programmer for Java Platform, SE6 Study
Guide. This book is part of a family of premium-quality Sybex books, all of which are
written by outstanding authors who combine practical experience with a gift for teaching.

Sybex was founded in 1976. More than thirty years later, we’re still committed to
producing consistently exceptional books. With each of our titles we’re working hard to
set a new standard for the industry. From the paper we print on, to the authors we work
with, our goal is to bring you the best books available.

I hope you see all that reflected in these pages. I’d be very interested to hear your comments
and get your feedback on how we’re doing. Feel free to let me know what you think
about this or any other Sybex book by sending me an email at nedde@wiley.com, or if you
think you’ve found a technical error in this book, please visit http://sybex.custhelp.com.
Customer feedback is critical to our efforts at Sybex.

Best regards,

Neil Edde
Vice President and Publisher
Sybex, an Imprint of Wiley
To Susan, Megan, Ryan, Katelyn, Emma and Sara.
Acknowledgments
A lot of time and energy goes into a book like this, and my wife and kids will be the fi rst
ones to attest to that fact! I owe them many thanks for their patience and understanding
during the months that went into this project.
I also want to thank Jennifer Leland, the Developmental Editor, for putting up with
my complete inability to learn when to use the appropriate styles. Everyone who reads
this book owes James Nuzzi a big thank you for his meticulous job as Technical Editor.
The text and sample questions involve a lot of code, and James did an amazing job fi nding
errors and typos. Thanks also to Jeff Kellum, Pete Gaughan, Christine O’Connor, and
everyone at John Wiley & Sons, Inc., who helped make this book a reality.
And last but not least, I want to thank all of you who are reading this book in hopes of
learning Java and passing the SCJP Exam. I hope all of you fi nd this book informative and
indispensable wherever your Java adventures take you. Good luck!

About the Author


Rich Raposa runs a Java training fi rm, JLicense, Inc., based out of Rapid City, SD. He is
a Sun Certified Java Programmer as well as a Sun Certified Java Instructor, and has spent
the past 11 years delivering Java training courses to businesses across the United States. He
has written dozens of Java courses ranging from introductory Java to advanced topics like
Enterprise JavaBeans, Java Web development, and Java Web Services. He enjoys playing
poker and playing the guitar (though he does not claim to be good at either).
Contents at a Glance
Introduction xvii

Assessment Test xxiv

Chapter 1 Fundamentals 1
Chapter 2 Declarations, Initialization, and Scoping 77
Chapter 3 Flow Control 187
Chapter 4 API Contents 269
Chapter 5 Concurrency 341
Chapter 6 Object-Oriented Concepts 381
Chapter 7 Collections and Generics 425
Appendix 491
Glossary 495

Index 507
Contents
Introduction xvii

Assessment Test xxiv

Chapter 1 Fundamentals 1
Writing Java Classes 2
Packages 4
The package Keyword 5
The import Keyword 6
Package Directory Structure 9
The CLASSPATH Environment Variable 11
Running Java Applications 12
The -classpath Flag 17
JAR Files 17
Command-Line Arguments 19
Reference vs. Primitive Types 22
Primitive Types 22
Reference Types 23
Garbage Collection 28
The System.gc Method 32
The finalize Method 33
Call by Value 36
Java Operators 43
The Assignment Operators 44
The Arithmetic Operators 46
The Relational Operators 50
The instanceof Operator 51
The Bitwise and Logical Operators 53
The Conditional Operator 55
The Equality Operators 56
Equality of Objects 58
Summary 61
Exam Essentials 61
Review Questions 63
Answers to Review Questions 73

Chapter 2 Declarations, Initialization, and Scoping 77


Declaring Variables 78
Scoping 80
Instance Variables 80
Class Variables 83
Local Variables 86
xii Contents

Declaring Arrays 88
Array References 89
Array Objects 90
Using Arrays 91
Multidimensional Arrays 93
Array Initializers 95
Declaring Classes 97
The Instantiation Process 100
Constructors 102
The Default Constructor 104
Using this in Constructors 105
Using super in Constructors 108
Instance Initializers 111
Static Initializers 114
Declaring Methods 116
Method Declarations 116
JavaBeans Naming Convention 118
Instance Methods 121
Static Methods 124
Variable-Length Arguments 126
Method Overloading 128
Method Overriding 131
Covariant Return Types 134
Method Hiding 135
Final Methods 137
Declaring Abstract Classes 138
Abstract Methods 140
Declaring Interfaces 143
Implementing Interfaces 144
Extending Interfaces 147
Declaring Enumerations 147
Using enums 149
Declaring enum Methods 150
Declaring enum Constructors 151
Declaring Nested Classes 152
Member Inner Classes 152
Local Inner Classes 158
Anonymous Inner Classes 159
Static Nested Classes 162
Summary 165
Exam Essentials 166
Review Questions 168
Answers to Review Questions 183
Contents xiii

Chapter 3 Flow Control 187


Overview of Flow Control 188
The if Statement 188
The switch Statement 192
Switching on an Enum 194
Final case Values 196
The for Statement 197
The Basic for Statement 197
The Enhanced for Statement 201
The while Statement 206
The do Statement 209
The break Statement 211
The continue Statement 215
Overview of Assertions 218
The assert Statement 219
Enabling Assertions 220
Using Assertions 221
Overview of Exceptions 224
The try Statement 227
Multiple catch Clauses 229
The Handle or Declare Rule 231
The finally Block 238
Java API Exceptions and Errors 242
ArrayIndexOutOfBoundsException 243
ClassCastException 244
IllegalArgumentException 245
IllegalStateException 245
NullPointerException 246
NumberFormatException 246
AssertionError 247
ExceptionInInitializerError 247
StackOverflowError 248
NoClassDefFoundError 249
Summary 249
Exam Essentials 250
Review Questions 251
Answers to Review Questions 265

Chapter 4 API Contents 269


The Primitive Wrapper Classes 270
Autoboxing and Unboxing 272
Strings 274
The String Class 275
The StringBuilder and StringBuffer Classes 278
xiv Contents

Input and Output 281


Streams vs. Readers and Writers 281
Low-Level vs. High-Level Streams 283
File Input and Output 285
The FileReader and FileWriter Classes 285
The File Class 287
The FileInputStream and FileOutputStream Classes 289
The DataInputStream and DataOutputStream Classes 291
The PrintWriter Class 294
The format and printf Methods 295
The Console Class 298
Object Serialization 301
The Serializable Interface 301
The ObjectOutputStream Class 303
The ObjectInputStream Class 304
Formatting and Parsing Data 306
Format and Parse Numbers and Currency 306
Format and Parse Dates 312
Regular Expressions 315
The Pattern and Matcher Classes 315
The String.split Method 320
The Scanner Class 322
Summary 324
Exam Essentials 325
Review Questions 326
Answers to Review Questions 337

Chapter 5 Concurrency 341


Overview of Threads 342
Writing a Thread 343
Implementing the Runnable Interface 344
Extending the Thread Class 346
Thread States 349
New Threads 349
Runnable Threads 351
Blocked Threads 353
Waiting and Timed-Waiting Threads 353
Terminated Threads 355
Thread Synchronization 355
The Monitor Lock 358
The wait, notify, and notifyAll Methods 363
Summary 368
Contents xv

Exam Essentials 369


Review Questions 370
Answers to Review Questions 378

Chapter 6 Object-Oriented Concepts 381


Encapsulation, Coupling, and Cohesion 382
Tight Encapsulation 382
Loose Coupling 385
High Cohesion 388
OO Design Relationships 390
The “is-a” Relationship 390
The “has-a” Relationship 392
Modifiers and Inheritance 393
The Access Modifiers 393
The abstract Modifier 397
The final Modifier 399
Polymorphism 401
Understanding Polymorphism 402
Casting Polymorphic References 405
Summary 409
Exam Essentials 410
Review Questions 412
Answers to Review Questions 423

Chapter 7 Collections and Generics 425


Overview of Collections 426
The Collections Interfaces 426
The Comparable Interface 432
The Difference Between == and equals 433
Using Generics 436
Limitations of Nongeneric Collections 436
Lists 438
Sets 441
Maps 445
Generic Types and Methods 449
Generic Classes 450
Generic Interfaces 451
Generic Methods 453
Bounded Generic Types 455
Generic Wildcards 458
Working with Lists 461
Sorting Lists 461
Searching Lists 467
xvi Contents

Working with Arrays 470


Sorting Arrays 470
Searching Arrays 473
Summary 475
Exam Essentials 476
Review Questions 478
Answers to Review Questions 487

Appendix 491

Glossary 495

Index 507

Das könnte Ihnen auch gefallen