0% нашли этот документ полезным (0 голосов)
29 просмотров10 страниц

Learn Java For Android Development (Migrating Java SE Programming Skills To Mobile Development) (4th Edition) Späth

Книга 'Learn Java for Android Development: Migrating Java SE Programming Skills to Mobile Development' предназначена для разработчиков, желающих перенести свои навыки программирования на Java SE в мобильную разработку на Android. Она охватывает основы Java, архитектуру Android и предоставляет практические примеры и упражнения. Четвертое издание включает обновления и новые материалы для улучшения понимания языка и платформы.

Загружено:

niyakashi1977
Авторское право
© © All Rights Reserved
Мы серьезно относимся к защите прав на контент. Если вы подозреваете, что это ваш контент, заявите об этом здесь.
Доступные форматы
Скачать в формате PDF, TXT или читать онлайн в Scribd
0% нашли этот документ полезным (0 голосов)
29 просмотров10 страниц

Learn Java For Android Development (Migrating Java SE Programming Skills To Mobile Development) (4th Edition) Späth

Книга 'Learn Java for Android Development: Migrating Java SE Programming Skills to Mobile Development' предназначена для разработчиков, желающих перенести свои навыки программирования на Java SE в мобильную разработку на Android. Она охватывает основы Java, архитектуру Android и предоставляет практические примеры и упражнения. Четвертое издание включает обновления и новые материалы для улучшения понимания языка и платформы.

Загружено:

niyakashi1977
Авторское право
© © All Rights Reserved
Мы серьезно относимся к защите прав на контент. Если вы подозреваете, что это ваш контент, заявите об этом здесь.
Доступные форматы
Скачать в формате PDF, TXT или читать онлайн в Scribd
Вы находитесь на странице: 1/ 10

roid Development (Migrating Java SE Programming Skills to Mobile Development)

nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-

Learn Java
for Android
Development
Migrating Java SE Programming Skills
to Mobile Development

Fourth Edition

Peter Späth
Jeff Friesen

This Book is Available on YakiBooki.com


roid Development (Migrating Java SE Programming Skills to Mobile Development)

nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-

Learn Java for Android


Development
Migrating Java SE Programming
Skills to Mobile Development
Fourth Edition

Peter Späth
Jeff Friesen

This Book is Available on YakiBooki.com


roid Development (Migrating Java SE Programming Skills to Mobile Development)

Learn Java for Android Development: Migrating Java SE Programming Skills


to Mobile Development

Peter Späth Jeff Friesen


nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Leipzig, Sachsen, Germany Winnipeg, MB, Canada

ISBN-13 (pbk): 978-1-4842-5942-9 ISBN-13 (electronic): 978-1-4842-5943-6


https://doi.org/10.1007/978-1-4842-5943-6

Copyright © 2020 by Peter Späth and 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.
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.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Editorial Operations Manager: Mark Powers
Cover designed by eStudioCalamar
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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 [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles 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 Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484259429. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper

This Book is Available on YakiBooki.com


roid Development (Migrating Java SE Programming Skills to Mobile Development)

Table of Contents
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-

About the Authors����������������������������������������������������������������������������������������������������xv

About the Technical Reviewer �������������������������������������������������������������������������������xvii

Introduction ������������������������������������������������������������������������������������������������������������xix

Fourth Edition Notes �����������������������������������������������������������������������������������������������xxi

Chapter 1: Getting Started with Java ����������������������������������������������������������������������� 1


What Is Java? �������������������������������������������������������������������������������������������������������������������������������� 2
Java Is a Language ������������������������������������������������������������������������������������������������������������������ 2
Java Is a Platform �������������������������������������������������������������������������������������������������������������������� 3
Java SE and Java EE���������������������������������������������������������������������������������������������������������������� 6
Installing the JDK and Exploring Example Applications ���������������������������������������������������������������� 7
Hello, World! ���������������������������������������������������������������������������������������������������������������������������� 8
DumpArgs ������������������������������������������������������������������������������������������������������������������������������ 12
EchoText �������������������������������������������������������������������������������������������������������������������������������� 13
Installing and Exploring the Eclipse IDE �������������������������������������������������������������������������������������� 16
Java Meets Android��������������������������������������������������������������������������������������������������������������������� 21
What Is Android? ������������������������������������������������������������������������������������������������������������������� 21
History of Android ������������������������������������������������������������������������������������������������������������������ 22
Android Architecture �������������������������������������������������������������������������������������������������������������� 22
Android Says Hello ���������������������������������������������������������������������������������������������������������������� 26
Summary������������������������������������������������������������������������������������������������������������������������������������� 28

iii
This Book is Available on YakiBooki.com
roid Development (Migrating Java SE Programming Skills to Mobile Development)
Table of ConTenTs

Chapter 2: Learning Language Fundamentals �������������������������������������������������������� 31


Learning Application Structure ��������������������������������������������������������������������������������������������������� 31
Learning Comments �������������������������������������������������������������������������������������������������������������������� 33
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Single-Line Comments ���������������������������������������������������������������������������������������������������������� 33
Multiline Comments��������������������������������������������������������������������������������������������������������������� 34
Javadoc Comments ��������������������������������������������������������������������������������������������������������������� 34
Learning Identifiers ��������������������������������������������������������������������������������������������������������������������� 38
Learning Types ���������������������������������������������������������������������������������������������������������������������������� 39
Primitive Types ���������������������������������������������������������������������������������������������������������������������� 40
Object Types �������������������������������������������������������������������������������������������������������������������������� 42
Array Types ���������������������������������������������������������������������������������������������������������������������������� 43
Learning Variables ���������������������������������������������������������������������������������������������������������������������� 43
Learning Expressions ������������������������������������������������������������������������������������������������������������������ 44
Simple Expressions ��������������������������������������������������������������������������������������������������������������� 45
Compound Expressions ��������������������������������������������������������������������������������������������������������� 51
Learning Statements������������������������������������������������������������������������������������������������������������������� 80
Assignment Statements �������������������������������������������������������������������������������������������������������� 80
Decision Statements�������������������������������������������������������������������������������������������������������������� 81
Loop Statements ������������������������������������������������������������������������������������������������������������������� 87
Break Statements ������������������������������������������������������������������������������������������������������������������ 94
Continue Statements ������������������������������������������������������������������������������������������������������������� 96
Summary������������������������������������������������������������������������������������������������������������������������������������� 98

Chapter 3: Discovering Classes and Objects �������������������������������������������������������� 101


Declaring Classes ��������������������������������������������������������������������������������������������������������������������� 102
Classes and Applications ����������������������������������������������������������������������������������������������������� 102
Constructing Objects ����������������������������������������������������������������������������������������������������������������� 103
Default Constructor ������������������������������������������������������������������������������������������������������������� 105
Explicit Constructors������������������������������������������������������������������������������������������������������������ 105
Objects and Applications ����������������������������������������������������������������������������������������������������� 109

iv
This Book is Available on YakiBooki.com
roid Development (Migrating Java SE Programming Skills to Mobile Development)
Table of ConTenTs

Encapsulating State and Behaviors ������������������������������������������������������������������������������������������ 111


Representing State via Fields ���������������������������������������������������������������������������������������������� 111
Representing Behaviors via Methods ���������������������������������������������������������������������������������� 121
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Hiding Information �������������������������������������������������������������������������������������������������������������������� 136
Initializing Classes and Objects ������������������������������������������������������������������������������������������������ 142
Class Initializers ������������������������������������������������������������������������������������������������������������������ 143
Instance Initializers ������������������������������������������������������������������������������������������������������������� 144
Initialization Order ��������������������������������������������������������������������������������������������������������������� 146
Collecting Garbage �������������������������������������������������������������������������������������������������������������������� 146
Revisiting Arrays ����������������������������������������������������������������������������������������������������������������������� 147
Summary����������������������������������������������������������������������������������������������������������������������������������� 154

Chapter 4: Discovering Inheritance, Polymorphism, and Interfaces �������������������� 157


Building Class Hierarchies �������������������������������������������������������������������������������������������������������� 157
Extending Classes ��������������������������������������������������������������������������������������������������������������� 158
The Ultimate Superclass������������������������������������������������������������������������������������������������������ 165
Composition������������������������������������������������������������������������������������������������������������������������� 173
Changing Form�������������������������������������������������������������������������������������������������������������������������� 174
Upcasting and Late Binding������������������������������������������������������������������������������������������������� 176
Abstract Classes and Abstract Methods ������������������������������������������������������������������������������ 180
Downcasting and Runtime Type Identification �������������������������������������������������������������������� 182
Covariant Return Types �������������������������������������������������������������������������������������������������������� 185
Formalizing Class Interfaces ����������������������������������������������������������������������������������������������������� 186
Declaring Interfaces ������������������������������������������������������������������������������������������������������������ 186
Implementing Interfaces������������������������������������������������������������������������������������������������������ 188
Extending Interfaces ������������������������������������������������������������������������������������������������������������ 192
Why Use Interfaces? ������������������������������������������������������������������������������������������������������������ 193
Summary����������������������������������������������������������������������������������������������������������������������������������� 201

v
This Book is Available on YakiBooki.com
roid Development (Migrating Java SE Programming Skills to Mobile Development)
Table of ConTenTs

Chapter 5: Mastering Advanced Language Features, Part 1��������������������������������� 203


Mastering Nested Types ������������������������������������������������������������������������������������������������������������ 203
Static Member Classes �������������������������������������������������������������������������������������������������������� 203
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Nonstatic Member Classes �������������������������������������������������������������������������������������������������� 208
Anonymous Classes������������������������������������������������������������������������������������������������������������� 210
Local Classes ����������������������������������������������������������������������������������������������������������������������� 212
Interfaces Within Classes ���������������������������������������������������������������������������������������������������� 214
Mastering Packages ����������������������������������������������������������������������������������������������������������������� 215
What Are Packages? ������������������������������������������������������������������������������������������������������������ 215
Package Names Must Be Unique ���������������������������������������������������������������������������������������� 216
The Package Statement ������������������������������������������������������������������������������������������������������ 216
The Import Statement���������������������������������������������������������������������������������������������������������� 217
Searching for Packages and Types �������������������������������������������������������������������������������������� 218
Playing with Packages �������������������������������������������������������������������������������������������������������� 220
Packages and JAR Files ������������������������������������������������������������������������������������������������������ 225
Mastering Static Imports����������������������������������������������������������������������������������������������������������� 226
Mastering Exceptions���������������������������������������������������������������������������������������������������������������� 227
What Are Exceptions? ���������������������������������������������������������������������������������������������������������� 228
Representing Exceptions in Source Code ���������������������������������������������������������������������������� 228
Throwing Exceptions ����������������������������������������������������������������������������������������������������������� 233
Handling Exceptions ������������������������������������������������������������������������������������������������������������ 236
Performing Cleanup ������������������������������������������������������������������������������������������������������������� 241
Automatic Resource Management �������������������������������������������������������������������������������������� 244
Summary����������������������������������������������������������������������������������������������������������������������������������� 247

Chapter 6: Mastering Advanced Language Features, Part 2��������������������������������� 249


Mastering Annotations �������������������������������������������������������������������������������������������������������������� 249
Discovering Annotations ������������������������������������������������������������������������������������������������������ 249
Declaring Annotation Types and Annotating Source Code ��������������������������������������������������� 253
Processing Annotations ������������������������������������������������������������������������������������������������������� 259

vi
This Book is Available on YakiBooki.com
roid Development (Migrating Java SE Programming Skills to Mobile Development)
Table of ConTenTs

Mastering Generics ������������������������������������������������������������������������������������������������������������������� 261


Collections and the Need for Type Safety ���������������������������������������������������������������������������� 261
Generic Types ���������������������������������������������������������������������������������������������������������������������� 263
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Generic Methods ����������������������������������������������������������������������������������������������������������������� 275
Arrays and Generics ������������������������������������������������������������������������������������������������������������ 279
Mastering Enums ���������������������������������������������������������������������������������������������������������������������� 282
The Trouble with Traditional Enumerated Types ������������������������������������������������������������������ 282
The Enum Alternative ���������������������������������������������������������������������������������������������������������� 284
The Enum Class ������������������������������������������������������������������������������������������������������������������� 290
Summary����������������������������������������������������������������������������������������������������������������������������������� 296

Chapter 7: Exploring the Basic APIs, Part 1���������������������������������������������������������� 299


Exploring Math �������������������������������������������������������������������������������������������������������������������������� 299
Exploring Number and Its Children ������������������������������������������������������������������������������������������� 302
BigDecimal �������������������������������������������������������������������������������������������������������������������������� 303
BigInteger ���������������������������������������������������������������������������������������������������������������������������� 307
Primitive Type Wrapper Classes ������������������������������������������������������������������������������������������ 311
Exploring String, StringBuffer, and StringBuilder ���������������������������������������������������������������������� 319
String ����������������������������������������������������������������������������������������������������������������������������������� 319
StringBuffer and StringBuilder �������������������������������������������������������������������������������������������� 322
Exploring System ���������������������������������������������������������������������������������������������������������������������� 324
Exploring Threads ��������������������������������������������������������������������������������������������������������������������� 327
Runnable and Thread����������������������������������������������������������������������������������������������������������� 328
Synchronization ������������������������������������������������������������������������������������������������������������������� 334
Thread-Local Variables �������������������������������������������������������������������������������������������������������� 350
Summary����������������������������������������������������������������������������������������������������������������������������������� 355

Chapter 8: Exploring the Basic APIs, Part 2���������������������������������������������������������� 359


Exploring Random ��������������������������������������������������������������������������������������������������������������������� 359
Exploring Reflection ������������������������������������������������������������������������������������������������������������������ 363
The Class Entry Point����������������������������������������������������������������������������������������������������������� 363
Constructor, Field, and Method �������������������������������������������������������������������������������������������� 369

vii
This Book is Available on YakiBooki.com
roid Development (Migrating Java SE Programming Skills to Mobile Development)
Table of ConTenTs

Package ������������������������������������������������������������������������������������������������������������������������������� 373


Array ������������������������������������������������������������������������������������������������������������������������������������ 378
Exploring StringTokenizer ��������������������������������������������������������������������������������������������������������� 379
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Exploring Timer and TimerTask ������������������������������������������������������������������������������������������������� 381
Summary����������������������������������������������������������������������������������������������������������������������������������� 384

Chapter 9: Exploring the Collections Framework ������������������������������������������������� 387


Exploring Collections Framework Fundamentals ���������������������������������������������������������������������� 387
Comparable vs� Comparator ������������������������������������������������������������������������������������������������ 389
Iterable and Collection ��������������������������������������������������������������������������������������������������������� 391
Exploring Lists��������������������������������������������������������������������������������������������������������������������������� 395
ArrayList ������������������������������������������������������������������������������������������������������������������������������ 396
LinkedList ���������������������������������������������������������������������������������������������������������������������������� 397
Exploring Sets ��������������������������������������������������������������������������������������������������������������������������� 398
TreeSet �������������������������������������������������������������������������������������������������������������������������������� 398
HashSet ������������������������������������������������������������������������������������������������������������������������������� 400
EnumSet ������������������������������������������������������������������������������������������������������������������������������ 400
Exploring Sorted Sets���������������������������������������������������������������������������������������������������������������� 401
Exploring Navigable Sets ���������������������������������������������������������������������������������������������������������� 404
Exploring Queues ���������������������������������������������������������������������������������������������������������������������� 406
PriorityQueue ����������������������������������������������������������������������������������������������������������������������� 407
Exploring Deques ���������������������������������������������������������������������������������������������������������������������� 409
ArrayDeque �������������������������������������������������������������������������������������������������������������������������� 410
Exploring Maps ������������������������������������������������������������������������������������������������������������������������� 411
TreeMap ������������������������������������������������������������������������������������������������������������������������������� 414
HashMap ����������������������������������������������������������������������������������������������������������������������������� 414
IdentityHashMap������������������������������������������������������������������������������������������������������������������ 418
WeakHashMap ��������������������������������������������������������������������������������������������������������������������� 419
EnumMap ���������������������������������������������������������������������������������������������������������������������������� 419
Exploring Sorted Maps �������������������������������������������������������������������������������������������������������� 419
Exploring Navigable Maps ��������������������������������������������������������������������������������������������������� 420

viii
This Book is Available on YakiBooki.com
roid Development (Migrating Java SE Programming Skills to Mobile Development)
Table of ConTenTs

Exploring the Arrays and Collections Utility APIs����������������������������������������������������������������������� 420


Exploring the Legacy Collection APIs ���������������������������������������������������������������������������������������� 423
Summary����������������������������������������������������������������������������������������������������������������������������������� 428
nload/learn-java-for-android-development-migrating-java-se-programming-skills-to-
Chapter 10: Functional Programming ������������������������������������������������������������������ 431
Functions and Operators ����������������������������������������������������������������������������������������������������������� 432
Lambda Calculus����������������������������������������������������������������������������������������������������������������������� 432
Entering a Stream ��������������������������������������������������������������������������������������������������������������������� 433
Mapping ������������������������������������������������������������������������������������������������������������������������������������ 435
Filtering ������������������������������������������������������������������������������������������������������������������������������������� 436
Terminating a Stream���������������������������������������������������������������������������������������������������������������� 437
Performing Actions on Each Element ���������������������������������������������������������������������������������������� 437
Limiting, Skipping, Sorting, and Distinct ����������������������������������������������������������������������������������� 438
Ranges �������������������������������������������������������������������������������������������������������������������������������������� 438
Reducing ����������������������������������������������������������������������������������������������������������������������������������� 438
Collecting ���������������������������������������������������������������������������������������������������������������������������������� 439
Methods As Functions ��������������������������������������������������������������������������������������������������������������� 442
Single-Method Interfaces���������������������������������������������������������������������������������������������������������� 443
Streams and Parallelization ������������������������������������������������������������������������������������������������������ 444
Protonpack, a Stream Utility Library ����������������������������������������������������������������������������������������� 444
Summary����������������������������������������������������������������������������������������������������������������������������������� 446

Chapter 11: Exploring the Concurrency Utilities��������������������������������������������������� 449


Introducing the Concurrency Utilities ���������������������������������������������������������������������������������������� 449
Exploring Executors ������������������������������������������������������������������������������������������������������������������ 451
Exploring Synchronizers ����������������������������������������������������������������������������������������������������������� 454
Countdown Latches ������������������������������������������������������������������������������������������������������������� 454
Cyclic Barriers ��������������������������������������������������������������������������������������������������������������������� 454
Exchangers �������������������������������������������������������������������������������������������������������������������������� 455
Semaphores ������������������������������������������������������������������������������������������������������������������������ 455

ix
This Book is Available on YakiBooki.com

Вам также может понравиться