beginning java 8 fundamentals - home - springer978-1-4302-66… ·  · 2017-08-26beginning java 8...

35
Beginning Java 8 Fundamentals Language Syntax, Arrays, Data Types, Objects, and Regular Expressions Kishori Sharan

Upload: ngodung

Post on 24-May-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

Beginning Java 8 Fundamentals

Language Syntax, Arrays, Data Types, Objects, and Regular Expressions

Kishori Sharan

Page 2: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

Beginning Java 8 Fundamentals: Language Syntax, Arrays, Data Types, Objects, and Regular Expressions

Copyright © 2014 by Kishori Sharan

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-13 (pbk): 978-1-4302-6652-5

ISBN-13 (electronic): 978-1-4302-6653-2

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.

Publisher: Heinz WeinheimerLead Editor: Steve AnglinDevelopment Editor: Matthew MoodieTechnical Reviewer: John ZukowskiEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, James T. DeWolf,

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, Steve Weiss

Coordinating Editors: Anamika Panchoo, Kevin SheaCopy Editor: Mary BehrCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover 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 [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 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 material 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/.

Page 3: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

To

My parents, Ram Vinod Singh and Pratibha Devi

Page 4: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements
Page 5: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

v

Contents at a Glance

Foreword �������������������������������������������������������������������������������������������������������������������������� xxv

About the Author ������������������������������������������������������������������������������������������������������������ xxvii

About the Technical Reviewer ����������������������������������������������������������������������������������������� xxix

Acknowledgments ����������������������������������������������������������������������������������������������������������� xxxi

Introduction ������������������������������������������������������������������������������������������������������������������� xxxiii

Chapter 1: Programming Concepts ■ �����������������������������������������������������������������������������������1

Chapter 2: Writing Java Programs ■ ����������������������������������������������������������������������������������31

Chapter 3: Data Types ■ ������������������������������������������������������������������������������������������������������61

Chapter 4: Operators ■ �������������������������������������������������������������������������������������������������������99

Chapter 5: Statements ■ ���������������������������������������������������������������������������������������������������139

Chapter 6: Classes and Objects ■ �������������������������������������������������������������������������������������165

Chapter 7: The Object and Objects Classes ■ �������������������������������������������������������������������281

Chapter 8: Wrapper Classes ■ ������������������������������������������������������������������������������������������317

Chapter 9: Exception Handling ■ ��������������������������������������������������������������������������������������335

Chapter 10: Assertions ■ ��������������������������������������������������������������������������������������������������379

Chapter 11: Strings ■ ��������������������������������������������������������������������������������������������������������387

Chapter 12: Dates and Times ■ �����������������������������������������������������������������������������������������411

Chapter 13: Formatting Data ■ �����������������������������������������������������������������������������������������485

Chapter 14: Regular Expressions ■ ����������������������������������������������������������������������������������519

Chapter 15: Arrays ■ ��������������������������������������������������������������������������������������������������������543

Page 6: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents at a GlanCe

vi

Chapter 16: Inheritance ■ �������������������������������������������������������������������������������������������������583

Chapter 17: Interfaces ■ ���������������������������������������������������������������������������������������������������643

Chapter 18: Enum Types ■ ������������������������������������������������������������������������������������������������705

Appendix A: Character Encodings ■ ���������������������������������������������������������������������������������727

Appendix B: Documentation Comments ■ ������������������������������������������������������������������������739

Appendix C: Compact Profiles ■ ���������������������������������������������������������������������������������������759

Index ���������������������������������������������������������������������������������������������������������������������������������775

Page 7: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

vii

Contents

Foreword �������������������������������������������������������������������������������������������������������������������������� xxv

About the Author ������������������������������������������������������������������������������������������������������������ xxvii

About the Technical Reviewer ����������������������������������������������������������������������������������������� xxix

Acknowledgments ����������������������������������������������������������������������������������������������������������� xxxi

Introduction ������������������������������������������������������������������������������������������������������������������� xxxiii

Chapter 1: Programming Concepts ■ �����������������������������������������������������������������������������������1

What Is Programming? ������������������������������������������������������������������������������������������������������������������1

Components of a Programming Language ������������������������������������������������������������������������������������3

Programming Paradigms ���������������������������������������������������������������������������������������������������������������4

Imperative Paradigm ��������������������������������������������������������������������������������������������������������������������������������������������� 5

Procedural Paradigm ��������������������������������������������������������������������������������������������������������������������������������������������� 6

Declarative Paradigm �������������������������������������������������������������������������������������������������������������������������������������������� 7

Functional Paradigm ���������������������������������������������������������������������������������������������������������������������������������������������� 7

Logic Paradigm ������������������������������������������������������������������������������������������������������������������������������������������������������ 8

Object-Oriented Paradigm ������������������������������������������������������������������������������������������������������������������������������������� 8

What Is Java? ������������������������������������������������������������������������������������������������������������������������������11

The Object-Oriented Paradigm and Java �������������������������������������������������������������������������������������12

Abstraction ���������������������������������������������������������������������������������������������������������������������������������������������������������� 13

Encapsulation and Information Hiding ����������������������������������������������������������������������������������������������������������������� 22

Inheritance ����������������������������������������������������������������������������������������������������������������������������������������������������������� 23

Polymorphism ������������������������������������������������������������������������������������������������������������������������������������������������������ 25

Summary �������������������������������������������������������������������������������������������������������������������������������������29

Page 8: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

viii

Chapter 2: Writing Java Programs ■ ����������������������������������������������������������������������������������31

What is a Java Program? ������������������������������������������������������������������������������������������������������������31

System Requirements �����������������������������������������������������������������������������������������������������������������31

Writing the Source Code ��������������������������������������������������������������������������������������������������������������32

Package Declaration �������������������������������������������������������������������������������������������������������������������������������������������� 33

Import Declarations ��������������������������������������������������������������������������������������������������������������������������������������������� 34

Class Declaration ������������������������������������������������������������������������������������������������������������������������������������������������� 35

Compiling the Source Code ���������������������������������������������������������������������������������������������������������42

Running the Compiled Code �������������������������������������������������������������������������������������������������������43

Using NetBeans IDE ���������������������������������������������������������������������������������������������������������������������48

Creating a Java Project ���������������������������������������������������������������������������������������������������������������������������������������� 48

Adding Classes to the Project ������������������������������������������������������������������������������������������������������������������������������ 52

Customizing Project Properties ��������������������������������������������������������������������������������������������������������������������������� 54

Opening an Existing NetBeans Project ���������������������������������������������������������������������������������������������������������������� 55

Behind the Scenes ����������������������������������������������������������������������������������������������������������������������56

Summary �������������������������������������������������������������������������������������������������������������������������������������59

Chapter 3: Data Types ■ ������������������������������������������������������������������������������������������������������61

What Is a Data Type? �������������������������������������������������������������������������������������������������������������������61

What Is an Identifier? ������������������������������������������������������������������������������������������������������������������62

Data Types in Java �����������������������������������������������������������������������������������������������������������������������63

Primitive Data Types in Java �������������������������������������������������������������������������������������������������������67

Integral Data Types ���������������������������������������������������������������������������������������������������������������������������������������������� 67

Floating-Point Data Types ������������������������������������������������������������������������������������������������������������������������������������ 76

Underscores in Numeric Literals �������������������������������������������������������������������������������������������������80

Java Compiler and Unicode Escape Sequence ���������������������������������������������������������������������������81

A Short Break ������������������������������������������������������������������������������������������������������������������������������82

Binary Representation of Integers �����������������������������������������������������������������������������������������������83

Diminished Radix Complement ���������������������������������������������������������������������������������������������������������������������������� 84

Radix Complement ���������������������������������������������������������������������������������������������������������������������������������������������� 85

Page 9: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

ix

Binary Representation of Floating-Point Numbers ����������������������������������������������������������������������86

32-bit Single-Precision Floating-Point Format ���������������������������������������������������������������������������������������������������� 88

Special Floating-Point Numbers ��������������������������������������������������������������������������������������������������90

Signed Zeros �������������������������������������������������������������������������������������������������������������������������������������������������������� 90

Signed Infinities ��������������������������������������������������������������������������������������������������������������������������������������������������� 91

NaN ���������������������������������������������������������������������������������������������������������������������������������������������������������������������� 91

Denormals ����������������������������������������������������������������������������������������������������������������������������������������������������������� 92

Rounding Modes �������������������������������������������������������������������������������������������������������������������������93

Rounding Toward Zero ����������������������������������������������������������������������������������������������������������������������������������������� 93

Rounding Toward Positive Infinity ������������������������������������������������������������������������������������������������������������������������ 93

Rounding Toward Negative Infinity ���������������������������������������������������������������������������������������������������������������������� 94

Rounding Toward Nearest ������������������������������������������������������������������������������������������������������������������������������������ 94

IEEE Floating-Point Exceptions ����������������������������������������������������������������������������������������������������94

Division by Zero Exception ����������������������������������������������������������������������������������������������������������������������������������� 95

Invalid Operation Exception �������������������������������������������������������������������������������������������������������������������������������� 95

Overflow Exception ���������������������������������������������������������������������������������������������������������������������������������������������� 95

Underflow Exception ������������������������������������������������������������������������������������������������������������������������������������������� 95

Inexact Exception ����������������������������������������������������������������������������������������������������������������������������������������������� 96

Java and IEEE Floating-Point Standards �������������������������������������������������������������������������������������96

Little-Endian and Big-Endian�������������������������������������������������������������������������������������������������������96

Summary �������������������������������������������������������������������������������������������������������������������������������������97

Chapter 4: Operators ■ �������������������������������������������������������������������������������������������������������99

What Is an Operator? �������������������������������������������������������������������������������������������������������������������99

Assignment Operator (=) �����������������������������������������������������������������������������������������������������������100

Declaration, Initialization, and Assignment ������������������������������������������������������������������������������102

Arithmetic Operators �����������������������������������������������������������������������������������������������������������������103

Addition Operator (+) ����������������������������������������������������������������������������������������������������������������������������������������� 104

Subtraction Operator (-) ������������������������������������������������������������������������������������������������������������������������������������� 107

Multiplication Operator (*) ��������������������������������������������������������������������������������������������������������������������������������� 107

Division Operator (/) ������������������������������������������������������������������������������������������������������������������������������������������� 108

Page 10: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

x

Modulus Operator (%) ���������������������������������������������������������������������������������������������������������������������������������������� 110

Unary Plus Operator (+) ������������������������������������������������������������������������������������������������������������������������������������ 112

Unary Minus Operator (-) ����������������������������������������������������������������������������������������������������������������������������������� 112

Compound Arithmetic Assignment Operators ���������������������������������������������������������������������������������������������������� 113

Increment (++) and Decrement (--) Operators �������������������������������������������������������������������������������������������������� 114

String Concatenation Operator (+) ��������������������������������������������������������������������������������������������117

Relational Operators ������������������������������������������������������������������������������������������������������������������122

Equality Operator (==) ��������������������������������������������������������������������������������������������������������������������������������������� 122

Inequality Operator (!=) ������������������������������������������������������������������������������������������������������������������������������������� 124

Greater Than Operator (>)���������������������������������������������������������������������������������������������������������������������������������� 125

Greater Than or Equal to Operator (>=) ������������������������������������������������������������������������������������������������������������ 125

Less Than Operator (<) ������������������������������������������������������������������������������������������������������������������������������������� 126

Less Than or Equal to Operator (<=) ���������������������������������������������������������������������������������������������������������������� 126

Boolean Logical Operators ��������������������������������������������������������������������������������������������������������126

Logical NOT Operator (!) ������������������������������������������������������������������������������������������������������������������������������������ 127

Logical Short-Circuit AND Operator (&&) ����������������������������������������������������������������������������������������������������������� 127

Logical AND Operator (&) ����������������������������������������������������������������������������������������������������������������������������������� 129

Logical Short-Circuit OR Operator (||) ���������������������������������������������������������������������������������������������������������������� 129

Logical OR Operator (|) ��������������������������������������������������������������������������������������������������������������������������������������� 130

Logical XOR Operator (^) ����������������������������������������������������������������������������������������������������������������������������������� 130

Compound Boolean Logical Assignment Operators ������������������������������������������������������������������������������������������� 131

Ternary Operator (? :) ����������������������������������������������������������������������������������������������������������������131

Operator Precedence �����������������������������������������������������������������������������������������������������������������132

Bitwise Operators ����������������������������������������������������������������������������������������������������������������������134

Summary �����������������������������������������������������������������������������������������������������������������������������������137

Chapter 5: Statements ■ ���������������������������������������������������������������������������������������������������139

What Is a Statement? ����������������������������������������������������������������������������������������������������������������139

Types of Statements ������������������������������������������������������������������������������������������������������������������139

Declaration Statement ��������������������������������������������������������������������������������������������������������������������������������������� 139

Expression Statement ���������������������������������������������������������������������������������������������������������������������������������������� 140

Control Flow Statement ������������������������������������������������������������������������������������������������������������������������������������� 140

Page 11: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xi

A Block Statement ���������������������������������������������������������������������������������������������������������������������140

The if-else Statement ����������������������������������������������������������������������������������������������������������������142

The switch Statement ���������������������������������������������������������������������������������������������������������������146

The for Statement ���������������������������������������������������������������������������������������������������������������������150

Initialization ������������������������������������������������������������������������������������������������������������������������������������������������������� 152

Condition-expression ����������������������������������������������������������������������������������������������������������������������������������������� 152

Expression-list ��������������������������������������������������������������������������������������������������������������������������������������������������� 153

The for-each Statement �������������������������������������������������������������������������������������������������������������156

The while Statement �����������������������������������������������������������������������������������������������������������������156

The do-while Statement ������������������������������������������������������������������������������������������������������������158

The break Statement �����������������������������������������������������������������������������������������������������������������158

The continue Statement ������������������������������������������������������������������������������������������������������������160

An Empty Statement������������������������������������������������������������������������������������������������������������������162

Summary �����������������������������������������������������������������������������������������������������������������������������������163

Chapter 6: Classes and Objects ■ �������������������������������������������������������������������������������������165

What Is a Class? ������������������������������������������������������������������������������������������������������������������������165

Declaring a Class �����������������������������������������������������������������������������������������������������������������������166

Declaring Fields in a Class ��������������������������������������������������������������������������������������������������������167

Creating Instances of a Class ����������������������������������������������������������������������������������������������������168

The null Reference Type ������������������������������������������������������������������������������������������������������������170

Using Dot Notation to Access Fields of a Class �������������������������������������������������������������������������171

Default Initialization of Fields ����������������������������������������������������������������������������������������������������173

Access Level Modifiers for a Class ��������������������������������������������������������������������������������������������174

Import Declarations �������������������������������������������������������������������������������������������������������������������177

Single-Type Import Declaration ������������������������������������������������������������������������������������������������������������������������� 178

Import-on-Demand Declaration ������������������������������������������������������������������������������������������������������������������������� 179

Import Declarations and Type Search Order ������������������������������������������������������������������������������������������������������ 182

Automatic Import Declarations �������������������������������������������������������������������������������������������������������������������������� 187

Page 12: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xii

Static Import Declarations ���������������������������������������������������������������������������������������������������������188

Declaring Methods of a Class ����������������������������������������������������������������������������������������������������192

Local Variables ��������������������������������������������������������������������������������������������������������������������������196

Instance Method and Class Method ������������������������������������������������������������������������������������������199

Invoking a Method ���������������������������������������������������������������������������������������������������������������������201

The Special main( ) Method �������������������������������������������������������������������������������������������������������202

What Is this? ������������������������������������������������������������������������������������������������������������������������������205

Access Levels for Class Members ���������������������������������������������������������������������������������������������210

Access Level—A Case Study ����������������������������������������������������������������������������������������������������216

Parameter Passing Mechanisms �����������������������������������������������������������������������������������������������222

Pass By Value ���������������������������������������������������������������������������������������������������������������������������������������������������� 223

Pass By Constant Value ������������������������������������������������������������������������������������������������������������������������������������� 226

Pass By Reference ��������������������������������������������������������������������������������������������������������������������������������������������� 226

Pass By Reference Value ����������������������������������������������������������������������������������������������������������������������������������� 230

Pass by Constant Reference Value �������������������������������������������������������������������������������������������������������������������� 230

Pass by Result ��������������������������������������������������������������������������������������������������������������������������������������������������� 231

Pass by Value Result ������������������������������������������������������������������������������������������������������������������������������������������ 231

Pass By Name ���������������������������������������������������������������������������������������������������������������������������������������������������� 232

Pass by Need ����������������������������������������������������������������������������������������������������������������������������������������������������� 233

Parameter Passing Mechanisms in Java�����������������������������������������������������������������������������������233

Constructors ������������������������������������������������������������������������������������������������������������������������������245

Declaring a Constructor ������������������������������������������������������������������������������������������������������������������������������������� 245

Overloading a Constructor ��������������������������������������������������������������������������������������������������������������������������������� 247

Writing Code for a Constructor �������������������������������������������������������������������������������������������������������������������������� 249

Calling a Constructor from another Constructor ������������������������������������������������������������������������������������������������ 252

Using a return Statement Inside a Constructor ������������������������������������������������������������������������������������������������� 254

Access Level Modifier for a Constructor ������������������������������������������������������������������������������������������������������������ 254

Default Constructor�������������������������������������������������������������������������������������������������������������������������������������������� 258

A static Constructor ������������������������������������������������������������������������������������������������������������������������������������������� 259

Page 13: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xiii

Instance Initialization Block ������������������������������������������������������������������������������������������������������260

static Initialization Block �����������������������������������������������������������������������������������������������������������261

The final Keyword����������������������������������������������������������������������������������������������������������������������263

final Local Variables ������������������������������������������������������������������������������������������������������������������������������������������� 264

final Parameters ������������������������������������������������������������������������������������������������������������������������������������������������ 264

final Instance Variables�������������������������������������������������������������������������������������������������������������������������������������� 265

final Class Variables������������������������������������������������������������������������������������������������������������������������������������������� 267

final Reference Variables ����������������������������������������������������������������������������������������������������������������������������������� 268

Compile-time vs� Runtime final Variables ���������������������������������������������������������������������������������������������������������� 268

What is a varargs Method?��������������������������������������������������������������������������������������������������������269

Overloading a Varargs Method��������������������������������������������������������������������������������������������������������������������������� 274

Varargs Methods and the main( ) Method ���������������������������������������������������������������������������������������������������������� 275

Generic Classes �������������������������������������������������������������������������������������������������������������������������276

Summary �����������������������������������������������������������������������������������������������������������������������������������278

Chapter 7: The Object and Objects Classes ■ �������������������������������������������������������������������281

The Object Class ������������������������������������������������������������������������������������������������������������������������281

Rule #1 �������������������������������������������������������������������������������������������������������������������������������������������������������������� 281

Rule #2 �������������������������������������������������������������������������������������������������������������������������������������������������������������� 283

What Is the Class of an Object? �������������������������������������������������������������������������������������������������284

Computing Hash Code of an Object �������������������������������������������������������������������������������������������285

Comparing Objects for Equality �������������������������������������������������������������������������������������������������289

String Representation of an Object �������������������������������������������������������������������������������������������295

Cloning Objects �������������������������������������������������������������������������������������������������������������������������299

Finalizing an Object �������������������������������������������������������������������������������������������������������������������306

Immutable Objects ��������������������������������������������������������������������������������������������������������������������308

The Objects Class ����������������������������������������������������������������������������������������������������������������������313

Summary �����������������������������������������������������������������������������������������������������������������������������������316

Page 14: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xiv

Chapter 8: Wrapper Classes ■ ������������������������������������������������������������������������������������������317

Wrapper Classes �����������������������������������������������������������������������������������������������������������������������317

Numeric Wrapper Classes ��������������������������������������������������������������������������������������������������������������������������������� 320

The Character Wrapper Class ���������������������������������������������������������������������������������������������������������������������������� 322

The Boolean Wrapper Class ������������������������������������������������������������������������������������������������������������������������������� 323

Unsigned Numeric Operations ���������������������������������������������������������������������������������������������������324

Autoboxing and Unboxing����������������������������������������������������������������������������������������������������������326

Beware of Null Values ���������������������������������������������������������������������������������������������������������������328

Overloaded Methods and Autoboxing/Unboxing ������������������������������������������������������������������������329

Comparison Operators and AutoBoxing/Unboxing���������������������������������������������������������������������331

Collections and Autoboxing/Unboxing ���������������������������������������������������������������������������������������333

Summary �����������������������������������������������������������������������������������������������������������������������������������334

Chapter 9: Exception Handling ■ ��������������������������������������������������������������������������������������335

What Is an Exception? ���������������������������������������������������������������������������������������������������������������335

An Exception Is an Object ����������������������������������������������������������������������������������������������������������337

Using a try-catch Block �������������������������������������������������������������������������������������������������������������338

Transfer of Control ���������������������������������������������������������������������������������������������������������������������341

Exception Class Hierarchy ���������������������������������������������������������������������������������������������������������341

Arranging Multiple catch Blocks �����������������������������������������������������������������������������������������������343

Checked and Unchecked Exceptions�����������������������������������������������������������������������������������������346

Checked Exception - Catch or Declare ��������������������������������������������������������������������������������������349

Checked Exceptions and Initializers ������������������������������������������������������������������������������������������355

Throwing an Exception ��������������������������������������������������������������������������������������������������������������357

Creating an Exception Class ������������������������������������������������������������������������������������������������������357

The finally Block ������������������������������������������������������������������������������������������������������������������������361

Rethrowing an Exception ����������������������������������������������������������������������������������������������������������365

Analysis of Rethrown Exceptions ����������������������������������������������������������������������������������������������368

Throwing too Many Exceptions �������������������������������������������������������������������������������������������������368

Page 15: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xv

Accessing the Stack of a Thread �����������������������������������������������������������������������������������������������369

The try-with-resources Block ����������������������������������������������������������������������������������������������������372

A Multi-Catch Block �������������������������������������������������������������������������������������������������������������������376

Summary �����������������������������������������������������������������������������������������������������������������������������������377

Chapter 10: Assertions ■ ��������������������������������������������������������������������������������������������������379

What Is an Assertion? ����������������������������������������������������������������������������������������������������������������379

Testing Assertions ���������������������������������������������������������������������������������������������������������������������381

Enabling/Disabling Assertions ���������������������������������������������������������������������������������������������������382

Using Assertions ������������������������������������������������������������������������������������������������������������������������384

Checking for Assertion Status ���������������������������������������������������������������������������������������������������385

Summary �����������������������������������������������������������������������������������������������������������������������������������386

Chapter 11: Strings ■ ��������������������������������������������������������������������������������������������������������387

What is a String? �����������������������������������������������������������������������������������������������������������������������387

String Literals ����������������������������������������������������������������������������������������������������������������������������387

Escape Sequence Characters in String Literals ������������������������������������������������������������������������388

Unicode Escapes in String Literals ��������������������������������������������������������������������������������������������388

What is a CharSequence? ���������������������������������������������������������������������������������������������������������389

Creating String Objects �������������������������������������������������������������������������������������������������������������389

Length of a String ����������������������������������������������������������������������������������������������������������������������390

String Literals Are String Objects ����������������������������������������������������������������������������������������������390

String Objects Are Immutable ���������������������������������������������������������������������������������������������������391

Comparing Two Strings �������������������������������������������������������������������������������������������������������������392

String Pool ���������������������������������������������������������������������������������������������������������������������������������393

String Operations ����������������������������������������������������������������������������������������������������������������������395

Getting the Character at an Index ���������������������������������������������������������������������������������������������������������������������� 395

Testing Strings for Equality �������������������������������������������������������������������������������������������������������������������������������� 396

Testing a String to be Empty ������������������������������������������������������������������������������������������������������������������������������ 397

Changing the Case ��������������������������������������������������������������������������������������������������������������������������������������������� 397

Page 16: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xvi

Searching for a String ���������������������������������������������������������������������������������������������������������������������������������������� 398

Representing Values as Strings ������������������������������������������������������������������������������������������������������������������������� 398

Getting a Substring �������������������������������������������������������������������������������������������������������������������������������������������� 398

Trimming a String ���������������������������������������������������������������������������������������������������������������������������������������������� 398

Replacing Part of a String ���������������������������������������������������������������������������������������������������������������������������������� 399

Matching Start and End of a String ������������������������������������������������������������������������������������������������������������������� 399

Splitting and Joining Strings �����������������������������������������������������������������������������������������������������400

Strings in a switch Statement ���������������������������������������������������������������������������������������������������401

Testing a String for Palindrome �������������������������������������������������������������������������������������������������403

StringBuilder and StringBuffer ��������������������������������������������������������������������������������������������������404

String Concatenation Operator (+) ��������������������������������������������������������������������������������������������408

Language-Sensitive String Comparison ������������������������������������������������������������������������������������408

Summary �����������������������������������������������������������������������������������������������������������������������������������409

Chapter 12: Dates and Times ■ �����������������������������������������������������������������������������������������411

The Date-Time API ���������������������������������������������������������������������������������������������������������������������411

Design Principles �����������������������������������������������������������������������������������������������������������������������412

A Quick Example �����������������������������������������������������������������������������������������������������������������������412

Evolution of Timekeeping ����������������������������������������������������������������������������������������������������������414

Time Zones and Daylight Saving Time ��������������������������������������������������������������������������������������416

Calendar Systems ���������������������������������������������������������������������������������������������������������������������417

The Julian Calendar ������������������������������������������������������������������������������������������������������������������������������������������� 418

The Gregorian Calendar ������������������������������������������������������������������������������������������������������������������������������������� 418

ISO-8601 Standards for Datetime ���������������������������������������������������������������������������������������������419

Exploring the New Date-Time API ���������������������������������������������������������������������������������������������421

The ofXXX( ) Methods ����������������������������������������������������������������������������������������������������������������������������������������� 421

The from( ) Methods ������������������������������������������������������������������������������������������������������������������������������������������� 422

The withXXX( ) Methods ������������������������������������������������������������������������������������������������������������������������������������� 422

The getXXX( ) Methods ��������������������������������������������������������������������������������������������������������������������������������������� 422

Page 17: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xvii

The toXXX( ) Methods ����������������������������������������������������������������������������������������������������������������������������������������� 422

The atXXX( ) Methods ����������������������������������������������������������������������������������������������������������������������������������������� 423

The plusXXX( ) and minusXXX( ) Methods ����������������������������������������������������������������������������������������������������������� 423

The multipliedBy( ), dividedBy( ), and negated( ) Methods ���������������������������������������������������������������������������������� 424

Instants and Durations ��������������������������������������������������������������������������������������������������������������424

Human-Scale Time ��������������������������������������������������������������������������������������������������������������������427

The ZoneOffset Class ���������������������������������������������������������������������������������������������������������������������������������������� 427

The ZoneId Class ����������������������������������������������������������������������������������������������������������������������������������������������� 429

Useful Datetime-Related Enums ����������������������������������������������������������������������������������������������������������������������� 431

Local Date, Time, and Datetime ������������������������������������������������������������������������������������������������������������������������� 436

Offset Time and Datetime ���������������������������������������������������������������������������������������������������������������������������������� 439

Zoned Datetime ������������������������������������������������������������������������������������������������������������������������������������������������� 440

Same Instant, Different Times ���������������������������������������������������������������������������������������������������443

Clocks ����������������������������������������������������������������������������������������������������������������������������������������444

Periods ��������������������������������������������������������������������������������������������������������������������������������������445

Period Between Two Dates and Times ��������������������������������������������������������������������������������������448

Partials ��������������������������������������������������������������������������������������������������������������������������������������449

Adjusting Dates �������������������������������������������������������������������������������������������������������������������������452

Querying Datetime Objects��������������������������������������������������������������������������������������������������������457

Non-ISO Calendar Systems �������������������������������������������������������������������������������������������������������462

Formatting Dates and Times �����������������������������������������������������������������������������������������������������463

Using Predefined Formatters ����������������������������������������������������������������������������������������������������������������������������� 463

Using the format( ) Method of Datetime Classes ����������������������������������������������������������������������������������������������� 465

Using User-Defined Patterns ����������������������������������������������������������������������������������������������������������������������������� 466

Using Locale Specific Formats �������������������������������������������������������������������������������������������������������������������������� 471

Using the DateTimeFormatterBuilder Class ������������������������������������������������������������������������������������������������������� 473

Parsing Dates and Times �����������������������������������������������������������������������������������������������������������474

Page 18: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xviii

Legacy Datetime Classes ����������������������������������������������������������������������������������������������������������477

The Date Class ��������������������������������������������������������������������������������������������������������������������������������������������������� 477

The Calendar Class �������������������������������������������������������������������������������������������������������������������������������������������� 478

The add( ) Method ���������������������������������������������������������������������������������������������������������������������������������������������� 479

The roll( ) Method ����������������������������������������������������������������������������������������������������������������������������������������������� 479

Interoperability with Legacy Datetime Classes �������������������������������������������������������������������������481

Summary �����������������������������������������������������������������������������������������������������������������������������������484

Chapter 13: Formatting Data ■ �����������������������������������������������������������������������������������������485

Formatting Dates �����������������������������������������������������������������������������������������������������������������������485

Formatting Numbers �����������������������������������������������������������������������������������������������������������������492

Printf-style Formatting ��������������������������������������������������������������������������������������������������������������495

The Big Picture �������������������������������������������������������������������������������������������������������������������������������������������������� 495

The Details ��������������������������������������������������������������������������������������������������������������������������������������������������������� 498

Referencing an Argument inside a Format Specifier ����������������������������������������������������������������������������������������� 500

Using Flags in a Format Specifier ���������������������������������������������������������������������������������������������������������������������� 504

Conversion Characters �������������������������������������������������������������������������������������������������������������������������������������� 505

Summary �����������������������������������������������������������������������������������������������������������������������������������518

Chapter 14: Regular Expressions ■ ����������������������������������������������������������������������������������519

What Is a Regular Expression? ��������������������������������������������������������������������������������������������������519

Metacharacters �������������������������������������������������������������������������������������������������������������������������522

Character Classes ���������������������������������������������������������������������������������������������������������������������523

Predefined Character Classes ��������������������������������������������������������������������������������������������������������������������������� 523

More Powers to Regular Expressions ����������������������������������������������������������������������������������������524

Compiling Regular Expressions ������������������������������������������������������������������������������������������������������������������������� 524

Creating a Matcher �������������������������������������������������������������������������������������������������������������������������������������������� 526

Matching the Pattern ����������������������������������������������������������������������������������������������������������������������������������������� 526

Beware of Backslashes �������������������������������������������������������������������������������������������������������������529

Quantifiers in Regular Expressions �������������������������������������������������������������������������������������������529

Matching Boundaries ����������������������������������������������������������������������������������������������������������������530

Page 19: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xix

Groups and Back Referencing ���������������������������������������������������������������������������������������������������531

Using Named Groups �����������������������������������������������������������������������������������������������������������������535

Resetting the Matcher ���������������������������������������������������������������������������������������������������������������538

Final Words on E-mail Validations ���������������������������������������������������������������������������������������������538

Find-and-Replace Using Regular Expressions ��������������������������������������������������������������������������538

Summary �����������������������������������������������������������������������������������������������������������������������������������542

Chapter 15: Arrays ■ ��������������������������������������������������������������������������������������������������������543

What Is an Array? ����������������������������������������������������������������������������������������������������������������������543

Arrays Are Objects ���������������������������������������������������������������������������������������������������������������������544

Accessing Array Elements ���������������������������������������������������������������������������������������������������������546

Length of an Array ���������������������������������������������������������������������������������������������������������������������546

Initializing Array Elements ���������������������������������������������������������������������������������������������������������547

Beware of Reference Type Arrays����������������������������������������������������������������������������������������������549

Explicit Array Initialization ���������������������������������������������������������������������������������������������������������551

Limitations of Using Arrays �������������������������������������������������������������������������������������������������������552

Variable-Length Arrays ��������������������������������������������������������������������������������������������������������������554

Passing an Array as a Parameter ����������������������������������������������������������������������������������������������558

Array Parameter Reference ������������������������������������������������������������������������������������������������������������������������������� 562

Elements of the Array Parameter ����������������������������������������������������������������������������������������������������������������������� 563

The Object Referred by the Array Parameter Elements ������������������������������������������������������������������������������������� 564

Command-Line Arguments ��������������������������������������������������������������������������������������������������������566

Multi-Dimensional Arrays ����������������������������������������������������������������������������������������������������������570

Accessing Elements of a Multi-Dimensional Array �������������������������������������������������������������������������������������������� 573

Initializing Multi-Dimensional Arrays ����������������������������������������������������������������������������������������������������������������� 574

Enhanced for Loop for Arrays ����������������������������������������������������������������������������������������������������574

Array Declaration Syntax �����������������������������������������������������������������������������������������������������������575

Runtime Array Bounds Checks ��������������������������������������������������������������������������������������������������576

What Is the Class of an Array Object �����������������������������������������������������������������������������������������577

Page 20: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xx

Array Assignment Compatibility ������������������������������������������������������������������������������������������������579

Converting an ArrayList/Vector to an Array �������������������������������������������������������������������������������581

Summary �����������������������������������������������������������������������������������������������������������������������������������582

Chapter 16: Inheritance ■ �������������������������������������������������������������������������������������������������583

What is Inheritance? �����������������������������������������������������������������������������������������������������������������583

Object Class is the Default Superclass ��������������������������������������������������������������������������������������586

Inheritance and Hierarchical Relationship ��������������������������������������������������������������������������������587

What Is Inherited by a Subclass? ����������������������������������������������������������������������������������������������588

Upcasting and Downcasting ������������������������������������������������������������������������������������������������������589

The instanceof Operator ������������������������������������������������������������������������������������������������������������593

Binding ��������������������������������������������������������������������������������������������������������������������������������������596

Early Binding ����������������������������������������������������������������������������������������������������������������������������������������������������� 596

Late Binding ������������������������������������������������������������������������������������������������������������������������������������������������������� 599

Method Overriding ���������������������������������������������������������������������������������������������������������������������602

Method Overriding Rule #1 �������������������������������������������������������������������������������������������������������������������������������� 604

Method Overriding Rule #2 �������������������������������������������������������������������������������������������������������������������������������� 604

Method Overriding Rule #3 �������������������������������������������������������������������������������������������������������������������������������� 604

Method Overriding Rule #4 �������������������������������������������������������������������������������������������������������������������������������� 604

Method Overriding Rule #5 �������������������������������������������������������������������������������������������������������������������������������� 605

Method Overriding Rule #6 �������������������������������������������������������������������������������������������������������������������������������� 605

Accessing Overridden Method ��������������������������������������������������������������������������������������������������608

Method Overloading ������������������������������������������������������������������������������������������������������������������610

Inheritance and Constructors ����������������������������������������������������������������������������������������������������615

Method Hiding ���������������������������������������������������������������������������������������������������������������������������622

Field Hiding �������������������������������������������������������������������������������������������������������������������������������624

Disabling Inheritance ���������������������������������������������������������������������������������������������������������������628

Abstract Classes and Methods ��������������������������������������������������������������������������������������������������629

Method Overriding and Generic Method Signatures �����������������������������������������������������������������636

Typo Danger in Method Overriding ��������������������������������������������������������������������������������������������638

Page 21: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xxi

Is-a, has-a, and part-of Relationships ���������������������������������������������������������������������������������������639

No Multiple Inheritance of Classes ��������������������������������������������������������������������������������������������641

Summary �����������������������������������������������������������������������������������������������������������������������������������642

Chapter 17: Interfaces ■ ���������������������������������������������������������������������������������������������������643

What Is an Interface? ����������������������������������������������������������������������������������������������������������������643

Proposed Solution #1����������������������������������������������������������������������������������������������������������������������������������������� 645

Proposed Solution #2����������������������������������������������������������������������������������������������������������������������������������������� 647

Proposed Solution #3����������������������������������������������������������������������������������������������������������������������������������������� 647

An Ideal Solution ������������������������������������������������������������������������������������������������������������������������������������������������ 648

Declaring an Interface ���������������������������������������������������������������������������������������������������������������652

Declaring Interface Members ����������������������������������������������������������������������������������������������������653

Constant Fields Declarations ����������������������������������������������������������������������������������������������������������������������������� 653

Methods Declarations ���������������������������������������������������������������������������������������������������������������������������������������� 655

Nested Type Declarations ���������������������������������������������������������������������������������������������������������������������������������� 662

An Interface Defines a New Type �����������������������������������������������������������������������������������������������664

Implementing an Interface ��������������������������������������������������������������������������������������������������������666

Implementing Interface Methods ����������������������������������������������������������������������������������������������670

Implementing Multiple Interfaces ���������������������������������������������������������������������������������������������673

Partially Implementing an Interface ������������������������������������������������������������������������������������������676

The Supertype-Subtype Relationship ����������������������������������������������������������������������������������������677

Interface Inheritance �����������������������������������������������������������������������������������������������������������������678

The Superinterface-Subinterface Relationship �������������������������������������������������������������������������684

Inheriting Conflicting Implementations �������������������������������������������������������������������������������������685

The Superclass Always Wins ����������������������������������������������������������������������������������������������������������������������������� 685

The Most Specific Superinterface Wins ������������������������������������������������������������������������������������������������������������� 687

The Class Must Override the Conflicting Method ���������������������������������������������������������������������������������������������� 688

The instanceof Operator ������������������������������������������������������������������������������������������������������������689

Marker Interfaces ����������������������������������������������������������������������������������������������������������������������692

Functional Interfaces �����������������������������������������������������������������������������������������������������������������693

Page 22: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xxii

Comparing Objects ��������������������������������������������������������������������������������������������������������������������694

Using the Comparable Interface ������������������������������������������������������������������������������������������������������������������������ 694

Using the Comparator Interface ������������������������������������������������������������������������������������������������������������������������� 696

Polymorphism—One Object, Many Views ���������������������������������������������������������������������������������701

Dynamic Binding and Interfaces �����������������������������������������������������������������������������������������������703

Summary �����������������������������������������������������������������������������������������������������������������������������������704

Chapter 18: Enum Types ■ ������������������������������������������������������������������������������������������������705

What Is an Enum Type? �������������������������������������������������������������������������������������������������������������705

Superclass of an Enum Type �����������������������������������������������������������������������������������������������������709

Using Enum Types in switch Statements ����������������������������������������������������������������������������������712

Associating Data and Methods to Enum Constants �������������������������������������������������������������������713

Associating a Body to an Enum Constant ����������������������������������������������������������������������������������715

Comparing Two Enum Constants �����������������������������������������������������������������������������������������������719

Nested Enum Types �������������������������������������������������������������������������������������������������������������������720

Implementing an Interface to an Enum Type �����������������������������������������������������������������������������722

Reverse Lookup for Enum Constants ����������������������������������������������������������������������������������������723

Range of Enum Constants ���������������������������������������������������������������������������������������������������������724

Summary �����������������������������������������������������������������������������������������������������������������������������������725

Appendix A: Character Encodings ■ ���������������������������������������������������������������������������������727

ASCII ������������������������������������������������������������������������������������������������������������������������������������������728

8-bit Character Sets ������������������������������������������������������������������������������������������������������������������733

Universal Multiple-Octet Coded Character Set (UCS) ����������������������������������������������������������������733

UCS-2����������������������������������������������������������������������������������������������������������������������������������������������������������������� 734

UCS-4����������������������������������������������������������������������������������������������������������������������������������������������������������������� 734

UTF-16 (UCS Transformation Format 16) ����������������������������������������������������������������������������������������������������������� 734

UTF-8 (UCS Transformation Format 8) ��������������������������������������������������������������������������������������������������������������� 735

Java and Character Encodings ��������������������������������������������������������������������������������������������������735

Page 23: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xxiii

Appendix B: Documentation Comments ■ ������������������������������������������������������������������������739

Writing Documentation Comments �������������������������������������������������������������������������������������������740

List of Block and Inline Tags ������������������������������������������������������������������������������������������������������742

@author <author-name(s)> ������������������������������������������������������������������������������������������������������������������������������ 742

@deprecated <explanation-text> ��������������������������������������������������������������������������������������������������������������������� 743

@exception <class-name> <description> ������������������������������������������������������������������������������������������������������� 743

@param <parameter-name> <description> ���������������������������������������������������������������������������������������������������� 743

@return <description> �������������������������������������������������������������������������������������������������������������������������������������� 744

@see <reference> �������������������������������������������������������������������������������������������������������������������������������������������� 744

@serial <field-description or include/exclude> ������������������������������������������������������������������������������������������������ 745

@serialData <data-description> ����������������������������������������������������������������������������������������������������������������������� 746

@serialField <field-name> <field-type> <field-description> �������������������������������������������������������������������������� 746

@since <description> ��������������������������������������������������������������������������������������������������������������������������������������� 747

@throws <class-name> <description> ������������������������������������������������������������������������������������������������������������ 747

@version <version-text> ���������������������������������������������������������������������������������������������������������������������������������� 748

{@code <text>} ������������������������������������������������������������������������������������������������������������������������������������������������� 748

{@docRoot} �������������������������������������������������������������������������������������������������������������������������������������������������������� 748

{@inheritDoc}����������������������������������������������������������������������������������������������������������������������������������������������������� 748

{@link <package�class#member> <label>} ����������������������������������������������������������������������������������������������������� 750

{@linkplain <package�class#member> <label>} ���������������������������������������������������������������������������������������������� 750

{@literal <text>} ������������������������������������������������������������������������������������������������������������������������������������������������ 750

{@value <package�class#field>} ����������������������������������������������������������������������������������������������������������������������� 750

Documenting Packages �������������������������������������������������������������������������������������������������������������751

com/jdojo/utility/package-info�java file ������������������������������������������������������������������������������������������������������������� 751

com/jdojo/utility/package�html file �������������������������������������������������������������������������������������������������������������������� 752

Overview Documentation ����������������������������������������������������������������������������������������������������������752

Including Unprocessed Files in Documentation ������������������������������������������������������������������������752

Skipping Source Files Processing ���������������������������������������������������������������������������������������������752

An Example of Documentation Comments ��������������������������������������������������������������������������������753

Running the javadoc Tool ����������������������������������������������������������������������������������������������������������755

Page 24: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ Contents

xxiv

Generated Documentation Files ������������������������������������������������������������������������������������������������757

Viewing Generated HTML Documentation ���������������������������������������������������������������������������������758

Summary �����������������������������������������������������������������������������������������������������������������������������������758

Appendix C: Compact Profiles ■ ���������������������������������������������������������������������������������������759

Enhancement in the javac Command ����������������������������������������������������������������������������������������762

Enhancement in Java API Documentation ��������������������������������������������������������������������������������765

Checking for Profile Dependencies �������������������������������������������������������������������������������������������765

Creating Custom JREs Using the EJDK �������������������������������������������������������������������������������������768

Installing EJDK ��������������������������������������������������������������������������������������������������������������������������������������������������� 768

Running the jrecreate Command ����������������������������������������������������������������������������������������������������������������������� 769

Deploying the Custom JRE �������������������������������������������������������������������������������������������������������������������������������� 773

Running the Custom JRE ����������������������������������������������������������������������������������������������������������������������������������� 773

Index ���������������������������������������������������������������������������������������������������������������������������������775

Page 25: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

xxv

Foreword

Java is a very versatile programming language. It began over 20 years ago in the depths of Sun Microsystems, which has since been taken over by Oracle. The original version of Java—including the compiler, runtime, source code, and documentation—could fit onto a single 720KB floppy disk. Nowadays, the closest thing to a floppy disk one can find is one of those micro SD cards you plug into your phone, and you’ll be lucky if the whole of the Java world could fit on it. Instead of just the single desktop version of the Java platform, today you’ll find separate versions for micro devices and the enterprise, and even multiple desktop profiles. You’ll find specialized libraries for security, databases, 3D graphics, TV, messaging, expressions, e-mail, and so many other things.

This large library set is what makes the Java platform so great. You don’t need to keep reinventing the wheel. More than likely, somebody else has already done it for you. Most of the custom code done these days is for the business logic behind a project, not the management of data structures nor the marshaling and unmarshalling of data sent across the wire. This large library set is also what makes learning the Java platform so hard. There is just so much out there! You must focus on a small part of it to get started before you can tackle Java as a whole.

This book takes a focused approach to learning the fundamentals of the Java platform. Author Kishori Sharan has weeded through all there is to the core Java development environment and focuses on those parts you need to know to get yourself started with object-oriented development using the Java programming language. There is certainly much more out there than can fit in one introductory text. Until you’re comfortable with the basics, just don’t worry about the rest. You’ll learn about them soon enough, when you need to. For now, read on, and you’ll be glad you picked up this text.

—John ZukowskiJava Author and Senior Software Engineer, TripAdvisor.com

@JavaJohnZ

Page 26: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements
Page 27: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

xxvii

About the Author

Kishori Sharan is a senior software consultant at Doozer, Inc. He holds a Master of Science in Computer Information Systems from Troy State University in Montgomery, Alabama. He is a Sun Certified Java programmer and Sybase Certified PowerBuilder Developer Professional. He specializes in developing enterprise application using Java SE, Java EE, PowerBuilder, and Oracle database. He has been working in the software industry for over 16 years. He has helped several clients to migrate legacy applications to the Web. He loves writing technical books in his free time. He maintains his web site at www.jdojo.com where he posts blogs on Java and JavaFX.

Page 28: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements
Page 29: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

xxix

About the Technical Reviewer

John Zukowski is currently a software engineer with TripAdvisor, the world’s largest travel site (www.tripadvisor.com). He has been playing with Java technologies for 20 years now and is the author of 10 Java-related books. His books cover Java 6, Java Swing, Java Collections, and JBuilder from Apress, Java AWT from O’Reilly, and introductory Java from Sybex. He lives outside Boston, Massachusetts and has a Master’s degree in software engineering from The Johns Hopkins University. You can follow him on Twitter at http://twitter.com/javajohnz.

Page 30: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements
Page 31: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

xxxi

Acknowledgments

My heartfelt thanks are due to my father-in-law, Mr. Jim Baker, for displaying extraordinary patience in reading the initial draft of the book. I am very grateful to him for spending so much of his valuable time teaching me quite a bit of English grammar that helped me produce better material.

I would like to thank my friend Richard Castillo for his hard work in reading my initial draft of the book and weeding out several mistakes. Richard was instrumental in running all examples and pointing out errors.

My wife, Ellen, has always been patient when I spent long hours at my computer desk working on this book. She would happily bring me snacks, fruit, and a glass of water every 30 minutes or so to sustain me during that period. I want to thank her for all her support in writing this book. She also deserves my sincere thanks for letting me sometimes seclude myself on weekends so I could focus on this book.

I would like to thank my family members and friends for their encouragement and support for writing this book: my elder brothers, Janki Sharan and Dr. Sita Sharan; my sister and brother-in-law, Ratna and Abhay; my nephews Babalu, Dabalu, Gaurav, Saurav, and Chitranjan; my friends Shivashankar Ravindranath, Kannan Somasekar, Mahbub Choudhury, Biju Nair, Srinivas Kakkera, Anil Kumar Singh, Chris Coley, Willie Baptiste, Rahul Jain, Larry Brewster, Greg Langham, Ram Atmakuri, LaTondra Okeke, Rahul Nagpal, Ravi Datla, Prakash Chandra, and many more friends not mentioned here.

My sincere thanks are due to the wonderful team at Apress for their support during the publication of this book. Thanks to Anamika Panchoo, the Senior Coordinating Editor, for providing excellent support and for being exceptionally patient with me when I asked her so many questions in the beginning, Thanks to Matthew Moodie and John Zukovski for their technical insights and feedback during the editing process. Last but not least, my sincere thanks to Steve Anglin, the Lead Editor at Apress, for taking the initiative for the publication of this book.

Page 32: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements
Page 33: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

xxxiii

Introduction

How This Book Came AboutMy first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took a Java 2 Programmer certification examination. I did very well on the test, scoring 95 percent. The three questions that I missed on the test made me realize that the books that I had read did not adequately cover details of all the topics necessary about Java. I made up my mind to write a book on the Java programming language. So, I formulated a plan to cover most of the topics that a Java developer needs to use the Java programming language effectively in a project, as well as to get a certification. I initially planned to cover all essential topics in Java in 700 to 800 pages.

As I progressed, I realized that a book covering most of the Java topics in detail could not be written in 700 to 800 hundred pages. One chapter alone that covered data types, operators, and statements spanned 90 pages. I was then faced with the question, “Should I shorten the content of the book or include all the details that I think a Java developer needs?” I opted for including all the details in the book, rather than shortening its content to keep the number of pages low. It has never been my intent to make lots of money from this book. I was never in a hurry to finish this book because that rush could have compromised the quality and the coverage of its contents. In short, I wrote this book to help the Java community understand and use the Java programming language effectively, without having to read many books on the same subject. I wrote this book with the plan that it would be a comprehensive one-stop reference for everyone who wants to learn and grasp the intricacies of the Java programming language.

One of my high school teachers used to tell us that if one wanted to understand a building, one must first understand the bricks, steel, and mortar that make up the building. The same logic applies to most of the things that we want to understand in our lives. It certainly applies to an understanding of the Java programming language. If you want to master the Java programming language, you must start by understanding its basic building blocks. I have used this approach throughout this book, endeavoring to build each topic by describing the basics first. In the book, you will rarely find a topic described without first learning its background. Wherever possible, I have tried to correlate the programming practices with activities in our daily life. Most of the books about the Java programming language available in the market either do not include any pictures at all or have only a few. I believe in the adage, “A picture is worth a thousand words.” To a reader, a picture makes a topic easier to understand and remember. I have included plenty of illustrations in the book to aid readers in understanding and visualizing the contents. Developers who have little or no programming experience have difficulty in putting things together to make it a complete program. Keeping them in mind, the book contains over 240 complete Java programs that are ready to be compiled and run.

I spent countless hours doing research for writing this book. My main source of research was the Java Language Specification, white papers and articles on Java topics, and Java Specification Requests (JSRs). I also spent quite a bit of time reading the Java source code to learn more about some of the Java topics. Sometimes, it took a few months researching a topic before I could write the first sentence on the topic. Finally, it was always fun to play with Java programs, sometimes for hours, to add them to the book.

Page 34: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ IntroduCtIon

xxxiv

Structure of the BookThis book contains 18 chapters and three appendixes. The chapters contain fundamental topics of Java such as syntax, data types, operators, classes, objects, etc. The chapters are arranged in an order that aids learning the Java programming language faster. The first chapter, “Programming Concepts,” explains basic concepts related to programming in general, without going into too much technical details; it introduces Java and its features. The second chapter, “Writing Java Programs,” introduces the first program using Java; this chapter is especially written for those learning Java for the first time. Subsequent chapters introduce Java topics in an increasing order of complexity. The new features of Java 8 are included wherever they fit in the chapter. The new Date-Time API, which is one of the biggest addition in Java 8, has been discussed in great detail in over 80 pages in Chapter 12.

After finishing this book, to take your Java knowledge to the next level, two companion books are available by the author: Beginning Java 8 Language Features (ISBN 978-1-4302-6658-7) and Beginning Java 8 APIs, Extensions, and Libraries (ISBN 978-1-4302-6661-7).

AudienceThis book is designed to be useful for anyone who wants to learn the Java programming language. If you are a beginner, with little or no programming background, you need to read from the first chapter to the last, in order. The book contains topics of various degrees of complexity. As a beginner, if you find yourself overwhelmed while reading a section in a chapter, you can skip to the next section or the next chapter, and revisit it later when you gain more experience.

If you are a Java developer with an intermediate or advanced level of experience, you can jump to a chapter or to a section in a chapter directly. If a section uses an unfamiliar topic, you need to visit that topic before continuing the current one.

If you are reading this book to get a certification in the Java programming language, you need to read almost all of the chapters, paying attention to all the detailed descriptions and rules. Most of the certification programs test your fundamental knowledge of the language, not the advanced knowledge. You need to read only those topics that are part of your certification test. Compiling and running over 240 complete Java programs will help you prepare for your certification.

If you are a student who is attending a class in the Java programming language, you need to read the first six chapters of this book thoroughly. These chapters cover the basics of the Java programming languages in detail. You cannot do well in a Java class unless you first master the basics. After covering the basics, you need to read only those chapters that are covered in your class syllabus. I am sure, you, as a Java student, do not need to read the entire book page-by-page.

How to Use This BookThis book is the beginning, not the end, for you to gain the knowledge of the Java programming language. If you are reading this book, it means you are heading in the right direction to learn the Java programming language that will enable you to excel in your academic and professional career. However, there is always a higher goal for you to achieve and you must constantly work harder to achieve it. The following quotations from some great thinkers may help you understand the importance of working hard and constantly looking for knowledge with both your eyes and mind open.

The learning and knowledge that we have, is, at the most, but little compared with that of which we are ignorant.

—Plato

True knowledge exists in knowing that you know nothing. And in knowing that you know nothing, that makes you the smartest of all.

—Socrates

Page 35: Beginning Java 8 Fundamentals - Home - Springer978-1-4302-66… ·  · 2017-08-26Beginning Java 8 Fundamentals Language Syntax, Arrays, ... Chapter 3: Data Types ... Chapter 5: Statements

■ IntroduCtIon

xxxv

Readers are advised to use the API documentation for the Java programming language, as much as possible, while using this book. The Java API documentation is the place where you will find a complete list of documentation for everything available in the Java class library. You can download (or view) the Java API documentation from the official web site of Oracle Corporation at www.oracle.com. While you read this book, you need to practice writing Java programs yourself. You can also practice by tweaking the programs provided in the book. It does not help much in your learning process if you just read this book and do not practice by writing your own programs. Remember that “practice makes perfect,” which is also true in learning how to program in Java.

Source Code and ErrataSource code and errata for this book may be downloaded from www.apress.com/source-code.

Questions and CommentsPlease direct all your questions and comments for the author to [email protected].