Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Learn Java for Android Development Second Edition pptx
Nội dung xem thử
Mô tả chi tiết
www.it-ebooks.info
Learn Java for Android
Development
Second Edition
Jeff Friesen
Apress
www.it-ebooks.info
Learn Java for Android Development
Copyright © 2013 by Jeff Friesen
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material
supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the
purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the
Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from
Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are
liable to prosecution under the respective Copyright Law.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion
and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified
as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning
Lead Editor: Steve Anglin
Developmental Editors: Tom Welsh and Matthew Moodie
Technical Reviewers: Paul Connolly, Chad Darby and Onur Cinar
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,
Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham,
Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Gwenan Spearing, Matt Wade, Tom Welsh
Coordinating Editor: Katie Sullivan
Copy Editor: Deanna K. Hegle
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit
www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science +
Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook
Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to readers at
www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/.
ISBN 978-1-4302-5722-6
ISBN 978-1-4302-5723-3 (eBook)
www.it-ebooks.info
To Amaury.
www.it-ebooks.info
v
Contents at a Glance
About the Author .............................................................................................................. xvii
About the Technical Reviewers ......................................................................................... xix
Acknowledgments ............................................................................................................. xxi
Introduction ..................................................................................................................... xxiii
NChapter 1: Getting Started With Java ................................................................................1
NChapter 2: Learning Language Fundamentals .................................................................21
NChapter 3: Discovering Classes and Objects ...................................................................63
NChapter 4: Discovering Inheritance, Polymorphism, and Interfaces .............................105
NChapter 5: Mastering Advanced Language Features Part 1 ..........................................153
NChapter 6: Mastering Advanced Language Features Part 2 ..........................................197
NChapter 7: Exploring the Basic APIs Part 1 ...................................................................247
NChapter 8: Exploring the Basic APIs Part 2 ...................................................................279
NChapter 9: Exploring the Collections Framework ..........................................................327
NChapter 10: Exploring Additional Utility APIs ................................................................407
NChapter 11: Performing Classic I/O ...............................................................................449
NChapter 12: Accessing Networks ..................................................................................525
www.it-ebooks.info
vi Contents at a Glance
NChapter 13: Migrating to New I/O ........................................................................... 561
NChapter 14: Accessing Databases .................................................................................603
NAppendix A: Solutions to Exercises ...............................................................................643
NAppendix B: Four of a Kind ............................................................................................713
Index .................................................................................................................................735
www.it-ebooks.info
vii
Contents
About the Author .............................................................................................................. xvii
About the Technical Reviewers ......................................................................................... xix
Acknowledgments ............................................................................................................. xxi
Introduction ..................................................................................................................... xxiii
NChapter 1: Getting Started With Java ................................................................................1
What Is Java? ................................................................................................................................2
Java Is a Language ................................................................................................................................................2
Java Is a Platform ..................................................................................................................................................4
Java SE, Java EE, Java ME, and Android ............................................................................................................... 5
Installing and Exploring the JDK ...................................................................................................6
Installing and Exploring the Eclipse IDE ......................................................................................12
Overview of Java APIs .................................................................................................................16
Language-Support and Other Language-Oriented APIs ....................................................................................... 17
Collections-Oriented APIs .................................................................................................................................... 17
Additional Utility APIs ...........................................................................................................................................17
Classic I/O APIs ....................................................................................................................................................17
Networking APIs ..................................................................................................................................................18
www.it-ebooks.info
viii Contents
New I/O APIs ........................................................................................................................................................18
Database APIs ......................................................................................................................................................18
Summary .....................................................................................................................................19
NChapter 2: Learning Language Fundamentals .................................................................21
Learning Comments ....................................................................................................................21
Single-Line Comments ........................................................................................................................................22
Multiline Comments .............................................................................................................................................22
Javadoc Comments .............................................................................................................................................23
Learning Identifiers .....................................................................................................................25
Learning Types ............................................................................................................................26
Primitive Types ....................................................................................................................................................26
User-Defined Types ..............................................................................................................................................28
Array Types ..........................................................................................................................................................28
Learning Variables .......................................................................................................................29
Learning Expressions ..................................................................................................................30
Simple Expressions .............................................................................................................................................30
Compound Expressions ....................................................................................................................................... 34
Learning Statements ...................................................................................................................46
Assignment Statements ......................................................................................................................................46
Decision Statements ............................................................................................................................................47
Loop Statements..................................................................................................................................................51
Break and Labeled Break Statements ................................................................................................................. 56
Continue and Labeled Continue Statements........................................................................................................ 58
Summary .....................................................................................................................................60
NChapter 3: Discovering Classes and Objects ...................................................................63
Declaring Classes and Instantiating Objects ...............................................................................63
Declaring Classes ................................................................................................................................................64
Instantiating Objects with the New Operator and a Constructor ......................................................................... 64
Specifying Constructor Parameters and Local Variables ..................................................................................... 65
www.it-ebooks.info
Contents ix
Encapsulating State and Behaviors .............................................................................................69
Representing State via Fields .............................................................................................................................. 70
Representing Behaviors via Methods .................................................................................................................. 75
Hiding Information ...............................................................................................................................................84
Initializing Classes and Objects ...................................................................................................89
Class Initializers ...................................................................................................................................................89
Instance Initializers..............................................................................................................................................91
Initialization Order ...............................................................................................................................................93
Collecting Garbage ......................................................................................................................96
Revisiting Arrays .........................................................................................................................99
Summary ...................................................................................................................................104
NChapter 4: Discovering Inheritance, Polymorphism, and Interfaces .............................105
Building Class Hierarchies.........................................................................................................105
Extending Classes .............................................................................................................................................106
The Ultimate Superclass ....................................................................................................................................112
Composition .......................................................................................................................................................122
The Trouble with Implementation Inheritance ................................................................................................... 122
Changing Form ..........................................................................................................................126
Upcasting and Late Binding ............................................................................................................................... 127
Abstract Classes and Abstract Methods ............................................................................................................ 131
Downcasting and Runtime Type Identification................................................................................................... 133
Covariant Return Types ......................................................................................................................................136
Formalizing Class Interfaces .....................................................................................................139
Declaring Interfaces ..........................................................................................................................................139
Implementing Interfaces ....................................................................................................................................140
Extending Interfaces ..........................................................................................................................................144
Why Use Interfaces? ..........................................................................................................................................146
Summary ...................................................................................................................................152
www.it-ebooks.info
x Contents
NChapter 5: Mastering Advanced Language Features Part 1 ..........................................153
Mastering Nested Types ............................................................................................................153
Static Member Classes ......................................................................................................................................153
Nonstatic Member Classes ................................................................................................................................157
Anonymous Classes ...........................................................................................................................................161
Local Classes .....................................................................................................................................................164
Interfaces within Classes ..................................................................................................................................166
Mastering Packages ..................................................................................................................167
What Are Packages? ..........................................................................................................................................168
The Package Statement.....................................................................................................................................169
The Import Statement ........................................................................................................................................169
Searching for Packages and Types .................................................................................................................... 170
Playing with Packages .......................................................................................................................................172
Packages and JAR Files ....................................................................................................................................176
Mastering Static Imports ...........................................................................................................177
Mastering Exceptions ................................................................................................................179
What Are Exceptions? ........................................................................................................................................179
Representing Exceptions in Source Code .......................................................................................................... 179
Throwing Exceptions .........................................................................................................................................184
Handling Exceptions ..........................................................................................................................................187
Performing Cleanup ...........................................................................................................................................190
Summary ...................................................................................................................................195
NChapter 6: Mastering Advanced Language Features Part 2 ..........................................197
Mastering Assertions .................................................................................................................197
Declaring Assertions ..........................................................................................................................................198
Using Assertions ................................................................................................................................................199
Avoiding Assertions ...........................................................................................................................................205
Enabling and Disabling Assertions .................................................................................................................... 206
www.it-ebooks.info
Contents xi
Mastering Annotations ..............................................................................................................207
Discovering Annotations .................................................................................................................................... 207
Declaring Annotation Types and Annotating Source Code ................................................................................. 210
Processing Annotations .....................................................................................................................................215
Mastering Generics ...................................................................................................................217
Collections and the Need for Type Safety .......................................................................................................... 217
Generic Types ....................................................................................................................................................220
Generic Methods ................................................................................................................................................ 229
Arrays and Generics ..........................................................................................................................................232
Mastering Enums ......................................................................................................................234
The Trouble with Traditional Enumerated Types ................................................................................................ 234
The Enum Alternative ........................................................................................................................................235
The Enum Class .................................................................................................................................................241
Summary ...................................................................................................................................245
NChapter 7: Exploring the Basic APIs Part 1 ...................................................................247
Exploring the Math APIs ............................................................................................................247
Math and StrictMath ..........................................................................................................................................247
BigDecimal ........................................................................................................................................................255
BigInteger ..........................................................................................................................................................260
Exploring String Management ...................................................................................................264
String .................................................................................................................................................................264
StringBuffer and StringBuilder .......................................................................................................................... 268
Obtaining Package Information .................................................................................................270
Summary ...................................................................................................................................276
NChapter 8: Exploring the Basic APIs Part 2 ...................................................................279
Exploring the Primitive Type Wrapper Classes ..........................................................................279
Boolean ..............................................................................................................................................................280
Character ...........................................................................................................................................................281
Float and Double ................................................................................................................................................282
Integer, Long, Short, and Byte ............................................................................................................................ 286
Number ..............................................................................................................................................................288
www.it-ebooks.info
xii Contents
Exploring Threads ......................................................................................................................288
Runnable and Thread .........................................................................................................................................289
Thread Synchronization ..................................................................................................................................... 298
Exploring System Capabilities ...................................................................................................314
System ............................................................................................................................................................... 314
Runtime and Process .........................................................................................................................................319
Summary ...................................................................................................................................324
NChapter 9: Exploring the Collections Framework ..........................................................327
Exploring Collections Framework Fundamentals ......................................................................327
Comparable Versus Comparator ........................................................................................................................ 328
Iterable and Collection .......................................................................................................................................330
Exploring Lists ...........................................................................................................................337
ArrayList ............................................................................................................................................................341
LinkedList ..........................................................................................................................................................342
Exploring Sets ...........................................................................................................................344
TreeSet...............................................................................................................................................................344
HashSet ............................................................................................................................................................. 346
EnumSet ............................................................................................................................................................350
Exploring Sorted Sets ................................................................................................................353
Exploring Navigable Sets ...........................................................................................................361
Exploring Queues ......................................................................................................................364
PriorityQueue .....................................................................................................................................................365
Exploring Deques ......................................................................................................................368
ArrayDeque ........................................................................................................................................................372
Exploring Maps ..........................................................................................................................373
TreeMap .............................................................................................................................................................377
HashMap ............................................................................................................................................................378
IdentityHashMap ................................................................................................................................................384
EnumMap...........................................................................................................................................................386
www.it-ebooks.info
Contents xiii
Exploring Sorted Maps ........................................................................................................387
Exploring Navigable Maps ...................................................................................................390
Exploring the Arrays and Collections Utility APIs .................................................................394
Exploring the Legacy Collection APIs ..................................................................................398
Summary .............................................................................................................................404
NChapter 10: Exploring Additional Utility APIs ..........................................................407
Exploring the Concurrency Utilities .....................................................................................407
Executors ....................................................................................................................................................407
Synchronizers .............................................................................................................................................417
Concurrent Collections ...............................................................................................................................420
Locks ..........................................................................................................................................................422
Atomic Variables .........................................................................................................................................425
Exploring the Date Class .....................................................................................................426
Exploring the Formatter Class .............................................................................................428
Exploring the Random Class................................................................................................430
Exploring the Scanner Class................................................................................................432
Exploring the ZIP and JAR APIs ...........................................................................................434
Exploring the ZIP API ...................................................................................................................................435
Exploring the JAR API .................................................................................................................................442
Summary .............................................................................................................................447
NChapter 11: Performing Classic I/O .........................................................................449
Working with the File API ....................................................................................................449
Working with the RandomAccessFile API ............................................................................462
Working with Streams .........................................................................................................473
Stream Classes Overview ...........................................................................................................................474
OutputStream and InputStream ..................................................................................................................475
ByteArrayOutputStream and ByteArrayInputStream ...................................................................................478
FileOutputStream and FileInputStream ......................................................................................................479
PipedOutputStream and PipedInputStream ................................................................................................482
FilterOutputStream and FilterInputStream .................................................................................................485
www.it-ebooks.info
xiv Contents
BufferedOutputStream and BufferedInputStream ............................................................................................. 493
DataOutputStream and DataInputStream .......................................................................................................... 494
Object Serialization and Deserialization ............................................................................................................ 496
PrintStream........................................................................................................................................................510
Working with Writers and Readers ............................................................................................511
Writer and Reader Classes Overview ................................................................................................................ 512
Writer and Reader..............................................................................................................................................514
OutputStreamWriter and InputStreamReader .................................................................................................... 514
FileWriter and FileReader ..................................................................................................................................516
Summary ...................................................................................................................................524
NChapter 12: Accessing Networks ..................................................................................525
Accessing Networks via Sockets ..............................................................................................526
Socket Addresses ..............................................................................................................................................528
Socket Options...................................................................................................................................................529
Socket and ServerSocket ..................................................................................................................................530
DatagramSocket and MulticastSocket .............................................................................................................. 536
Accessing Networks via URLs ...................................................................................................543
URL and URLConnection ....................................................................................................................................543
URLEncoder and URLDecoder ............................................................................................................................547
Accessing Network Interfaces and Interface Addresses ...........................................................549
Managing Cookies .....................................................................................................................555
Summary ...................................................................................................................................559
NChapter 13: Migrating to New I/O ..................................................................................561
Working with Buffers .................................................................................................................562
Buffer and Its Children .......................................................................................................................................562
Buffers in Depth .................................................................................................................................................566
Working with Channels .............................................................................................................575
Channel and Its Children ....................................................................................................................................576
Channels in Depth .............................................................................................................................................580
www.it-ebooks.info
Contents xv
Working With Regular Expressions ......................................................................................589
Pattern, PatternSyntaxException, and Matcher ..........................................................................................589
Character Classes .......................................................................................................................................593
Capturing Groups ........................................................................................................................................594
Boundary Matchers and Zero-Length Matches ..........................................................................................595
Quantifiers ..................................................................................................................................................596
Practical Regular Expressions ....................................................................................................................598
Summary .............................................................................................................................601
NChapter 14: Accessing Databases ...........................................................................603
Introducing Java DB ............................................................................................................604
Java DB Installation and Configuration .......................................................................................................605
Java DB Demos ...........................................................................................................................................607
Java DB Command-Line Tools ....................................................................................................................609
Introducing SQLite ...............................................................................................................611
Accessing Databases via JDBC ...........................................................................................613
Data Sources, Drivers, and Connections .....................................................................................................613
Exceptions ..................................................................................................................................................616
Statements .................................................................................................................................................620
Metadata ....................................................................................................................................................633
Summary .............................................................................................................................639
NAppendix A: Solutions to Exercises .........................................................................643
Chapter 1: Getting Started with Java ..................................................................................643
Chapter 2: Learning Language Fundamentals .....................................................................644
Chapter 3: Discovering Classes and Objects .......................................................................647
Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces ....................................651
Chapter 5: Mastering Advanced Language Features Part 1 ................................................659
Chapter 6: Mastering Advanced Language Features Part 2 ................................................666
Chapter 7: Exploring the Basic APIs Part 1 ..........................................................................671
Chapter 8: Exploring the Basic APIs Part 2 ..........................................................................676
Chapter 9: Exploring the Collections Framework ................................................................679
www.it-ebooks.info