Learn java for android development, 3rd edition

1.2K 135 0
Learn java for android development, 3rd 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

Learn the Java skills you will need to start developing Android apps Learn Java for Android Development THIRD EDITION Jeff Friesen www.it-ebooks.info 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 www.it-ebooks.info 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 v www.it-ebooks.info 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 www.it-ebooks.info 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 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 nicely 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 xxvii www.it-ebooks.info xxviii Introduction Book Organization The first edition of this book was organized into 10 chapters and appendix The second edition was organized into 14 chapters and appendixes This third edition is organized into 16 chapters and 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 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 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 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 Chapter 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 introduces you to four categories of advanced language features: nested types, packages, static imports, and exceptions Chapter introduces you to four additional advanced language feature categories: assertions, annotations, generics, and enums Chapter 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 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 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 www.it-ebooks.info Introduction xxix 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 introduces you to New I/O You learn about buffers, channels, selectors, regular expressions, charsets, and the Formatter and Scanner types in this chapter Chapter 14 focuses on databases You first learn about the Java DB and SQLite database products, and then explore JDBC for communicating with databases created via these products Chapter 15 emphasizes Java’s support for XML I first provide a tutorial on this topic where you learn about the XML declaration, elements and attributes, character references and CDATA sections, namespaces, comments and processing instructions, well-formed documents, and valid documents (in terms of Document Type Definition and XML Schema) I then show you how to parse XML documents via the SAX API, parse and create XML documents via the DOM API, parse XML documents via the XMLPULL V1 API (supported by Android as an alternative to Java’s StAX API), use the XPath API to concisely select nodes via location path expressions, and transform XML documents via XSLT Chapter 16 completes the chapter portion of this book by covering odds and ends You first learn about useful Java language features that I’ve successfully used in Android apps Next, you explore classloaders, the Console class, design patterns (with emphasis on the Strategy pattern), double brace initialization, fluent interfaces, immutability, internationalization (in terms of locales; resource bundles; break iterators; collators; dates, time zones, and calendars; and formatters), the Logging API, the Preferences API, the Runtime and Process classes, the Java Native Interface, and the ZIP and JAR APIs Appendix A presents solutions to all of the exercises in Chapters through 16 Appendix B introduces you to application development in the context of Four of a Kind, a console-based card game Appendix C provides an introduction to Android app development It gives you a chance to see how various Java language features and APIs are used in an Android context Unlike the other elements, Appendix C is not included in this book—it’s included with the book’s source code Appendix C doesn’t officially belong in Learn Java for Android Development because this book’s focus is to prepare you for getting into Android app development by teaching you the fundamentals of the Java language, and Appendix C goes beyond that focus by giving you a tutorial on Android app development Besides, the presence of this appendix would cause the book to exceed the 1,200-page print-on-demand limit www.it-ebooks.info xxx Introduction Note  You can download this book’s source code by pointing your web browser to www.apress.com/9781430264545 and clicking the Source Code tab followed by the Download Now link What Comes Next? After you complete this book, I recommend that you check out Apress’s other Android-oriented books, such as Beginning Android by Grant Allen (Apress, 2012), and learn more about developing Android apps In that book, you learn Android basics and how to create “innovative and salable applications for Android mobile devices.” Thanks for purchasing this third (and my final) edition of Learn Java for Android Development I hope you find it a helpful preparation for, and I wish you lots of success in achieving, a satisfying and lucrative career as an Android app developer —Jeff Friesen, January 2014 www.it-ebooks.info Chapter Getting Started with Java Android apps are written in Java and use various Java application program interfaces (APIs) Because you’ll want to write your own apps, but may be unfamiliar with the Java language and these APIs, this book teaches you about Java as a first step into Android app development It provides you with Java language fundamentals and Java APIs that are useful when developing apps Note  This book illustrates Java concepts via non-Android Java applications It’s easier for beginners to grasp these applications than corresponding Android apps However, I also reveal a trivial Android app toward the end of this chapter for comparison purposes An API is an interface that application code uses to communicate with other code, which is typically stored in a software library For more information on this term, check out Wikipedia’s “Application programming interface” topic at http://en.wikipedia.org/wiki/Application_programming_interface This chapter sets the stage for teaching you the essential Java concepts that you need to understand before embarking on an Android app development career I first answer the question: “What is Java?” Next, I show you how to install the Java SE Development Kit (JDK) and introduce you to JDK tools for compiling and running Java applications After presenting a few simple example applications, I show you how to install and use the open source Eclipse IDE (integrated development environment) so that you can more easily (and more quickly) develop Java applications and (eventually) Android apps I then provide you with a brief introduction to Android and show you how Java fits into the Android development paradigm www.it-ebooks.info CHAPTER 1: Getting Started with Java What Is Java? Java is a language and a platform 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 briefly explores each of these editions Note  Java has an interesting history that dates back to December 1990 At that time, James Gosling, Patrick Naughton, and Mike Sheridan (all employees of Sun Microsystems) were given the task of figuring out the next major trend in computing They concluded that one trend would involve the convergence of computing devices and intelligent consumer appliances Thus was born the Green Project The fruits of Green were Star7, a handheld wireless device featuring a five-inch color LCD screen, a SPARC processor, a sophisticated graphics capability, a version of Unix, and Oak, a language developed by James Gosling for writing applications to run on Star7 that he named after an oak tree growing outside of his office window at Sun To avoid a conflict with another language of the same name, Dr Gosling changed this language’s name to Java Sun Microsystems subsequently evolved the Java language and platform until Oracle acquired Sun in early 2010 Check out http://oracle.com/technetwork/java/index.html for the latest Java news from Oracle Java Is a Language Java is a language in which developers express source code (program text) Java’s syntax (rules for combining symbols into language features) is partly patterned after the C and C++ languages in order to shorten the learning curve for C/C++ developers The following list identifies a few similarities between Java and C/C++:  Java and C/C++ share the same single-line and multi-line comment styles Comments let you document source code  Many of Java’s reserved words are identical to their C/C++ counterparts (for, if, switch, and while are examples) and C++ counterparts (catch, class, public, and try are examples)  Java supports character, double precision floating-point, floating-point, integer, long integer, and short integer primitive types via the same char, double, float, int, long, and short reserved words  Java supports many of the same operators, including arithmetic (+, -, *, /, and %) and conditional (?:) operators  Java uses brace characters ({ and }) to delimit blocks of statements www.it-ebooks.info viii Contents Android Architecture�������������������������������������������������������������������������������������������������������������������������������������������� 24 Android Says Hello���������������������������������������������������������������������������������������������������������������������������������������������� 28 Summary�������������������������������������������������������������������������������������������������������������������������������������30 ■■Chapter 2: Learning Language Fundamentals�����������������������������������������������������������������31 Learning Application Structure����������������������������������������������������������������������������������������������������31 Learning Comments��������������������������������������������������������������������������������������������������������������������33 Single-Line Comments���������������������������������������������������������������������������������������������������������������������������������������� 33 Multiline Comments��������������������������������������������������������������������������������������������������������������������������������������������� 33 Javadoc Comments��������������������������������������������������������������������������������������������������������������������������������������������� 34 Learning Identifiers���������������������������������������������������������������������������������������������������������������������37 Learning Types����������������������������������������������������������������������������������������������������������������������������38 Primitive Types���������������������������������������������������������������������������������������������������������������������������������������������������� 38 User-Defined Types���������������������������������������������������������������������������������������������������������������������������������������������� 41 Array Types���������������������������������������������������������������������������������������������������������������������������������������������������������� 41 Learning Variables�����������������������������������������������������������������������������������������������������������������������42 Learning Expressions������������������������������������������������������������������������������������������������������������������43 Simple Expressions��������������������������������������������������������������������������������������������������������������������������������������������� 43 Compound Expressions��������������������������������������������������������������������������������������������������������������������������������������� 49 Learning Statements�������������������������������������������������������������������������������������������������������������������70 Assignment Statements�������������������������������������������������������������������������������������������������������������������������������������� 71 Decision Statements�������������������������������������������������������������������������������������������������������������������������������������������� 71 Loop Statements������������������������������������������������������������������������������������������������������������������������������������������������� 76 Break and Labeled Break Statements����������������������������������������������������������������������������������������������������������������� 81 Continue and Labeled Continue Statements������������������������������������������������������������������������������������������������������� 83 Summary�������������������������������������������������������������������������������������������������������������������������������������86 ■■Chapter 3: Discovering Classes and Objects�������������������������������������������������������������������89 Declaring Classes������������������������������������������������������������������������������������������������������������������������89 Classes and Applications������������������������������������������������������������������������������������������������������������������������������������� 91 www.it-ebooks.info Contents ix Constructing Objects�������������������������������������������������������������������������������������������������������������������92 Default Constructor��������������������������������������������������������������������������������������������������������������������������������������������� 93 Explicit Constructors�������������������������������������������������������������������������������������������������������������������������������������������� 93 Objects and Applications������������������������������������������������������������������������������������������������������������������������������������� 97 Encapsulating State and Behaviors���������������������������������������������������������������������������������������������98 Representing State via Fields������������������������������������������������������������������������������������������������������������������������������ 99 Representing Behaviors via Methods���������������������������������������������������������������������������������������������������������������� 107 Hiding Information���������������������������������������������������������������������������������������������������������������������119 Initializing Classes and Objects�������������������������������������������������������������������������������������������������124 Class Initializers������������������������������������������������������������������������������������������������������������������������������������������������� 124 Instance Initializers������������������������������������������������������������������������������������������������������������������������������������������� 126 Initialization Order��������������������������������������������������������������������������������������������������������������������������������������������� 128 Collecting Garbage��������������������������������������������������������������������������������������������������������������������131 Revisiting Arrays�����������������������������������������������������������������������������������������������������������������������134 Summary�����������������������������������������������������������������������������������������������������������������������������������139 ■■Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces�����������������������������141 Building Class Hierarchies��������������������������������������������������������������������������������������������������������141 Extending Classes��������������������������������������������������������������������������������������������������������������������������������������������� 142 The Ultimate Superclass������������������������������������������������������������������������������������������������������������������������������������ 148 Composition������������������������������������������������������������������������������������������������������������������������������������������������������� 157 The Trouble with Implementation Inheritance��������������������������������������������������������������������������������������������������� 158 Changing Form��������������������������������������������������������������������������������������������������������������������������162 Upcasting and Late Binding������������������������������������������������������������������������������������������������������������������������������� 163 Abstract Classes and Abstract Methods������������������������������������������������������������������������������������������������������������ 167 Downcasting and Runtime Type Identification�������������������������������������������������������������������������������������������������� 169 Covariant Return Types�������������������������������������������������������������������������������������������������������������������������������������� 172 Formalizing Class Interfaces�����������������������������������������������������������������������������������������������������174 Declaring Interfaces������������������������������������������������������������������������������������������������������������������������������������������ 174 Implementing Interfaces������������������������������������������������������������������������������������������������������������������������������������ 176 Extending Interfaces������������������������������������������������������������������������������������������������������������������������������������������ 180 www.it-ebooks.info x Contents Why Use Interfaces?������������������������������������������������������������������������������������������������������������������������������������������ 181 Summary�����������������������������������������������������������������������������������������������������������������������������������187 ■■Chapter 5: Mastering Advanced Language Features, Part 1�����������������������������������������189 Mastering Nested Types������������������������������������������������������������������������������������������������������������189 Static Member Classes�������������������������������������������������������������������������������������������������������������������������������������� 189 Nonstatic Member Classes�������������������������������������������������������������������������������������������������������������������������������� 193 Anonymous Classes������������������������������������������������������������������������������������������������������������������������������������������� 197 Local Classes����������������������������������������������������������������������������������������������������������������������������������������������������� 200 Inner Classes and Memory Leaks���������������������������������������������������������������������������������������������������������������������� 202 Interfaces within Classes and Classes within Interfaces���������������������������������������������������������������������������������� 203 Mastering Packages������������������������������������������������������������������������������������������������������������������205 What Are Packages?������������������������������������������������������������������������������������������������������������������������������������������ 206 The Package Statement������������������������������������������������������������������������������������������������������������������������������������ 207 The Import Statement���������������������������������������������������������������������������������������������������������������������������������������� 207 Searching for Packages and Types�������������������������������������������������������������������������������������������������������������������� 209 Playing with Packages��������������������������������������������������������������������������������������������������������������������������������������� 210 Packages and JAR Files������������������������������������������������������������������������������������������������������������������������������������ 215 Mastering Static Imports�����������������������������������������������������������������������������������������������������������215 Mastering Exceptions����������������������������������������������������������������������������������������������������������������217 What Are Exceptions?���������������������������������������������������������������������������������������������������������������������������������������� 218 Representing Exceptions in Source Code���������������������������������������������������������������������������������������������������������� 218 Throwing Exceptions����������������������������������������������������������������������������������������������������������������������������������������� 223 Handling Exceptions������������������������������������������������������������������������������������������������������������������������������������������ 226 Performing Cleanup������������������������������������������������������������������������������������������������������������������������������������������� 229 Summary�����������������������������������������������������������������������������������������������������������������������������������234 ■■Chapter 6: Mastering Advanced Language Features, Part 2�����������������������������������������235 Mastering Assertions�����������������������������������������������������������������������������������������������������������������235 Declaring Assertions������������������������������������������������������������������������������������������������������������������������������������������ 236 Using Assertions������������������������������������������������������������������������������������������������������������������������������������������������ 237 Avoiding Assertions������������������������������������������������������������������������������������������������������������������������������������������� 243 Enabling and Disabling Assertions�������������������������������������������������������������������������������������������������������������������� 244 www.it-ebooks.info Contents xi Mastering Annotations��������������������������������������������������������������������������������������������������������������245 Discovering Annotations������������������������������������������������������������������������������������������������������������������������������������ 245 Declaring Annotation Types and Annotating Source Code��������������������������������������������������������������������������������� 248 Processing Annotations������������������������������������������������������������������������������������������������������������������������������������� 253 Mastering Generics�������������������������������������������������������������������������������������������������������������������255 Collections and the Need for Type Safety���������������������������������������������������������������������������������������������������������� 255 Generic Types���������������������������������������������������������������������������������������������������������������������������������������������������� 258 Generic Methods������������������������������������������������������������������������������������������������������������������������������������������������ 267 Arrays and Generics������������������������������������������������������������������������������������������������������������������������������������������ 271 Mastering Enums����������������������������������������������������������������������������������������������������������������������273 The Trouble with Traditional Enumerated Types������������������������������������������������������������������������������������������������ 273 The Enum Alternative���������������������������������������������������������������������������������������������������������������������������������������� 275 The Enum Class������������������������������������������������������������������������������������������������������������������������������������������������� 280 Summary�����������������������������������������������������������������������������������������������������������������������������������284 ■■Chapter 7: Exploring the Basic APIs, Part 1������������������������������������������������������������������287 Exploring Math��������������������������������������������������������������������������������������������������������������������������287 StrictMath and strictfp��������������������������������������������������������������������������������������������������������������������������������������� 294 Exploring Number and Its Children��������������������������������������������������������������������������������������������295 BigDecimal�������������������������������������������������������������������������������������������������������������������������������������������������������� 295 BigInteger���������������������������������������������������������������������������������������������������������������������������������������������������������� 301 Primitive Type Wrapper Classes������������������������������������������������������������������������������������������������������������������������� 305 Exploring String, StringBuffer, and StringBuilder����������������������������������������������������������������������314 String����������������������������������������������������������������������������������������������������������������������������������������������������������������� 314 StringBuffer and StringBuilder�������������������������������������������������������������������������������������������������������������������������� 318 Exploring System����������������������������������������������������������������������������������������������������������������������322 Exploring Threads����������������������������������������������������������������������������������������������������������������������324 Runnable and Thread����������������������������������������������������������������������������������������������������������������������������������������� 325 Synchronization������������������������������������������������������������������������������������������������������������������������������������������������� 335 Thread-Local Variables�������������������������������������������������������������������������������������������������������������������������������������� 352 Summary�����������������������������������������������������������������������������������������������������������������������������������357 www.it-ebooks.info xii Contents ■■Chapter 8: Exploring the Basic APIs, Part 2������������������������������������������������������������������359 Exploring Random���������������������������������������������������������������������������������������������������������������������359 Exploring References����������������������������������������������������������������������������������������������������������������362 Basic Terminology��������������������������������������������������������������������������������������������������������������������������������������������� 362 Reference and ReferenceQueue������������������������������������������������������������������������������������������������������������������������ 363 SoftReference���������������������������������������������������������������������������������������������������������������������������������������������������� 365 WeakReference������������������������������������������������������������������������������������������������������������������������������������������������� 365 PhantomReference�������������������������������������������������������������������������������������������������������������������������������������������� 366 Exploring Reflection������������������������������������������������������������������������������������������������������������������368 The Class Entry Point����������������������������������������������������������������������������������������������������������������������������������������� 368 Constructor, Field, and Method�������������������������������������������������������������������������������������������������������������������������� 378 Package������������������������������������������������������������������������������������������������������������������������������������������������������������� 383 Array������������������������������������������������������������������������������������������������������������������������������������������������������������������ 388 Exploring StringTokenizer����������������������������������������������������������������������������������������������������������388 Exploring Timer and TimerTask�������������������������������������������������������������������������������������������������391 Timer in Depth��������������������������������������������������������������������������������������������������������������������������������������������������� 392 TimerTask in Depth�������������������������������������������������������������������������������������������������������������������������������������������� 395 Summary�����������������������������������������������������������������������������������������������������������������������������������399 ■■Chapter 9: Exploring the Collections Framework����������������������������������������������������������401 Exploring Collections Framework Fundamentals����������������������������������������������������������������������401 Comparable vs Comparator������������������������������������������������������������������������������������������������������������������������������ 402 Iterable and Collection��������������������������������������������������������������������������������������������������������������������������������������� 404 Exploring Lists���������������������������������������������������������������������������������������������������������������������������411 ArrayList������������������������������������������������������������������������������������������������������������������������������������������������������������ 415 LinkedList���������������������������������������������������������������������������������������������������������������������������������������������������������� 416 Exploring Sets���������������������������������������������������������������������������������������������������������������������������419 TreeSet�������������������������������������������������������������������������������������������������������������������������������������������������������������� 419 HashSet������������������������������������������������������������������������������������������������������������������������������������������������������������� 420 EnumSet������������������������������������������������������������������������������������������������������������������������������������������������������������ 425 Exploring Sorted Sets����������������������������������������������������������������������������������������������������������������428 Exploring Navigable Sets�����������������������������������������������������������������������������������������������������������435 www.it-ebooks.info Contents xiii Exploring Queues����������������������������������������������������������������������������������������������������������������������438 PriorityQueue����������������������������������������������������������������������������������������������������������������������������������������������������� 440 Exploring Deques����������������������������������������������������������������������������������������������������������������������443 ArrayDeque�������������������������������������������������������������������������������������������������������������������������������������������������������� 447 Exploring Maps��������������������������������������������������������������������������������������������������������������������������448 TreeMap������������������������������������������������������������������������������������������������������������������������������������������������������������� 452 HashMap������������������������������������������������������������������������������������������������������������������������������������������������������������ 453 IdentityHashMap������������������������������������������������������������������������������������������������������������������������������������������������ 463 WeakHashMap��������������������������������������������������������������������������������������������������������������������������������������������������� 465 EnumMap���������������������������������������������������������������������������������������������������������������������������������������������������������� 467 Exploring Sorted Maps��������������������������������������������������������������������������������������������������������������468 Exploring Navigable Maps���������������������������������������������������������������������������������������������������������471 Exploring the Arrays and Collections Utility APIs�����������������������������������������������������������������������475 Exploring the Legacy Collection APIs����������������������������������������������������������������������������������������479 Summary�����������������������������������������������������������������������������������������������������������������������������������486 ■■Chapter 10: Exploring the Concurrency Utilities�����������������������������������������������������������487 Introducing the Concurrency Utilities����������������������������������������������������������������������������������������487 Exploring Executors�������������������������������������������������������������������������������������������������������������������488 Exploring Synchronizers������������������������������������������������������������������������������������������������������������497 Countdown Latches������������������������������������������������������������������������������������������������������������������������������������������� 497 Cyclic Barriers��������������������������������������������������������������������������������������������������������������������������������������������������� 500 Exchangers�������������������������������������������������������������������������������������������������������������������������������������������������������� 505 Semaphores������������������������������������������������������������������������������������������������������������������������������������������������������ 509 Exploring the Concurrent Collections����������������������������������������������������������������������������������������515 Demonstrating BlockingQueue and ArrayBlockingQueue���������������������������������������������������������������������������������� 516 Learning More About ConcurrentHashMap�������������������������������������������������������������������������������������������������������� 518 Exploring the Locking Framework���������������������������������������������������������������������������������������������518 Lock������������������������������������������������������������������������������������������������������������������������������������������������������������������� 519 ReentrantLock��������������������������������������������������������������������������������������������������������������������������������������������������� 521 Condition����������������������������������������������������������������������������������������������������������������������������������������������������������� 524 www.it-ebooks.info xiv Contents ReadWriteLock�������������������������������������������������������������������������������������������������������������������������������������������������� 529 ReentrantReadWriteLock����������������������������������������������������������������������������������������������������������������������������������� 529 Exploring Atomic Variables��������������������������������������������������������������������������������������������������������533 Improving Performance with the Concurrency Utilities�������������������������������������������������������������535 Summary�����������������������������������������������������������������������������������������������������������������������������������538 ■■Chapter 11: Performing Classic I/O�������������������������������������������������������������������������������539 Working with the File API����������������������������������������������������������������������������������������������������������539 Constructing File Instances������������������������������������������������������������������������������������������������������������������������������� 540 Learning About Stored Abstract Pathnames������������������������������������������������������������������������������������������������������ 542 Learning About a Pathname’s File or Directory������������������������������������������������������������������������������������������������� 545 Obtaining Disk Space Information��������������������������������������������������������������������������������������������������������������������� 546 Listing Directories���������������������������������������������������������������������������������������������������������������������������������������������� 548 Creating and Manipulating Files and Directories����������������������������������������������������������������������������������������������� 550 Setting and Getting Permissions����������������������������������������������������������������������������������������������������������������������� 552 Exploring Miscellaneous Capabilities���������������������������������������������������������������������������������������������������������������� 554 Working with the RandomAccessFile API����������������������������������������������������������������������������������556 Working with Streams���������������������������������������������������������������������������������������������������������������567 Stream Classes Overview���������������������������������������������������������������������������������������������������������������������������������� 568 OutputStream and InputStream������������������������������������������������������������������������������������������������������������������������� 569 ByteArrayOutputStream and ByteArrayInputStream������������������������������������������������������������������������������������������ 572 FileOutputStream and FileInputStream������������������������������������������������������������������������������������������������������������� 573 PipedOutputStream and PipedInputStream������������������������������������������������������������������������������������������������������� 576 FilterOutputStream and FilterInputStream�������������������������������������������������������������������������������������������������������� 579 BufferedOutputStream and BufferedInputStream��������������������������������������������������������������������������������������������� 587 DataOutputStream and DataInputStream���������������������������������������������������������������������������������������������������������� 588 Object Serialization and Deserialization������������������������������������������������������������������������������������������������������������ 590 PrintStream������������������������������������������������������������������������������������������������������������������������������������������������������� 604 Standard I/O Revisited��������������������������������������������������������������������������������������������������������������������������������������� 606 Working with Writers and Readers��������������������������������������������������������������������������������������������607 Writer and Reader Classes Overview���������������������������������������������������������������������������������������������������������������� 608 Writer and Reader��������������������������������������������������������������������������������������������������������������������������������������������� 610 www.it-ebooks.info Contents xv OutputStreamWriter and InputStreamReader���������������������������������������������������������������������������������������������������� 611 FileWriter and FileReader���������������������������������������������������������������������������������������������������������������������������������� 612 Summary�����������������������������������������������������������������������������������������������������������������������������������620 ■■Chapter 12: Accessing Networks����������������������������������������������������������������������������������621 Accessing Networks via Sockets����������������������������������������������������������������������������������������������622 Socket Addresses���������������������������������������������������������������������������������������������������������������������������������������������� 624 Socket Options�������������������������������������������������������������������������������������������������������������������������������������������������� 625 Socket and ServerSocket���������������������������������������������������������������������������������������������������������������������������������� 626 DatagramSocket and MulticastSocket�������������������������������������������������������������������������������������������������������������� 632 Accessing Networks via URLs���������������������������������������������������������������������������������������������������639 URL and URLConnection������������������������������������������������������������������������������������������������������������������������������������ 639 URLEncoder and URLDecoder���������������������������������������������������������������������������������������������������������������������������� 643 URI��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 645 Accessing Network Interfaces and Interface Addresses�����������������������������������������������������������652 Managing Cookies���������������������������������������������������������������������������������������������������������������������657 Summary�����������������������������������������������������������������������������������������������������������������������������������662 ■■Chapter 13: Migrating to New I/O����������������������������������������������������������������������������������665 Working with Buffers�����������������������������������������������������������������������������������������������������������������666 Buffer and Its Children��������������������������������������������������������������������������������������������������������������������������������������� 666 Buffers in Depth������������������������������������������������������������������������������������������������������������������������������������������������� 670 Working with Channels�������������������������������������������������������������������������������������������������������������681 Channel and Its Children������������������������������������������������������������������������������������������������������������������������������������ 681 Channels in Depth��������������������������������������������������������������������������������������������������������������������������������������������� 686 Working with Selectors�������������������������������������������������������������������������������������������������������������723 Selector Fundamentals�������������������������������������������������������������������������������������������������������������������������������������� 724 Selector Demonstration������������������������������������������������������������������������������������������������������������������������������������� 728 Working with Regular Expressions��������������������������������������������������������������������������������������������732 Pattern, PatternSyntaxException, and Matcher������������������������������������������������������������������������������������������������� 732 Character Classes���������������������������������������������������������������������������������������������������������������������������������������������� 737 Capturing Groups����������������������������������������������������������������������������������������������������������������������������������������������� 738 Boundary Matchers and Zero-Length Matches������������������������������������������������������������������������������������������������� 739 www.it-ebooks.info xvi Contents Quantifiers��������������������������������������������������������������������������������������������������������������������������������������������������������� 740 Practical Regular Expressions��������������������������������������������������������������������������������������������������������������������������� 743 Working with Charsets��������������������������������������������������������������������������������������������������������������743 A Brief Review of the Fundamentals����������������������������������������������������������������������������������������������������������������� 743 Working with Charsets�������������������������������������������������������������������������������������������������������������������������������������� 744 Charsets and the String Class��������������������������������������������������������������������������������������������������������������������������� 750 Working with Formatter and Scanner���������������������������������������������������������������������������������������751 Working with Formatter������������������������������������������������������������������������������������������������������������������������������������� 752 Working with Scanner��������������������������������������������������������������������������������������������������������������������������������������� 756 Summary�����������������������������������������������������������������������������������������������������������������������������������761 ■■Chapter 14: Accessing Databases���������������������������������������������������������������������������������763 Introducing Java DB������������������������������������������������������������������������������������������������������������������764 Java DB Installation and Configuration�������������������������������������������������������������������������������������������������������������� 766 Java DB Demos�������������������������������������������������������������������������������������������������������������������������������������������������� 767 Java DB Command-Line Tools��������������������������������������������������������������������������������������������������������������������������� 769 Introducing SQLite���������������������������������������������������������������������������������������������������������������������772 Accessing Databases via JDBC�������������������������������������������������������������������������������������������������774 Data Sources, Drivers, and Connections������������������������������������������������������������������������������������������������������������ 774 Exceptions��������������������������������������������������������������������������������������������������������������������������������������������������������� 777 Statements�������������������������������������������������������������������������������������������������������������������������������������������������������� 781 Metadata����������������������������������������������������������������������������������������������������������������������������������������������������������� 794 Summary�����������������������������������������������������������������������������������������������������������������������������������801 ■■Chapter 15: Parsing, Creating, and Transforming XML Documents�������������������������������803 What Is XML?����������������������������������������������������������������������������������������������������������������������������803 XML Declaration������������������������������������������������������������������������������������������������������������������������������������������������ 805 Elements and Attributes������������������������������������������������������������������������������������������������������������������������������������ 806 Character References and CDATA Sections������������������������������������������������������������������������������������������������������� 808 Namespaces������������������������������������������������������������������������������������������������������������������������������������������������������ 809 Comment and Processing Instructions�������������������������������������������������������������������������������������������������������������� 813 Well-Formed Documents����������������������������������������������������������������������������������������������������������������������������������� 814 Valid Documents������������������������������������������������������������������������������������������������������������������������������������������������ 814 www.it-ebooks.info Contents xvii Parsing XML Documents with SAX��������������������������������������������������������������������������������������������824 Exploring the SAX API���������������������������������������������������������������������������������������������������������������������������������������� 825 Demonstrating the SAX API������������������������������������������������������������������������������������������������������������������������������� 831 Creating a Custom Entity Resolver�������������������������������������������������������������������������������������������������������������������� 840 Parsing and Creating XML Documents with DOM���������������������������������������������������������������������843 A Tree of Nodes�������������������������������������������������������������������������������������������������������������������������������������������������� 843 Exploring the DOM API��������������������������������������������������������������������������������������������������������������������������������������� 845 Parsing XML Documents with XMLPULL V1������������������������������������������������������������������������������855 Selecting XML Document Nodes with XPath�����������������������������������������������������������������������������859 XPath Language Primer������������������������������������������������������������������������������������������������������������������������������������� 859 XPath and DOM�������������������������������������������������������������������������������������������������������������������������������������������������� 863 Advanced XPath������������������������������������������������������������������������������������������������������������������������������������������������� 868 Transforming XML Documents with XSLT����������������������������������������������������������������������������������875 Exploring the XSLT API��������������������������������������������������������������������������������������������������������������������������������������� 875 Demonstrating the XSLT API������������������������������������������������������������������������������������������������������������������������������ 878 Summary�����������������������������������������������������������������������������������������������������������������������������������884 ■■Chapter 16: Focusing on Odds and Ends�����������������������������������������������������������������������885 Focusing on Additional Language Features������������������������������������������������������������������������������885 Numeric Literal Enhancements������������������������������������������������������������������������������������������������������������������������� 886 Switch-on-String����������������������������������������������������������������������������������������������������������������������������������������������� 886 Diamond Operator��������������������������������������������������������������������������������������������������������������������������������������������� 887 Multicatch���������������������������������������������������������������������������������������������������������������������������������������������������������� 887 Automatic Resource Management�������������������������������������������������������������������������������������������������������������������� 888 Focusing on Classloaders���������������������������������������������������������������������������������������������������������891 Kinds of Classloaders���������������������������������������������������������������������������������������������������������������������������������������� 891 Class-Loading Mechanics���������������������������������������������������������������������������������������������������������������������������������� 892 Playing with Classloaders���������������������������������������������������������������������������������������������������������������������������������� 893 Classloader Difficulties�������������������������������������������������������������������������������������������������������������������������������������� 897 Classloaders and Resources����������������������������������������������������������������������������������������������������������������������������� 899 Focusing on Console�����������������������������������������������������������������������������������������������������������������902 www.it-ebooks.info xviii Contents Focusing on Design Patterns�����������������������������������������������������������������������������������������������������905 Understanding Strategy������������������������������������������������������������������������������������������������������������������������������������� 906 Implementing Strategy�������������������������������������������������������������������������������������������������������������������������������������� 906 Focusing on Double Brace Initialization������������������������������������������������������������������������������������909 Focusing on Fluent Interfaces���������������������������������������������������������������������������������������������������910 Focusing on Immutability����������������������������������������������������������������������������������������������������������911 Focusing on Internationalization�����������������������������������������������������������������������������������������������914 Locales�������������������������������������������������������������������������������������������������������������������������������������������������������������� 915 Resource Bundles���������������������������������������������������������������������������������������������������������������������������������������������� 916 Break Iterators��������������������������������������������������������������������������������������������������������������������������������������������������� 935 Collators������������������������������������������������������������������������������������������������������������������������������������������������������������ 939 Dates, Time Zones, and Calendars��������������������������������������������������������������������������������������������������������������������� 941 Formatters��������������������������������������������������������������������������������������������������������������������������������������������������������� 948 Focusing on Logging�����������������������������������������������������������������������������������������������������������������958 Logging API Overview���������������������������������������������������������������������������������������������������������������������������������������� 959 A Hierarchy of Loggers�������������������������������������������������������������������������������������������������������������������������������������� 960 Logging Messages��������������������������������������������������������������������������������������������������������������������������������������������� 962 Filtering LogRecords������������������������������������������������������������������������������������������������������������������������������������������ 968 Handlers and Formatters����������������������������������������������������������������������������������������������������������������������������������� 971 LogManager and Configuration������������������������������������������������������������������������������������������������������������������������� 974 ErrorManager����������������������������������������������������������������������������������������������������������������������������������������������������� 977 Focusing on Preferences�����������������������������������������������������������������������������������������������������������981 Exploring Preferences��������������������������������������������������������������������������������������������������������������������������������������� 982 Focusing on Runtime and Process��������������������������������������������������������������������������������������������986 Focusing on the Java Native Interface��������������������������������������������������������������������������������������990 Creating a Hybrid Library����������������������������������������������������������������������������������������������������������������������������������� 990 Testing the Hybrid Library��������������������������������������������������������������������������������������������������������������������������������� 994 Focusing on ZIP and JAR�����������������������������������������������������������������������������������������������������������995 Focusing on the ZIP API������������������������������������������������������������������������������������������������������������������������������������� 995 Focusing on the JAR API���������������������������������������������������������������������������������������������������������������������������������� 1003 Summary���������������������������������������������������������������������������������������������������������������������������������1010 www.it-ebooks.info Contents xix ■■Appendix A: Solutions to Exercises�����������������������������������������������������������������������������1015 Chapter 1: Getting Started with Java��������������������������������������������������������������������������������������1015 Chapter 2: Learning Language Fundamentals�������������������������������������������������������������������������1017 Chapter 3: Discovering Classes and Objects���������������������������������������������������������������������������1020 Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces����������������������������������������1026 Chapter 5: Mastering Advanced Language Features, Part 1���������������������������������������������������1034 Chapter 6: Mastering Advanced Language Features, Part 2���������������������������������������������������1041 Chapter 7: Exploring the Basic APIs, Part 1�����������������������������������������������������������������������������1046 Chapter 8: Exploring the Basic APIs, Part 2�����������������������������������������������������������������������������1054 Chapter 9: Exploring the Collections Framework��������������������������������������������������������������������1059 Chapter 10: Exploring the Concurrency Utilities����������������������������������������������������������������������1065 Chapter 11: Performing Classic I/O�����������������������������������������������������������������������������������������1073 Chapter 12: Accessing Networks��������������������������������������������������������������������������������������������1083 Chapter 13: Migrating to New I/O��������������������������������������������������������������������������������������������1088 Chapter 14: Accessing Databases�������������������������������������������������������������������������������������������1096 Chapter 15: Parsing, Creating, and Transforming XML Documents�����������������������������������������1100 Chapter 16: Focusing on Odds and Ends���������������������������������������������������������������������������������1116 ■■Appendix B: Four of a Kind������������������������������������������������������������������������������������������1127 Understanding Four of a Kind��������������������������������������������������������������������������������������������������1127 Modeling Four of a Kind in Pseudocode����������������������������������������������������������������������������������1128 Converting Pseudocode to Java Code�������������������������������������������������������������������������������������1129 Compiling, Running, and Distributing FourOfAKind�����������������������������������������������������������������1145 Index�������������������������������������������������������������������������������������������������������������������������������1149 www.it-ebooks.info About the Author Jeff Friesen is a freelance tutor, author, and software developer with an emphasis on Java, Android, and HTML5 In addition to writing this book and its two predecessors, Jeff has written numerous articles on Java and other technologies for JavaWorld (www.javaworld.com), informIT (www.informit.com), java.net, SitePoint (www.sitepoint.com), and others Jeff can be contacted via his web site at tutortutor.ca xxi www.it-ebooks.info About the Technical Reviewer Chád Darby is an author, instructor, and speaker in the Java development world As a recognized authority on Java applications and architectures, he has presented technical sessions at software development conferences worldwide (U.S., U.K., India, Russia, and Australia) In his 15 years as a professional software architect, he’s had the opportunity to work for Blue Cross/Blue Shield, Merck, Boeing, Red Hat, and a handful of startup companies Chád is a contributing author to several Java books, including Professional Java E-Commerce (Wrox Press), Beginning Java Networking (Wrox Press), and XML and Web Services Unleashed (Sams Publishing) Chád has Java certifications from Sun Microsystems and IBM He holds a B.S in Computer Science from Carnegie Mellon University Visit Chád’s blog at www.luv2code.com to view his free video tutorials on Java You can also follow him on Twitter at @darbyluvs2code xxiii www.it-ebooks.info Acknowledgments I would like to thank Steve Anglin for contacting me to write this book, Jill Balzano and Anamika Panchoo for guiding me through the various aspects of this project, Tom Welsh and Gary Schwartz for helping me with the development of my chapters, and Chad Darby for his diligence in catching various flaws that would otherwise have made it into this book xxv www.it-ebooks.info ... 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, which are programs that run on mobile information... 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. .. applications for Android mobile devices.” Thanks for purchasing this third (and my final) edition of Learn Java for Android Development I hope you find it a helpful preparation for, and I wish

Ngày đăng: 12/03/2019, 13:19

Từ khóa liên quan

Mục lục

  • Contents at a Glance

  • Contents

  • About the Author

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

  • Chapter 1: Getting Started with Java

    • What Is Java?

      • Java Is a Language

      • Java Is a Platform

      • Java SE, Java EE, and Java ME

      • Installing the JDK and Exploring Example Applications

        • Hello, World!

        • DumpArgs

        • EchoText

        • Installing and Exploring the Eclipse IDE

        • Java Meets Android

          • What Is Android?

          • History of Android

          • Android Architecture

          • Android Says Hello

          • Summary

          • Chapter 2: Learning Language Fundamentals

            • Learning Application Structure

            • Learning Comments

              • Single-Line Comments

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

  • Đang cập nhật ...

Tài liệu liên quan