COMPANION eBOOK
Shelve in
Mobile Computing
User level:
Beginning–Intermediatewww.apress.com
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
SOURCE CODE ONLINE
L
earn Java for Android Development, Third Edition, is an update of a strong selling book that now
includes a primer on Android app development. This book teaches programmers the essential
Java language skills necessary for effectively picking up and using the new Android SDK platform to
build mobile, embedded, and even PC apps, especially game apps.
Android development is hot, and many programmers are interested in joining the fun. However,
because this technology is based on Java, you should first obtain a solid grasp of the Java language
and its APIs in order to improve your chances of succeeding as an effective Android app developer.
This book helps you do that.
Each of the book’s chapters provides an exercise section that gives you the opportunity to reinforce
your understanding of the chapter’s material. Answers to the book’s more than 700 exercises are
provided in an appendix. A second appendix provides a significant game-oriented Java application,
which you can convert into an Android app.
Once you complete this one-of-a-kind book written by Jeff Friesen, an expert Java developer and
JavaWorld.com columnist, you should be ready to begin your indie or professional Android app
development journey.
What You’ll Learn:
• The Java skills necessary for Android development
• The core Java language fundamentals
• Classes, objects, inheritance, polymorphism, and interfaces
• Advanced Java language features (such as generics)
• The basic Java APIs necessary for Android (such as the String class and threading)
• The Collections Framework for organizing objects
• The Concurrency Utilities for simplifying multithreading
• Classic and New I/O
• Networking and database access
• Parsing, creating, and transforming XML documents
• Additional APIs for creating and accessing ZIP and JAR files, and more
Learn
Java for Android
Development
Jeff Friesen
Learn the Java skills you will need to start
developing Android apps
Companion
eBook
Available
Friesen
Java
for
Android
Development
Learn
THIRD EDITION
THIRD
EDITION
9781430264545
54499
ISBN 978-1-4302-6454-5
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
v
Contents at a Glance
About the Author���������������������������������������������������������������������������������������������������������������xxi
About the Technical Reviewer�����������������������������������������������������������������������������������������xxiii
Acknowledgments������������������������������������������������������������������������������������������������������������xxv
Introduction��������������������������������������������������������������������������������������������������������������������xxvii
Chapter 1: Getting Started with Java■■��������������������������������������������������������������������������������1
Chapter 2: Learning Language Fundamentals■■�����������������������������������������������������������������31
Chapter 3: Discovering Classes and Objects■■�������������������������������������������������������������������89
Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces■■�����������������������������141
Chapter 5: Mastering Advanced Language Features, Part 1■■�����������������������������������������189
Chapter 6: Mastering Advanced Language Features, Part 2■■�����������������������������������������235
Chapter 7: Exploring the Basic APIs, Part 1■■������������������������������������������������������������������287
Chapter 8: Exploring the Basic APIs, Part 2■■������������������������������������������������������������������359
Chapter 9: Exploring the Collections Framework■■����������������������������������������������������������401
Chapter 10: Exploring the Concurrency Utilities■■�����������������������������������������������������������487
Chapter 11: Performing Classic I/O■■�������������������������������������������������������������������������������539
Chapter 12: Accessing Networks■■����������������������������������������������������������������������������������621
vi Contents at a Glance
Chapter 13: Migrating to New I/O■■����������������������������������������������������������������������������665
Chapter 14: Accessing Databases■■���������������������������������������������������������������������������763
Chapter 15: Parsing, Creating, and Transforming XML Documents■■�������������������������803
Chapter 16: Focusing on Odds and Ends■■�����������������������������������������������������������������885
Appendix A: Solutions to Exercises■■�����������������������������������������������������������������������1015
Appendix B: Four of a Kind■■������������������������������������������������������������������������������������1127
Index�������������������������������������������������������������������������������������������������������������������������1149
xxvii
Tip A good reason to consider Android app development over iPhone/iPad app development is the lower
startup costs that you’ll incur with Android. For example, you don’t need to purchase a Mac on which to
develop Android apps (a Mac is required for developing iPhone/iPad apps); your existing Windows, Linux, or
Unix machine will do nicely.
Introduction
Smartphones and tablets are all the rage these days. Their popularity is largely due to their ability to
run apps. Although the iPhone and iPad, with their growing collection of Objective-C based apps,
had a head start, Android-based smartphones and tablets, with their growing collection of
Java-based apps, have proven to be a strong competitor.
Not only are many iPhone/iPad app developers making money by selling their apps, but many
Android app developers are also making money by selling similar apps. According to tech web sites
such as The Register (www.theregister.co.uk), some Android app developers are making lots of
money (www.theregister.co.uk/2010/03/02/android_app_profit).
In today’s challenging economic climate, you might like to try your hand at developing Android apps
and make some money. If you have good ideas, perseverance, and some artistic talent (or perhaps
know some talented individuals), you are already part of the way toward achieving this goal.
Most importantly, you’ll need to possess a solid understanding of the Java language and
foundational application programming interfaces (APIs) before jumping into Android. After all,
Android apps are written in Java and interact with many of the standard Java APIs (such as
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 the many important APIs that are fundamental to Android
apps and other Java applications. It will also introduce you to key development tools.
xxviiiIntroduction
Book Organization
The first edition of this book was organized into 10 chapters and 1 appendix. The second edition
was organized into 14 chapters and 3 appendixes. This third edition is organized into 16 chapters
and 2 appendixes with a bonus appendix on Android app development. Each chapter in each edition
offers a set of exercises that you should complete to get the most benefit from its content. Their
solutions are presented in Appendix A.
Chapter 1 introduces you to Java by first focusing on Java’s dual nature (language and platform). It
then briefly introduces you to Oracle’s Java SE, Java EE, and Java ME editions of the Java platform.
You next learn how to download and install the Java SE Development Kit (JDK), and you learn some
Java basics by developing and playing with three simple Java applications. After receiving a brief
introduction to the Eclipse IDE, you receive a brief introduction to Android.
Chapter 2 starts you on an in-depth journey of the Java language by focusing on language
fundamentals. You first learn about simple application structure and then learn about comments,
identifiers (and reserved words), types, variables, expressions (and literals), and statements.
Chapter 3 continues your journey by focusing on classes and objects. You learn how to declare a
class and organize applications around multiple classes. You then learn how to construct objects
from classes, declare fields in classes and access these fields, declare methods in classes and call
them, initialize classes and objects, and remove objects when they’re no longer needed. You also
learn more about arrays, which were first introduced in Chapter 2.
Chapter 4 adds to Chapter 3’s pool of object-based knowledge by introducing you to the language
features that take you from object-based applications to object-oriented applications. Specifically,
you learn about features related to inheritance, polymorphism, and interfaces. While exploring
inheritance, you learn about Java’s ultimate superclass. Also, while exploring interfaces, you
discover why they were included in the Java language; interfaces are not merely a workaround for
Java’s lack of support for multiple implementation inheritance, but serve a higher purpose.
Chapter 5 introduces you to four categories of advanced language features: nested types, packages,
static imports, and exceptions.
Chapter 6 introduces you to four additional advanced language feature categories: assertions,
annotations, generics, and enums.
Chapter 7 begins a trend that focuses more on APIs than language features. This chapter first
introduces you to Java’s Math and StrictMath math-oriented types. It then explores Number and its
various subtypes (such as Integer, Double, and BigDecimal). Next you explore the string-oriented
types (String, StringBuffer, and StringBuilder) followed by the System type. Finally, you explore
the Thread class and related types for creating multithreaded applications.
Chapter 8 continues to explore Java’s basic APIs by focusing on the Random class for generating
random numbers; the References API, Reflection, the StringTokenizer class for breaking a string
into smaller components; and the Timer and TimerTask classes for occasionally or repeatedly
executing tasks.
Chapter 9 focuses exclusively on Java’s Collections Framework, which provides you with a solution
for organizing objects in lists, sets, queues, and maps. You also learn about collection-oriented utility
classes and review Java’s legacy collection types.
xxixIntroduction
Chapter 10 focuses exclusively on Java’s Concurrency Utilities. After receiving an introduction to
this framework, you explore executors, synchronizers (such as countdown latches), concurrent
collections, the Locking Framework, and atomic variables (where you discover compare-and-swap).
Chapter 11 is all about classic input/output (I/O), largely from a file perspective. In this chapter, you
explore classic I/O in terms of the File class, RandomAccessFile class, various stream classes, and
various writer/reader classes. My discussion of stream I/O includes coverage of Java’s object
serialization and deserialization mechanisms.
Chapter 12 continues to explore classic I/O by focusing on networks. You learn about the Socket,
ServerSocket, DatagramSocket, and MulticastSocket classes along with related types. You also
learn about the URL class for achieving networked I/O at a higher level and learn about the related
URI class. After learning about the low-level NetworkInterface and InterfaceAddress classes, you
explore cookie management, in terms of the CookieHandler and CookieManager classes, and the
CookiePolicy and CookieStore interfaces.
Chapter 13 i
0 коментара
За да коментирате, трябва да сте влезли в профила си.
Влезте