• Название:

    2013 Learn Java for Android Development 2 nd e..


  • Размер: 5.25 Мб
  • Формат: PDF
  • или
  • Сообщить о нарушении / Abuse

Установите безопасный браузер



    Предпросмотр документа

    www.it-ebooks.info

    Learn Java for Android
    Development
    Second Edition

    Jeff Friesen

    Apress

    www.it-ebooks.info

    Learn Java for Android Development
    Copyright © 2013 by Jeff Friesen
    This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
    is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
    reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
    electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
    Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material
    supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the
    purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the
    Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from
    Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are
    liable to prosecution under the respective Copyright Law.
    ISBN 978-1-4302-5722-6
    ISBN 978-1-4302-5723-3 (eBook)
    Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
    occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion
    and to the benefit of the trademark owner, with no intention of infringement of the trademark.
    The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified
    as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
    While the advice and information in this book are believed to be true and accurate at the date of publication, neither
    the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
    be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
    President and Publisher: Paul Manning
    Lead Editor: Steve Anglin
    Developmental Editors: Tom Welsh and Matthew Moodie
    Technical Reviewers: Paul Connolly, Chad Darby and Onur Cinar
    Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,
    Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham,
    Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
    Gwenan Spearing, Matt Wade, Tom Welsh
    Coordinating Editor: Katie Sullivan
    Copy Editor: Deanna K. Hegle
    Compositor: SPi Global
    Indexer: SPi Global
    Artist: SPi Global
    Cover Designer: Anna Ishchenko
    Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
    New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit
    www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science +
    Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
    For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
    Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook
    versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook
    Licensing web page at www.apress.com/bulk-sales.
    Any source code or other supplementary materials referenced by the author in this text is available to readers at
    www.apress.com. For detailed information about how to locate your book’s source code, go to
    www.apress.com/source-code/.

    www.it-ebooks.info

    To Amaury.

    www.it-ebooks.info

    Contents at a Glance
    About the Author .............................................................................................................. xvii
    About the Technical Reviewers ......................................................................................... xix
    Acknowledgments ............................................................................................................. xxi
    Introduction ..................................................................................................................... xxiii
    N Chapter 1: Getting Started With Java ................................................................................1
    N Chapter 2: Learning Language Fundamentals .................................................................21
    N Chapter 3: Discovering Classes and Objects ...................................................................63
    N Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces .............................105
    N Chapter 5: Mastering Advanced Language Features Part 1 ..........................................153
    N Chapter 6: Mastering Advanced Language Features Part 2 ..........................................197
    N Chapter 7: Exploring the Basic APIs Part 1 ...................................................................247
    N Chapter 8: Exploring the Basic APIs Part 2 ...................................................................279
    N Chapter 9: Exploring the Collections Framework ..........................................................327
    N Chapter 10: Exploring Additional Utility APIs ................................................................407
    N Chapter 11: Performing Classic I/O ...............................................................................449
    N Chapter 12: Accessing Networks ..................................................................................525

    v

    www.it-ebooks.info

    vi

    Contents at a Glance

    N Chapter 13: Migrating to New I/O ........................................................................... 561
    N Chapter 14: Accessing Databases .................................................................................603
    N Appendix A: Solutions to Exercises ...............................................................................643
    N Appendix B: Four of a Kind ............................................................................................713
    Index .................................................................................................................................735

    www.it-ebooks.info

    Contents
    About the Author .............................................................................................................. xvii
    About the Technical Reviewers ......................................................................................... xix
    Acknowledgments ............................................................................................................. xxi
    Introduction ..................................................................................................................... xxiii
    N Chapter 1: Getting Started With Java ................................................................................1
    What Is Java? ................................................................................................................................2
    Java Is a Language ................................................................................................................................................ 2
    Java Is a Platform .................................................................................................................................................. 4
    Java SE, Java EE, Java ME, and Android ............................................................................................................... 5

    Installing and Exploring the JDK ...................................................................................................6
    Installing and Exploring the Eclipse IDE ......................................................................................12
    Overview of Java APIs .................................................................................................................16
    Language-Support and Other Language-Oriented APIs ....................................................................................... 17
    Collections-Oriented APIs .................................................................................................................................... 17
    Additional Utility APIs ........................................................................................................................................... 17
    Classic I/O APIs .................................................................................................................................................... 17
    Networking APIs .................................................................................................................................................. 18

    vii

    www.it-ebooks.info

    viii

    Contents

    New I/O APIs ........................................................................................................................................................ 18
    Database APIs ...................................................................................................................................................... 18

    Summary .....................................................................................................................................19
    N Chapter 2: Learning Language Fundamentals .................................................................21
    Learning Comments ....................................................................................................................21
    Single-Line Comments ........................................................................................................................................ 22
    Multiline Comments............................................................................................................................................. 22
    Javadoc Comments ............................................................................................................................................. 23

    Learning Identifiers .....................................................................................................................25
    Learning Types ............................................................................................................................26
    Primitive Types .................................................................................................................................................... 26
    User-Defined Types .............................................................................................................................................. 28
    Array Types .......................................................................................................................................................... 28

    Learning Variables .......................................................................................................................29
    Learning Expressions ..................................................................................................................30
    Simple Expressions ............................................................................................................................................. 30
    Compound Expressions ....................................................................................................................................... 34

    Learning Statements ...................................................................................................................46
    Assignment Statements ...................................................................................................................................... 46
    Decision Statements............................................................................................................................................ 47
    Loop Statements.................................................................................................................................................. 51
    Break and Labeled Break Statements ................................................................................................................. 56
    Continue and Labeled Continue Statements........................................................................................................ 58

    Summary .....................................................................................................................................60
    N Chapter 3: Discovering Classes and Objects ...................................................................63
    Declaring Classes and Instantiating Objects ...............................................................................63
    Declaring Classes ................................................................................................................................................ 64
    Instantiating Objects with the New Operator and a Constructor ......................................................................... 64
    Specifying Constructor Parameters and Local Variables ..................................................................................... 65

    www.it-ebooks.info

    Contents

    ix

    Encapsulating State and Behaviors.............................................................................................69
    Representing State via Fields .............................................................................................................................. 70
    Representing Behaviors via Methods .................................................................................................................. 75
    Hiding Information ............................................................................................................................................... 84

    Initializing Classes and Objects ...................................................................................................89
    Class Initializers................................................................................................................................................... 89
    Instance Initializers.............................................................................................................................................. 91
    Initialization Order ............................................................................................................................................... 93

    Collecting Garbage ......................................................................................................................96
    Revisiting Arrays .........................................................................................................................99
    Summary ...................................................................................................................................104
    N Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces .............................105
    Building Class Hierarchies.........................................................................................................105
    Extending Classes ............................................................................................................................................. 106
    The Ultimate Superclass .................................................................................................................................... 112
    Composition ....................................................................................................................................................... 122
    The Trouble with Implementation Inheritance ................................................................................................... 122

    Changing Form ..........................................................................................................................126
    Upcasting and Late Binding ............................................................................................................................... 127
    Abstract Classes and Abstract Methods ............................................................................................................ 131
    Downcasting and Runtime Type Identification................................................................................................... 133
    Covariant Return Types ...................................................................................................................................... 136

    Formalizing Class Interfaces .....................................................................................................139
    Declaring Interfaces .......................................................................................................................................... 139
    Implementing Interfaces.................................................................................................................................... 140
    Extending Interfaces .......................................................................................................................................... 144
    Why Use Interfaces? .......................................................................................................................................... 146

    Summary ...................................................................................................................................152

    www.it-ebooks.info

    x

    Contents

    N Chapter 5: Mastering Advanced Language Features Part 1 ..........................................153
    Mastering Nested Types ............................................................................................................153
    Static Member Classes ...................................................................................................................................... 153
    Nonstatic Member Classes ................................................................................................................................ 157
    Anonymous Classes........................................................................................................................................... 161
    Local Classes ..................................................................................................................................................... 164
    Interfaces