ebook img

Learn Java for Android Development PDF

655 Pages·2010·9.02 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn Java for Android Development

Get the Java skills and know-how that you’ll need to learn and write successful Android apps Learn Java Android for Development Jeff “JavaJeff” Friesen m o 3.c e bl a ari @v mv a h ars h by d e as h urc p as w ok o b his T Learn Java for Android Development ■ ■ ■ Jeff “JavaJeff” Friesen i Learn Java for Android Development Copyright © 2010 by Jeff “JavaJeff” Friesen All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3156-1 ISBN-13 (electronic): 978-1-4302-3157-8 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Tom Welsh Technical Reviewer: Paul Connolly Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editor: Bill McManus Compositor: MacPS, LLC Indexer: John Collin Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com/book/view/1430231564. ii To my best friend iii Contents at a Glance ■Contents..........................................................................................................v ■About the Author.............................................................................................x ■About the Technical Reviewer........................................................................xi ■Acknowledgments.........................................................................................xii ■Introduction..................................................................................................xiii(cid:1) ■Chapter 1: Getting Started with Java..............................................................1(cid:1) ■Chapter 2: Learning Language Fundamentals...............................................43(cid:1) ■Chapter 3: Learning Object-Oriented Language Features..............................97(cid:1) ■Chapter 4: Mastering Advanced Language Features Part 1........................139(cid:1) ■Chapter 5: Mastering Advanced Language Features Part 2........................181(cid:1) ■Chapter 6: Exploring the Basic APIs Part 1.................................................227(cid:1) ■Chapter 7: Exploring the Basic APIs Part 2.................................................269(cid:1) ■Chapter 8: Discovering the Collections Framework....................................315(cid:1) ■Chapter 9: Discovering Additional Utility APIs............................................381(cid:1) ■Chapter 10: Performing I/O.........................................................................449(cid:1) ■Appendix: Solutions to Exercises................................................................533 ■Index............................................................................................................595 iv Contents ■Contents at a Glance.......................................................................................iv(cid:1) ■About the Author.............................................................................................x(cid:1) ■About the Technical Reviewer........................................................................xi(cid:1) ■Acknowledgments.........................................................................................xii(cid:1) ■Introduction..................................................................................................xiii(cid:1) ■Chapter 1: Getting Started with Java..............................................................1(cid:1) What Is Java?..........................................................................................................................................................1(cid:1) Java Is a Language............................................................................................................................................2(cid:1) Java Is a Platform..............................................................................................................................................3(cid:1) Java SE, Java EE, Java ME, and Android...........................................................................................................5(cid:1) Installing and Exploring the JDK.............................................................................................................................6(cid:1) Installing and Exploring Two Popular IDEs............................................................................................................12(cid:1) NetBeans IDE...................................................................................................................................................13(cid:1) Eclipse IDE.......................................................................................................................................................17(cid:1) Four of a Kind........................................................................................................................................................20(cid:1) Understanding Four of a Kind..........................................................................................................................21(cid:1) Modeling Four of a Kind in Pseudocode..........................................................................................................21(cid:1) Converting Pseudocode to Java Code..............................................................................................................23(cid:1) Compiling, Running, and Distributing FourOfAKind.........................................................................................37(cid:1) Summary..............................................................................................................................................................41(cid:1) ■Chapter 2: Learning Language Fundamentals...............................................43(cid:1) Classes..................................................................................................................................................................43(cid:1) Declaring Classes............................................................................................................................................44(cid:1) Introducing Fields............................................................................................................................................45(cid:1) Introducing Methods........................................................................................................................................58(cid:1) Introducing Constructors.................................................................................................................................75(cid:1) Introducing Other Initializers............................................................................................................................76(cid:1) Interface Versus Implementation.....................................................................................................................82 v ■ CONTENTS Objects..................................................................................................................................................................85(cid:1) Creating Objects and Arrays............................................................................................................................85(cid:1) Accessing Fields..............................................................................................................................................87(cid:1) Calling Methods...............................................................................................................................................89(cid:1) Garbage Collection...........................................................................................................................................92(cid:1) Summary..............................................................................................................................................................94(cid:1) ■Chapter 3: Learning Object-Oriented Language Features..............................97(cid:1) Inheritance............................................................................................................................................................97(cid:1) Extending Classes............................................................................................................................................98(cid:1) The Ultimate Superclass................................................................................................................................103(cid:1) Composition...................................................................................................................................................112(cid:1) The Trouble with Implementation Inheritance...............................................................................................112(cid:1) Polymorphism.....................................................................................................................................................116(cid:1) Upcasting and Late Binding...........................................................................................................................117(cid:1) Abstract Classes and Abstract Methods........................................................................................................120(cid:1) Downcasting and Runtime Type Identification..............................................................................................121(cid:1) Covariant Return Types..................................................................................................................................123(cid:1) Interfaces............................................................................................................................................................125(cid:1) Declaring Interfaces.......................................................................................................................................125(cid:1) Implementing Interfaces................................................................................................................................127(cid:1) Extending Interfaces......................................................................................................................................130(cid:1) Why Use Interfaces?......................................................................................................................................131(cid:1) Summary............................................................................................................................................................137(cid:1) ■Chapter 4: Mastering Advanced Language Features Part 1........................139(cid:1) Nested Types......................................................................................................................................................139(cid:1) Static Member Classes..................................................................................................................................139(cid:1) Nonstatic Member Classes............................................................................................................................142(cid:1) Anonymous Classes.......................................................................................................................................146(cid:1) Local Classes.................................................................................................................................................148(cid:1) Interfaces Within Classes..............................................................................................................................150(cid:1) Packages............................................................................................................................................................151(cid:1) What Are Packages?......................................................................................................................................151(cid:1) The Package Statement.................................................................................................................................152(cid:1) The Import Statement....................................................................................................................................153(cid:1) Searching for Packages and Types................................................................................................................154(cid:1) Playing with Packages...................................................................................................................................155(cid:1) Packages and JAR Files.................................................................................................................................159(cid:1) Static Imports.....................................................................................................................................................160(cid:1) Exceptions..........................................................................................................................................................161(cid:1) What Are Exceptions?....................................................................................................................................162(cid:1) Representing Exceptions in Source Code......................................................................................................162(cid:1) Throwing Exceptions......................................................................................................................................166(cid:1) Handling Exceptions......................................................................................................................................168(cid:1) Performing Cleanup.......................................................................................................................................172(cid:1) Summary............................................................................................................................................................178(cid:1) ■Chapter 5: Mastering Advanced Language Features Part 2........................181(cid:1) Assertions...........................................................................................................................................................181(cid:1) vi ■ CONTENTS Declaring Assertions......................................................................................................................................182(cid:1) Using Assertions............................................................................................................................................183(cid:1) Avoiding Assertions.......................................................................................................................................188(cid:1) Enabling and Disabling Assertions.................................................................................................................189(cid:1) Annotations.........................................................................................................................................................190(cid:1) Discovering Annotations................................................................................................................................190(cid:1) Declaring Annotation Types and Annotating Source Code.............................................................................193(cid:1) Processing Annotations.................................................................................................................................198(cid:1) Generics..............................................................................................................................................................200(cid:1) Collections and the Need for Type Safety......................................................................................................200(cid:1) Generic Types................................................................................................................................................202(cid:1) Generic Methods............................................................................................................................................212(cid:1) Enums.................................................................................................................................................................214(cid:1) The Trouble with Traditional Enumerated Types...........................................................................................214(cid:1) The Enum Alternative.....................................................................................................................................215(cid:1) The Enum Class.............................................................................................................................................220(cid:1) Summary............................................................................................................................................................225(cid:1) ■Chapter 6: Exploring the Basic APIs Part 1.................................................227(cid:1) Math APIs............................................................................................................................................................227(cid:1) Math and StrictMath......................................................................................................................................227(cid:1) BigDecimal.....................................................................................................................................................234(cid:1) BigInteger......................................................................................................................................................239(cid:1) Package Information...........................................................................................................................................243(cid:1) Primitive Wrapper Classes..................................................................................................................................247(cid:1) Boolean..........................................................................................................................................................248(cid:1) Character.......................................................................................................................................................250(cid:1) Float and Double............................................................................................................................................251(cid:1) Integer, Long, Short, and Byte.......................................................................................................................255(cid:1) Number..........................................................................................................................................................257(cid:1) References API....................................................................................................................................................257(cid:1) Basic Terminology.........................................................................................................................................257(cid:1) Reference and ReferenceQueue....................................................................................................................259(cid:1) SoftReference................................................................................................................................................260(cid:1) WeakReference..............................................................................................................................................263(cid:1) PhantomReference........................................................................................................................................263(cid:1) Summary............................................................................................................................................................268(cid:1) ■Chapter 7: Exploring the Basic APIs Part 2.................................................269(cid:1) Reflection API......................................................................................................................................................269(cid:1) String Management............................................................................................................................................277(cid:1) String.............................................................................................................................................................278(cid:1) StringBuffer...................................................................................................................................................281(cid:1) System................................................................................................................................................................284(cid:1) Threading API......................................................................................................................................................287(cid:1) Runnable and Thread.....................................................................................................................................288(cid:1) Thread Synchronization.................................................................................................................................296(cid:1) Summary............................................................................................................................................................313(cid:1) ■Chapter 8: Discovering the Collections Framework....................................315(cid:1) vii ■ CONTENTS Framework Overview..........................................................................................................................................315(cid:1) Comparable Versus Comparator....................................................................................................................316(cid:1) Iterable and Collection........................................................................................................................................318(cid:1) Iterator and the Enhanced For Loop Statement.............................................................................................321(cid:1) Autoboxing and Unboxing..............................................................................................................................323(cid:1) List......................................................................................................................................................................325(cid:1) ArrayList.........................................................................................................................................................329(cid:1) LinkedList......................................................................................................................................................330(cid:1) Set.......................................................................................................................................................................332(cid:1) TreeSet..........................................................................................................................................................332(cid:1) HashSet..........................................................................................................................................................333(cid:1) EnumSet........................................................................................................................................................337(cid:1) SortedSet............................................................................................................................................................339(cid:1) Queue..................................................................................................................................................................346(cid:1) PriorityQueue.................................................................................................................................................348(cid:1) Map.....................................................................................................................................................................351(cid:1) TreeMap.........................................................................................................................................................355(cid:1) HashMap........................................................................................................................................................356(cid:1) IdentityHashMap............................................................................................................................................362(cid:1) WeakHashMap...............................................................................................................................................364(cid:1) EnumMap.......................................................................................................................................................366(cid:1) SortedMap..........................................................................................................................................................367(cid:1) Utilities................................................................................................................................................................369(cid:1) Classic Collections Classes.................................................................................................................................372(cid:1) Summary............................................................................................................................................................379(cid:1) ■Chapter 9: Discovering Additional Utility APIs............................................381(cid:1) Concurrency Utilities...........................................................................................................................................381(cid:1) Executors.......................................................................................................................................................381(cid:1) Synchronizers................................................................................................................................................390(cid:1) Concurrent Collections...................................................................................................................................392(cid:1) Locks.............................................................................................................................................................394(cid:1) Atomic Variables............................................................................................................................................397(cid:1) Internationalization APIs.....................................................................................................................................397(cid:1) Locales...........................................................................................................................................................398(cid:1) Resource Bundles..........................................................................................................................................400(cid:1) Break Iterators...............................................................................................................................................409(cid:1) Collators.........................................................................................................................................................413(cid:1) Dates, Time Zones, and Calendars................................................................................................................415(cid:1) Formatters.....................................................................................................................................................421(cid:1) Preferences API..................................................................................................................................................428(cid:1) Random Number Generation...............................................................................................................................432(cid:1) Regular Expressions API.....................................................................................................................................434(cid:1) Summary............................................................................................................................................................447(cid:1) ■Chapter 10: Performing I/O.........................................................................449(cid:1) File......................................................................................................................................................................449(cid:1) RandomAccessFile..............................................................................................................................................462(cid:1) Streams..............................................................................................................................................................473(cid:1) viii

Description:
Java forAndroid. Development. Get the Java skills and know-how that you'll need to learn and write successful Android apps. Jeff “JavaJeff” Friesen
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.