ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Beginning Java
17 Fundamentals
Object-Oriented Programming in Java 17
—
Third Edition
—
Kishori Sharan
Adam L. Davis
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Beginning Java 17
Fundamentals
Object-Oriented Programming in Java 17
Third Edition
Kishori Sharan
Adam L. Davis
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17
Kishori Sharan Adam L. Davis
Montgomery, AL, USA Oviedo, FL, USA
ISBN-13 (pbk): 978-1-4842-7306-7 ISBN-13 (electronic): 978-1-4842-7307-4
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
https://doi.org/10.1007/978-1-4842-7307-4
Copyright © 2022 by Kishori Sharan and Adam L. Davis
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image by Christina Rumpf on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit
www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer
Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail
[email protected]; for reprint,
paperback, or audio rights, please e-mail
[email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484273067. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
Table of Contents
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
About the Authors��������������������������������������������������������������������������������������������������xxv
About the Technical Reviewer �����������������������������������������������������������������������������xxvii
Acknowledgments ������������������������������������������������������������������������������������������������xxix
Introduction ����������������������������������������������������������������������������������������������������������xxxi
■Chapter 1: Programming Concepts ����������������������������������������������������������������������� 1
What Is Programming? ����������������������������������������������������������������������������������������������������� 1
Components of a Programming Language ����������������������������������������������������������������������� 4
Programming Paradigms�������������������������������������������������������������������������������������������������� 4
Imperative Paradigm ������������������������������������������������������������������������������������������������������������������������������ 6
Procedural Paradigm ������������������������������������������������������������������������������������������������������������������������������ 7
Declarative Paradigm ����������������������������������������������������������������������������������������������������������������������������� 7
Functional Paradigm������������������������������������������������������������������������������������������������������������������������������� 8
Logic Paradigm ��������������������������������������������������������������������������������������������������������������������������������������� 8
Object-Oriented Paradigm ���������������������������������������������������������������������������������������������������������������������� 9
What Is Java? ����������������������������������������������������������������������������������������������������������������� 12
The Object-Oriented Paradigm and Java������������������������������������������������������������������������ 13
Abstraction ������������������������������������������������������������������������������������������������������������������������������������������� 14
Encapsulation and Information Hiding�������������������������������������������������������������������������������������������������� 23
Inheritance�������������������������������������������������������������������������������������������������������������������������������������������� 25
Polymorphism ��������������������������������������������������������������������������������������������������������������������������������������� 26
Summary ������������������������������������������������������������������������������������������������������������������������ 31
iii
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
■ Table of ConTenTs
■Chapter 2: Setting Up the Environment ��������������������������������������������������������������� 33
System Requirements ���������������������������������������������������������������������������������������������������� 33
Installing JDK 17 ������������������������������������������������������������������������������������������������������������ 33
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
The JDK Directory Structure������������������������������������������������������������������������������������������� 34
Verifying the JDK Installation ����������������������������������������������������������������������������������������� 37
Starting the JShell Tool �������������������������������������������������������������������������������������������������� 38
Installing NetBeans 12 ��������������������������������������������������������������������������������������������������� 38
Configuring NetBeans ���������������������������������������������������������������������������������������������������� 38
Summary ������������������������������������������������������������������������������������������������������������������������ 43
■Chapter 3: Writing Java Programs ���������������������������������������������������������������������� 45
The Goal Statement �������������������������������������������������������������������������������������������������������� 45
Using the JShell Tool ������������������������������������������������������������������������������������������������������ 46
What Is a Java Program? ����������������������������������������������������������������������������������������������� 46
Writing the Source Code������������������������������������������������������������������������������������������������� 47
Writing Comments ���������������������������������������������������������������������������������������������������������� 48
Declaring a Module �������������������������������������������������������������������������������������������������������� 49
Declaring Types �������������������������������������������������������������������������������������������������������������� 51
Package Declaration ����������������������������������������������������������������������������������������������������������������������������� 51
Import Declarations ������������������������������������������������������������������������������������������������������������������������������ 53
Class Declaration ���������������������������������������������������������������������������������������������������������������������������������� 54
Types Have Two Names ������������������������������������������������������������������������������������������������������������������������ 59
Compiling the Source Code �������������������������������������������������������������������������������������������� 59
Packaging the Compiled Code���������������������������������������������������������������������������������������� 62
Running a Java Program ������������������������������������������������������������������������������������������������ 64
Playing with Module Options ������������������������������������������������������������������������������������������ 70
Listing Observable Modules ����������������������������������������������������������������������������������������������������������������� 70
Limiting the Observable Modules ��������������������������������������������������������������������������������������������������������� 71
Describing a Module ����������������������������������������������������������������������������������������������������������������������������� 72
iv
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
■ Table of ConTenTs
Printing Module Resolution Details������������������������������������������������������������������������������������������������������� 73
Dry-Running Your Program ������������������������������������������������������������������������������������������������������������������� 74
Enhancing a Module Descriptor ������������������������������������������������������������������������������������� 74
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Running Java Programs in Legacy Mode ����������������������������������������������������������������������� 76
Duplicate Modules on a Module Path ����������������������������������������������������������������������������� 78
Syntax for Command-Line Options��������������������������������������������������������������������������������� 80
Writing Java Programs Using the NetBeans IDE ������������������������������������������������������������ 81
Creating a Java Project������������������������������������������������������������������������������������������������������������������������� 82
Creating Modular JARs in NetBeans ����������������������������������������������������������������������������������������������������� 90
NetBeans Project Directory Structure��������������������������������������������������������������������������������������������������� 91
Adding Classes to a Module ����������������������������������������������������������������������������������������������������������������� 91
Customizing NetBeans Project Properties �������������������������������������������������������������������������������������������� 91
Opening an Existing NetBeans Project ������������������������������������������������������������������������������������������������� 93
Behind the Scenes ��������������������������������������������������������������������������������������������������������� 94
Summary ������������������������������������������������������������������������������������������������������������������������ 97
■Chapter 4: Data Types ���������������������������������������������������������������������������������������� 101
What Is a Data Type ������������������������������������������������������������������������������������������������������ 101
What Is an Identifier? ��������������������������������������������������������������������������������������������������� 102
Keywords���������������������������������������������������������������������������������������������������������������������� 104
Data Types in Java�������������������������������������������������������������������������������������������������������� 105
Local Variable Type Inference ��������������������������������������������������������������������������������������� 110
Primitive Data Types in Java ���������������������������������������������������������������������������������������� 110
Integral Data Types ����������������������������������������������������������������������������������������������������������������������������� 111
Floating-Point Data Types ������������������������������������������������������������������������������������������������������������������� 121
Underscores in Numeric Literals ���������������������������������������������������������������������������������� 126
Java Compiler and Unicode Escape Sequence ������������������������������������������������������������ 127
A Short Break ��������������������������������������������������������������������������������������������������������������� 128
v
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
■ Table of ConTenTs
Binary Representation of Integers�������������������������������������������������������������������������������� 129
Diminished Radix Complement����������������������������������������������������������������������������������������������������������� 130
Radix Complement ����������������������������������������������������������������������������������������������������������������������������� 131
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Binary Representation of Floating-Point Numbers ������������������������������������������������������� 132
32-Bit Single-Precision Floating-Point Format ����������������������������������������������������������������������������������� 134
Special Floating-Point Numbers����������������������������������������������������������������������������������� 137
Signed Zeros��������������������������������������������������������������������������������������������������������������������������������������� 137
Signed Infinities ���������������������������������������������������������������������������������������������������������������������������������� 137
NaN ����������������������������������������������������������������������������������������������������������������������������������������������������� 138
Denormals ������������������������������������������������������������������������������������������������������������������������������������������ 139
Rounding Modes ���������������������������������������������������������������������������������������������������������� 139
Rounding Toward Zero ������������������������������������������������������������������������������������������������������������������������ 140
Rounding Toward Positive Infinity������������������������������������������������������������������������������������������������������� 140
Rounding Toward Negative Infinity ����������������������������������������������������������������������������������������������������� 140
Rounding Toward Nearest������������������������������������������������������������������������������������������������������������������� 141
IEEE Floating-Point Exceptions������������������������������������������������������������������������������������� 141
Division by Zero Exception������������������������������������������������������������������������������������������������������������������ 141
Invalid Operation Exception ���������������������������������������������������������������������������������������������������������������� 141
Overflow Exception ����������������������������������������������������������������������������������������������������������������������������� 142
Underflow Exception��������������������������������������������������������������������������������������������������������������������������� 142
Inexact Exception ������������������������������������������������������������������������������������������������������������������������������� 142
Java and IEEE Floating-Point Standards ���������������������������������������������������������������������� 143
Little-Endian and Big-Endian���������������������������������������������������������������������������������������� 143
Summary ���������������������������������������������������������������������������������������������������������������������� 144
■Chapter 5: Operators ����������������������������������������������������������������������������������������� 147
What Is an Operator?���������������������������������������������������������������������������������������������������� 147
Assignment Operator���������������������������������������������������������������������������������������������������� 149
Declaration, Initialization, and Assignment ������������������������������������������������������������������ 151
vi
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
■ Table of ConTenTs
Arithmetic Operators ���������������������������������������������������������������������������������������������������� 152
Addition Operator (+) �������������������������������������������������������������������������������������������������������������������������� 153
Subtraction Operator (-)���������������������������������������������������������������������������������������������������������������������� 155
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Multiplication Operator (*) ������������������������������������������������������������������������������������������������������������������ 156
Division Operator (/) ���������������������������������������������������������������������������������������������������������������������������� 156
Modulus Operator (%) ������������������������������������������������������������������������������������������������������������������������� 158
Unary Plus Operator (+) ���������������������������������������������������������������������������������������������������������������������� 160
Unary Minus Operator (-) �������������������������������������������������������������������������������������������������������������������� 161
Compound Arithmetic Assignment Operators ������������������������������������������������������������������������������������� 161
Increment (++) and Decrement (--) Operators ����������������������������������������������������������������������������������� 163
String Concatenation Operator (+) ������������������������������������������������������������������������������� 165
Relational Operators ����������������������������������������������������������������������������������������������������� 169
Equality Operator (==) ������������������������������������������������������������������������������������������������������������������������ 170
Inequality Operator (!=) ���������������������������������������������������������������������������������������������������������������������� 172
Greater Than Operator (>)������������������������������������������������������������������������������������������������������������������� 173
Greater Than or Equal to Operator (>=) ���������������������������������������������������������������������������������������������� 173
Less Than Operator (<) ����������������������������������������������������������������������������������������������������������������������� 174
Less Than or Equal to Operator (<=) �������������������������������������������������������������������������������������������������� 174
Boolean Logical Operators ������������������������������������������������������������������������������������������� 174
Logical NOT Operator (!) ��������������������������������������������������������������������������������������������������������������������� 175
Logical Short-Circuit AND Operator (&&) �������������������������������������������������������������������������������������������� 175
Logical AND Operator (&) �������������������������������������������������������������������������������������������������������������������� 177
Logical Short-Circuit OR Operator (||) ������������������������������������������������������������������������������������������������� 177
Logical OR Operator (|)������������������������������������������������������������������������������������������������������������������������ 178
Logical XOR Operator (^) �������������������������������������������������������������������������������������������������������������������� 178
Compound Boolean Logical Assignment Operators ���������������������������������������������������������������������������� 179
Ternary Operator (? :) ��������������������������������������������������������������������������������������������������� 179
Bitwise Operators ��������������������������������������������������������������������������������������������������������� 180
Operator Precedence���������������������������������������������������������������������������������������������������� 184
Summary ���������������������������������������������������������������������������������������������������������������������� 186
vii
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
■ Table of ConTenTs
■Chapter 6: Statements ��������������������������������������������������������������������������������������� 191
What Is a Statement? ��������������������������������������������������������������������������������������������������� 191
Types of Statements ����������������������������������������������������������������������������������������������������� 192
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Declaration Statement ������������������������������������������������������������������������������������������������������������������������ 192
Expression Statement������������������������������������������������������������������������������������������������������������������������� 192
Control Flow Statement ���������������������������������������������������������������������������������������������������������������������� 193
A Block Statement �������������������������������������������������������������������������������������������������������� 193
The if-else Statement ��������������������������������������������������������������������������������������������������� 195
The switch Statement �������������������������������������������������������������������������������������������������� 199
The switch Expression ������������������������������������������������������������������������������������������������� 203
The for Statement �������������������������������������������������������������������������������������������������������� 204
Initialization ���������������������������������������������������������������������������������������������������������������������������������������� 206
Condition Expression �������������������������������������������������������������������������������������������������������������������������� 207
Expression List ����������������������������������������������������������������������������������������������������������������������������������� 207
The for-each Statement������������������������������������������������������������������������������������������������ 210
The while Statement ���������������������������������������������������������������������������������������������������� 211
The do-while Statement ����������������������������������������������������������������������������������������������� 213
The break Statement ���������������������������������������������������������������������������������������������������� 215
The continue Statement ����������������������������������������������������������������������������������������������� 218
An Empty Statement����������������������������������������������������������������������������������������������������� 219
Summary ���������������������������������������������������������������������������������������������������������������������� 220
■Chapter 7: Classes ��������������������������������������������������������������������������������������������� 225
What Is a Class? ����������������������������������������������������������������������������������������������������������� 225
Declaring a Class���������������������������������������������������������������������������������������������������������� 226
Declaring Fields in a Class ������������������������������������������������������������������������������������������� 227
Creating Instances of a Class ��������������������������������������������������������������������������������������� 228
The null Reference Type ����������������������������������������������������������������������������������������������� 230
Using Dot Notation to Access Fields of a Class ������������������������������������������������������������ 231
Default Initialization of Fields ��������������������������������������������������������������������������������������� 234
viii
This Book is Available on YakiBooki.com
ng Java 17 Fundamentals (Object-Oriented Programming in Java 17) (3rd Edition)
■ Table of ConTenTs
Access Level Modifiers for a Class������������������������������������������������������������������������������� 235
Import Declarations ������������������������������������������������������������������������������������������������������ 238
Single-Type Import Declaration ���������������������������������������������������������������������������������������������������������� 239
ki.com/download/beginning-java-17-fundamentals-object-oriented-programming-in
Import-on-Demand Declaration ���������������������������������������������������������������������������������������������������������� 241
Import Declarations and Type Search Order ��������������������������������������������������������������������������������������� 243
Automatic Import Declarations ����������������������������������������������������������������������������������������������������������� 248
Static Import Declarations ������������������������������������������������������������������������������������������������������������������ 249
Declaring a Record ������������������������������������������������������������������������������������������������������� 253
Summary ���������������������������������������������������������������������������������������������������������������������� 254
■Chapter 8: Methods ������������������������������������������������������������������������������������������� 257
What Is a Method? ������������������������������������������������������������������������������������������������������� 257
Declaring Methods of a Class��������������������������������������������������������������������������������������� 257
Local Variables ������������������������������������������������������������������������������������������������������������� 262
Rule #1 ����������������������������������������������������������������������������������������������������������������������������������������������� 263
Rule #2 ����������������������������������������������������������������������������������������������������������������������������������������������� 263
Rule #3 ����������������������������������������������������������������������������������������������������������������������������������������������� 263
Rule #4 ����������������������������������������������������������������������������������������������������������������������������������������������� 263
Instance Methods and Class Methods�������������������������������������������������������������������������� 266
Invoking a Method �������������������������������������������������������������������������������������������������������� 267
The Special main() Method������������������������������������������������������������������������������������������� 268
What Is this?����������������������������������������������������������������������������������������������������������������� 271
Access Levels for Class Members �������������������������������������������������������������������������������� 276
Access Level: A Case Study ������������������������������������������������������������������������������������������ 283
What Is a Var-Args Method? ����������������������������������������������������������������������������������������� 288
Overloading a Var-Args Method ���������������������������������������������������������������������������������������������������������� 293
Var-Args Methods and the main() Method ������������������������������������������������������������������������������������������ 294
Parameter-Passing Mechanisms���������������������������������������������������������������������������������� 295
Pass by Value �������������������������������������������������������������������������������������������������������������������������������������� 297
Pass by Constant Value����������������������������������������������������������������������������������������������������������������������� 299
Pass by Reference ������������������������������������������������������������������������������������������������������������������������������ 299
ix
This Book is Available on YakiBooki.com