Thông tin tài liệu
www.it-ebooks.info
Learn Java for Android
Development
Second Edition
Je Friesen
Apress
www.it-ebooks.info
Learn Java for Android Development
Copyright © 2013 by Je Friesen
is work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microlms 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 specically 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 benet of the trademark owner, with no intention of infringement of the trademark.
e use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identied
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. e 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, Je Olson, Jerey 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 orders-ny@springer-sbm.com, 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 rights@apress.com, or visit www.apress.com.
A
press 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
ISB
N 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
Chapter 1: Getting Started With Java N 1
Chapter 2: Learning Language Fundamentals N 21
Chapter 3: Discovering Classes and Objects N 63
Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces N 105
Chapter 5: Mastering Advanced Language Features Part 1 N 153
Chapter 6: Mastering Advanced Language Features Part 2 N 197
Chapter 7: Exploring the Basic APIs Part 1 N 247
Chapter 8: Exploring the Basic APIs Part 2 N 279
Chapter 9: Exploring the Collections Framework N 327
Chapter 10: Exploring Additional Utility APIs N 407
Chapter 11: Performing Classic I/O N 449
Chapter 12: Accessing Networks N 525
www.it-ebooks.info
vi Contents at a Glance
Chapter 13: Migrating to New I/O N 561
Chapter 14: Accessing Databases N 603
Appendix A: Solutions to Exercises N 643
Appendix B: Four of a Kind N 713
Index 735
www.it-ebooks.info
vii
Contents
About the Author xvii
About the Technical Reviewers xix
Acknowledgments xxi
Introduction xxiii
Chapter 1: Getting Started With Java N 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
Chapter 2: Learning Language Fundamentals N 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
Chapter 3: Discovering Classes and Objects N 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
ixContents
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
Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces N 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
Chapter 5: Mastering Advanced Language Features Part 1 N 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
Chapter 6: Mastering Advanced Language Features Part 2 N 197
Mastering Assertions 197
Declaring Assertions 198
Using Assertions 199
Avoiding Assertions 205
Enabling and Disabling Assertions 206
www.it-ebooks.info
[...]... that run on desktops Java SE is also used to develop applets, which are programs that run in web browsers Java Platform, Enterprise Edition (Java EE): The Java platform for developing enterprise-oriented applications and servlets, which are server programs that conform to Java EE’s Servlet API Java EE is built on top of Java SE Java Platform, Micro Edition (Java ME): The Java platform for developing MIDlets,... platform (and possibly stealing sensitive information) Java SE, Java EE, Java ME, and Android Developers use different editions of the Java platform to create Java programs that run on desktop computers, web browsers, web servers, mobile information devices (e.g., feature phones), and embedded devices (e.g., television set-top boxes): Java Platform, Standard Edition (Java SE): The Java platform for. .. introduce you to Java by first focusing on Java s dual nature (language and platform) I then briefly introduce you to Oracle’s Java SE, Java EE, and Java ME editions of the Java development software, as well as Google’s Android edition You next learn how to download and install the Java SE Development Kit (JDK) and learn some Java basics by developing and playing with a pair of simple Java applications... a copy of Beginning Android 4 by Grant Allen (Apress, 2012) and start learning how to develop Android apps In that book, you learn Android basics and how to create “innovative and salable applications for Android 4 mobile devices.” Rather than give a few superficial details of Android development, Learn Java for Android Development Second Edition concentrates on teaching you the Java language and APIs... With Java In this book, I cover the Java language (supported by Java SE and Android) and various Java SE APIs that are also supported by Android I focus on language features through Java version 5 and on Java APIs through Java 5, with a small amount of Java 6 Note Google’s Android platform is based on an open source release of Java 5 It doesn’t officially recognize language features newer than Java. .. understand Java, how can you proceed to understand Android? Note I also recommend that you check out the second edition of Android Recipes (see www.apress.com/9781430246145) Although the content of that book largely contains independent recipes for learning all kinds of things about Android, Chapter 1 contains a summarized and rapid introduction to Android app development You will learn much about Android. .. which are programs that run on mobile information devices, and Xlets, which are programs that run on embedded devices Developers also use a special Google-created edition of the Java platform (see http://developer .android. com/index.html) to create Android apps that run on Android- enabled devices This edition is known as the Android platform Google’s Android platform presents a Dalvik virtual machine... written in Java and interact with many of the standard Java APIs (e.g., threading and input/output APIs) I wrote Learn Java for Android Development to give you a solid Java foundation that you can later extend with knowledge of Android architecture, API, and tool specifics This book will give you a strong grasp of the Java language and many important APIs that are fundamental to Android apps and other Java. .. develop Java applications (and, eventually, Android apps), I provide you with a high-level overview of various Java APIs that you can access from your Java applications and Android apps In subsequent chapters, you’ll explore these and other useful APIs in greater detail J Friesen, Learn Java for Android Development © Jeff Friesen 2013 1 www.it-ebooks.info 2 CHAPTER 1: Getting Started With Java Note... originated by Sun Microsystems In this section, I briefly describe this language and reveal what it means for Java to be a platform To meet various needs, Sun organized Java into three main editions: Java SE, Java EE, and Java ME This section also briefly explores each of these editions, along with Android Note Java has an interesting history that dates back to December 1990 At that time, James Gosling, Patrick . www.it-ebooks.info
Learn Java for Android
Development
Second Edition
Je Friesen
Apress
www.it-ebooks.info
Learn Java for Android Development
Copyright. xxiii
Chapter 1: Getting Started With Java N 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
Ngày đăng: 17/03/2014, 11:20
Xem thêm: Learn Java for Android Development Second Edition doc, Learn Java for Android Development Second Edition doc