Beginning Java 9 Fundamentals (eBook)
XXXVI, 1037 Seiten
Apress (Verlag)
978-1-4842-2902-6 (ISBN)
- Write your first Java programs with an emphasis on learning object-oriented programming in Java
- Work with data types, operators, statements, classes and objects
- Handle exceptions, assertions, strings and dates, and object formatting
- Use regular expressions
- Work with arrays, interfaces, enums, and inheritance
- Deploy Java applications on memory-constrained devices using compact profiles
- Take advantage of the new JShell REPL tool
Kishori Sharan has earned a Master of Science in Computer Information Systems degree from Troy State University, Alabama. He is a Sun Certified Java 2 programmer. He has vast experience in providing training to professional developers in Java, JSP, EJB, and Web technology. He possesses over ten years of experience in implementing enterprise level Java application.
Learn the basics of Java 9, including basic programming concepts and the object-oriented fundamentals necessary at all levels of Java development. Author Kishori Sharan walks you through writing your first Java program step-by-step. Armed with that practical experience, you'll be ready to learn the core of the Java language. Beginning Java 9 Fundamentals provides over 90 diagrams and 240 complete programs to help you learn the topics faster.The book continues with a series of foundation topics, including using data types, working with operators, and writing statements in Java. These basics lead onto the heart of the Java language: object-oriented programming. By learning topics such as classes, objects, interfaces, and inheritance you'll have a good understanding ofJava's object-oriented model.The final collection of topics takes what you've learned and turns you into a real Java programmer. You'll see how to take the power of object-oriented programming and write programs that can handle errors and exceptions, process strings and dates, format data, and work with arrays to manipulate data.This book is a companion to two other books also by Sharan focusing on APIs and advanced Java topics.What You'll LearnWrite your first Java programs with an emphasis on learning object-oriented programming in JavaWork with data types, operators, statements, classes and objectsHandle exceptions, assertions, strings and dates, and object formattingUse regular expressions Work with arrays, interfaces, enums, and inheritanceTake advantage of the new JShell REPL toolWho This Book Is ForThose who are new to Java programming, who may have some or even no prior programming experience.
Kishori Sharan has earned a Master of Science in Computer Information Systems degree from Troy State University, Alabama. He is a Sun Certified Java 2 programmer. He has vast experience in providing training to professional developers in Java, JSP, EJB, and Web technology. He possesses over ten years of experience in implementing enterprise level Java application.
Contents at a Glance 4
Contents 6
About the Author 27
About the Technical Reviewer 28
Acknowledgments 29
Introduction 30
Chapter 1: Programming Concepts 34
What Is Programming? 34
Components of a Programming Language 37
Programming Paradigms 37
Imperative Paradigm 39
Procedural Paradigm 39
Declarative Paradigm 40
Functional Paradigm 41
Logic Paradigm 41
Object-Oriented Paradigm 42
What Is Java? 45
The Object-Oriented Paradigm and Java 46
Abstraction 47
Abstraction for Hiding Complexities 48
Data Abstraction 53
Encapsulation and Information Hiding 56
Inheritance 58
Polymorphism 59
Overloading Polymorphism 60
Coercion Polymorphism 61
Inclusion Polymorphism 62
Parametric Polymorphism 63
Summary 64
Chapter 2: Setting Up the Environment 65
System Requirements 65
Installing JDK 9 65
The JDK Directory Structure 66
Verifying the JDK Installation 69
Starting the JShell Tool 70
Installing NetBeans 9 70
Configuring NetBeans 71
Summary 75
Chapter 3: Writing Java Programs 76
The Goal Statement 76
Using the JShell Tool 77
What Is a Java Program? 77
Writing the Source Code 78
Writing Comments 79
Declaring a Module 80
Declaring Types 82
Package Declaration 83
Import Declarations 84
Class Declaration 85
Types Have Two Names 90
Compiling the Source Code 91
Packaging the Compiled Code 93
Running a Java Program 95
Playing with Module Options 100
Listing Observable Modules 100
Limiting the Observable Modules 101
Describing a Module 102
Printing Module Resolution Details 103
Dry Running Your Program 104
Enhancing a Module Descriptor 104
Running Java Programs in Legacy Mode 106
Duplicate Modules on Module Path 109
Syntax for Command-Line Options 111
Writing Java Programs Using the NetBeans IDE 112
Creating a Java Project 112
Creating Modular JARs in NetBeans 119
NetBeans Project Directory Structure 120
Adding Classes to a Module 120
Customizing NetBeans Project Properties 120
Opening an Existing NetBeans Project 122
Behind the Scenes 122
Summary 126
Chapter 4: Data Types 129
What Is a Data Type? 129
What Is an Identifier? 130
Keywords 132
Data Types in Java 132
Primitive Data Types in Java 137
Integral Data Types 138
The int Data Type 138
The long Data Type 140
The byte Data Type 142
The short Data Type 143
The char Data Type 144
Character Literals in Single Quotes 144
Character Escape Sequence 145
Unicode Character Escape Sequence 145
Octal Character Escape Sequence 145
The boolean Data Type 147
Floating-Point Data Types 148
The float Data Type 148
The double Data Type 151
Underscores in Numeric Literals 153
Java Compiler and Unicode Escape Sequence 153
A Short Break 155
Binary Representation of Integers 156
Diminished Radix Complement 157
Radix Complement 158
Binary Representation of Floating-Point Numbers 159
32-bit Single-Precision Floating-Point Format 161
Sign 162
Exponent 162
Significand 163
Special Floating-Point Numbers 164
Signed Zeros 164
Signed Infinities 164
NaN 165
Denormals 166
Rounding Modes 166
Rounding Toward Zero 167
Rounding Toward Positive Infinity 167
Rounding Toward Negative Infinity 167
Rounding Toward Nearest 168
IEEE Floating-Point Exceptions 168
Division by Zero Exception 168
Invalid Operation Exception 168
Overflow Exception 169
Underflow Exception 169
Inexact Exception 169
Java and IEEE Floating-Point Standards 170
Little-Endian and Big-Endian 170
Summary 171
Chapter 5: Operators 174
What Is an Operator? 174
Assignment Operator 176
Declaration, Initialization, and Assignment 178
Arithmetic Operators 179
Addition Operator (+) 180
Subtraction Operator (-) 182
Multiplication Operator (*) 183
Division Operator (/) 184
Modulus Operator (%) 186
Rule #1 186
Rule #2 186
Rule #1 187
Rule #2 187
Rule #3 187
Rule #4 187
Rule #5 187
Unary Plus Operator (+) 188
Unary Minus Operator (-) 188
Compound Arithmetic Assignment Operators 189
Increment (++) and Decrement (--) Operators 190
String Concatenation Operator (+) 193
Relational Operators 198
Equality Operator (==) 198
Rule #1 199
Rule #2 200
Rule #3 200
Inequality Operator (!=) 201
Greater Than Operator (> )
Greater Than or Equal to Operator (> =)
Less Than Operator (< )
Less Than or Equal to Operator (< =)
Boolean Logical Operators 203
Logical NOT Operator (!) 204
Logical Short-Circuit AND Operator (& &
Logical AND Operator (& )
Logical Short-Circuit OR Operator (||) 207
Logical OR Operator (|) 207
Logical XOR Operator (^) 207
Compound Boolean Logical Assignment Operators 208
Ternary Operator (?:) 209
Bitwise Operators 209
Operators Precedence 213
Summary 215
Chapter 6: Statements 219
What Is a Statement? 219
Types of Statements 220
Declaration Statement 220
Expression Statement 220
Control Flow Statement 221
A Block Statement 222
The if-else Statement 223
The switch Statement 228
The for Statement 232
Initialization 233
Condition-Expression 234
Expression-List 235
The for-each Statement 237
The while Statement 238
The do-while Statement 240
The break Statement 242
The continue Statement 245
An Empty Statement 246
Summary 247
Chapter 7: Classes 251
What Is a Class? 251
Declaring a Class 252
Declaring Fields in a Class 253
Creating Instances of a Class 254
The null Reference Type 256
Using Dot Notation to Access Fields of a Class 257
Default Initialization of Fields 260
Access Level Modifiers for a Class 261
Import Declarations 264
Single-Type Import Declaration 265
Import-on-Demand Declaration 267
Import Declarations and Type Search Order 269
Automatic Import Declarations 275
Static Import Declarations 276
Static Import Rule #1 278
Static Import Rule #2 278
Static Import Rule #3 278
Summary 279
Chapter 8: Methods 283
What Is a Method? 283
Declaring Methods of a Class 283
Local Variables 288
Rule #1 288
Rule #2 289
Rule #3 289
Rule #4 289
Instance Methods and Class Methods 292
Invoking a Method 293
The Special main() Method 295
What Is this? 297
Access Levels for Class Members 303
Access Level: A Case Study 310
What Is a Var-Args Method? 316
Overloading a Var-Args Method 321
Var-Args Methods and the main() Method 322
Parameter-Passing Mechanisms 323
Pass By Value 324
Pass By Constant Value 327
Pass By Reference 327
Pass By Reference Value 331
Pass by Constant Reference Value 332
Pass by Result 332
Pass by Value Result 332
Pass By Name 333
Pass by Need 334
Parameter-Passing Mechanisms in Java 334
Summary 346
Chapter 9: Constructors 350
What Is a Constructor? 350
Declaring a Constructor 350
Overloading a Constructor 353
Writing Code for a Constructor 354
Calling a Constructor from Another Constructor 357
Using a return Statement Inside a Constructor 359
Access Level Modifier for a Constructor 360
Default Constructor 364
A static Constructor 365
Instance Initialization Block 365
static Initialization Block 366
The final Keyword 368
final Local Variables 369
final Parameters 370
final Instance Variables 370
final Class Variables 373
final Reference Variables 373
Compile-Time vs. Runtime final Variables 374
Generic Classes 374
Summary 377
Chapter 10: Modules 382
What Is a Module? 382
Declaring Modules 383
Declaring Module Dependence 385
An Example of Module Dependence 387
Troubleshooting 393
Empty Package Error 393
Module Not Found Error 394
Package Does Not Exist Error 394
Module Resolution Exception 394
Implicit Dependence 395
Optional Dependence 400
Opening Modules and Packages 400
Open Modules 402
Opening Packages 402
Splitting Packages Across Modules 403
Restrictions in Module Declarations 404
Types of Modules 404
Normal Modules 406
Open Modules 406
Automatic Modules 406
Unnamed Modules 410
Aggregator Modules 411
Knowing about Modules at Runtime 411
Migration Path to JDK 9 413
Disassembling Module Definitions 415
Summary 418
Chapter 11: Object and Objects Classes 422
The Object Class 422
Rule #1 423
Rule #2 424
What Is the Class of an Object? 426
Computing the Hash Code of an Object 427
Comparing Objects for Equality 431
String Representation of an Object 437
Cloning Objects 441
Finalizing an Object 449
Immutable Objects 451
The Objects Class 456
Bounds Checks 457
Comparing Objects 457
Computing Hash Code 458
Checking for Null 459
Validating Arguments 459
Obtaining String Representation of Objects 460
Using the Objects Class 460
Summary 462
Chapter 12: Wrapper Classes 465
Wrapper Classes 465
Numeric Wrapper Classes 468
The Character Wrapper Class 471
The Boolean Wrapper Class 473
Unsigned Numeric Operations 473
Autoboxing and Unboxing 475
Beware of Null Values 478
Overloaded Methods and Autoboxing/Unboxing 478
Comparison Operators and Autoboxing/Unboxing 481
Collections and Autoboxing/Unboxing 483
Summary 484
Chapter 13: Exception Handling 486
What Is an Exception? 486
An Exception Is an Object 489
Using a try-catch Block 489
Transfer of Control 492
Exception Class Hierarchy 494
Arranging Multiple catch Blocks 495
A Multi-catch Block 498
Checked and Unchecked Exceptions 499
Checked Exception: Catch or Declare 502
Checked Exceptions and Initializers 509
Throwing an Exception 510
Creating an Exception Class 511
The finally Block 515
Rethrowing an Exception 519
Analysis of Rethrown Exceptions 522
Throwing Too Many Exceptions 522
Accessing the Stack of a Thread 524
The try-with-resources Block 527
Summary 534
Chapter 14: Assertions 536
What Is an Assertion? 536
Testing Assertions 538
Enabling/Disabling Assertions 540
Using Assertions 542
Checking for Assertion Status 543
Summary 544
Chapter 15: Strings 546
What Is a String? 546
String Literals 547
Escape Sequence Characters in String Literals 547
Unicode Escapes in String Literals 548
What Is a CharSequence? 548
Creating String Objects 548
Length of a String 549
String Literals Are String Objects 549
String Objects Are Immutable 550
Comparing Strings 551
String Pool 553
String Operations 555
Getting the Character at an Index 555
Testing Strings for Equality 556
Testing a String to be Empty 556
Changing the Case 557
Searching for a String 557
Representing Values as Strings 557
Getting a Substring 558
Trimming a String 558
Replacing Part of a String 558
Matching the Start and End of a String 559
Splitting and Joining Strings 560
Strings in a switch Statement 561
Testing a String for Palindrome 563
StringBuilder and StringBuffer 564
String Concatenation Operator (+) 568
Language-Sensitive String Comparison 568
Summary 569
Chapter 16: Dates and Times 573
The Date-Time API 573
Design Principles 574
A Quick Example 575
Evolution of Timekeeping 576
Time Zones and Daylight Savings Time 579
Calendar Systems 580
The Julian Calendar 580
The Gregorian Calendar 581
ISO-8601 Standards for Datetime 582
Exploring the New Date-Time API 584
The ofXxx() Methods 584
The from() Methods 584
The withXxx() Methods 585
The getXxx() Methods 585
The toXxx() Methods 585
The atXxx() Methods 586
The plusXxx() and minusXxx() Methods 586
The multipliedBy(), dividedBy(), and negated() Methods 586
Instants and Durations 587
Dividing a Duration by Another Duration 590
Converting and Retrieving Duration Parts 590
Truncating Duration 591
Human-Scale Time 592
The ZoneOffset Class 592
The ZoneId Class 594
Useful Datetime-Related Enums 596
Representing Months 597
Representing the Day of the Week 598
Representing DateTime Fields 598
Representing the Units of Datetime Fields 600
Local Date, Time, and Datetime 601
Offset Time and Datetime 606
Zoned Datetime 607
Same Instant, Different Times 611
Clocks 611
Periods 613
Period Between Two Dates and Times 615
Partials 617
Adjusting Dates 619
Querying Datetime Objects 624
Non-ISO Calendar Systems 629
Formatting Dates and Times 631
Using Predefined Formatters 631
Using the format() Method of Datetime Classes 633
Using User-Defined Patterns 634
Using Locale Specific Formats 639
Using the DateTimeFormatterBuilder Class 641
Parsing Dates and Times 642
Legacy Datetime Classes 645
The Date Class 645
The Calendar Class 646
The add() Method 647
The roll() Method 648
Interoperability with Legacy Datetime Classes 649
Summary 653
Chapter 17: Formatting Data 655
Formatting Dates 655
Using Predefined Date Formats 656
Using Custom Date Formats 659
Parsing Dates 661
Formatting Numbers 663
Using Predefined Number Formats 664
Using Custom Number Formats 665
Parsing Numbers 666
printf-Style Formatting 667
The Big Picture 667
The Details 670
Referencing an Argument Inside a Format Specifier 672
Ordinary Indexing 672
Explicit Indexing 674
Relative Indexing 674
Using Flags in a Format Specifier 676
Conversion Characters 677
General Formatting 677
Writing a Custom Formatter 680
Character Formatting 681
Numeric Formatting 682
Integral Number Formatting 682
Floating-Point Number Formatting 685
Formatting Dates and Times 687
Summary 691
Chapter 18: Regular Expressions 693
What Is a Regular Expression? 693
Metacharacters 696
Character Classes 697
Predefined Character Classes 698
More Powers to Regular Expressions 698
Compiling Regular Expressions 698
Creating a Matcher 700
Matching the Pattern 700
Querying a Match 703
Beware of Backslashes 703
Quantifiers in Regular Expressions 704
Matching Boundaries 705
Groups and Back Referencing 706
Using Named Groups 712
Resetting the Matcher 714
Final Words on E-Mail Validations 715
Find-and-Replace Using Regular Expressions 715
Streams of Matched Results 719
Summary 721
Chapter 19: Arrays 725
What Is an Array? 725
Arrays Are Objects 727
Accessing Array Elements 728
Length of an Array 729
Initializing Array Elements 730
Beware of Reference Type Arrays 732
Explicit Array Initialization 733
Limitations of Using Arrays 734
Simulating Variable-Length Arrays 738
Passing an Array as a Parameter 741
Array Parameter Reference 746
Elements of the Array Parameter 747
The Object Referred by the Array Parameter Elements 748
Command-Line Arguments 750
Multi-Dimensional Arrays 754
Accessing Elements of a Multi-Dimensional Array 758
Initializing Multi-Dimensional Arrays 758
Enhanced for Loop for Arrays 759
Array Declaration Syntax 760
Runtime Array Bounds Checks 761
What Is the Class of an Array Object? 762
Array Assignment Compatibility 764
Converting an ArrayList/Vector to an Array 766
Performing Array Operations 767
Converting Arrays to Another Type 769
Searching an Array 770
Comparing Arrays 770
Copying Arrays 772
Filling Arrays 772
Computing Hash Code 773
Performing Parallel Accumulation 773
Sorting Arrays 774
Summary 774
Chapter 20: Inheritance 778
What Is Inheritance? 778
Inheriting Classes 779
The Object Class Is the Default Superclass 782
Inheritance and Hierarchical Relationship 782
What Is Inherited by a Subclass? 783
Upcasting and Downcasting 785
The instanceof Operator 789
Binding 791
Early Binding 792
Late Binding 795
Method Overriding 798
Method Overriding Rule #1 800
Method Overriding Rule #2 800
Method Overriding Rule #3 800
Method Overriding Rule #4 800
Method Overriding Rule #5 801
Method Overriding Rule #6 802
Accessing Overridden Method 805
Method Overloading 807
Inheritance and Constructors 811
Method Hiding 819
Field Hiding 821
Disabling Inheritance 825
Abstract Classes and Methods 826
Method Overriding and Generic Method Signatures 834
Typo Danger in Method Overriding 836
Is-a, has-a, and part-of Relationships 837
No Multiple Inheritance of Classes 840
Summary 840
Chapter 21: Interfaces 846
What Is an Interface? 846
Proposed Solution #1 849
Proposed Solution #2 850
Proposed Solution #3 851
An Ideal Solution 851
Declaring an Interface 856
Declaring Interface Members 857
Constant Fields Declarations 857
Methods Declarations 859
Abstract Methods Declarations 860
Static Methods Declarations 861
Default Methods Declarations 862
Private Methods in an Interface 867
Nested Type Declarations 869
An Interface Defines a New Type 871
Implementing an Interface 874
Implementing Interface Methods 878
Implementing Multiple Interfaces 881
Implementing an Interface Partially 884
The Supertype-Subtype Relationship 886
Interface Inheritance 887
The Superinterface-Subinterface Relationship 893
Inheriting Conflicting Implementations 893
The Superclass Always Wins 894
Example #1 894
Example #2 895
Example #3 895
The Most Specific Superinterface Wins 896
The Class Must Override the Conflicting Method 897
The instanceof Operator 898
Marker Interfaces 902
Functional Interfaces 903
Comparing Objects 903
Using the Comparable Interface 903
Using the Comparator Interface 906
Polymorphism—One Object, Many Views 910
Dynamic Binding and Interfaces 912
Summary 913
Chapter 22: Enum Types 918
What Is an Enum Type? 918
The Superclass of an Enum Type 922
Using Enum Types in switch Statements 926
Associating Data and Methods to Enum Constants 926
Associating a Body to an Enum Constant 928
Comparing Two Enum Constants 933
Nested Enum Types 934
Implementing an Interface to an Enum Type 936
Reverse Lookup for Enum Constants 937
Range of Enum Constants 937
Summary 939
Chapter 23: Java Shell 943
What Is the Java Shell? 944
The JShell Architecture 945
Starting the JShell Tool 946
Exiting the JShell Tool 949
What Are Snippets and Commands? 949
Evaluating Expressions 951
Listing Snippets 953
Editing Snippets 957
Rerunning Previous Snippets 959
Declaring Variables 959
Import Statements 962
Method Declarations 966
Type Declarations 967
Setting the Execution Environment 970
No Checked Exceptions 972
Auto-Completion 972
Snippets and Commands History 976
Reading JShell Stack Trace 977
Reusing JShell Sessions 978
Resetting the JShell State 980
Reloading the JShell State 980
Configuring JShell 983
Setting the Snippet Editor 983
Setting Feedback Mode 984
Creating Custom feedback Modes 987
Setting Up Startup Snippets 991
Using JShell Documentation 994
The JShell API 996
Creating a JShell 997
Working with Snippets 998
Handling Snippet Events 1000
An Example 1000
Summary 1004
Appendix A: Character Encodings 1006
ASCII 1007
8-Bit Character Sets 1011
Universal Multiple-Octet Coded Character Set (UCS) 1012
UCS-2 1013
UCS-4 1013
UTF-16 (UCS Transformation Format 16) 1013
UTF-8 (UCS Transformation Format 8) 1014
Java and Character Encodings 1015
Appendix B: Documentation Comments 1017
Writing Documentation Comments 1018
List of Block and Inline Tags 1020
@author < author-name(s)>
@deprecated < explanation-text>
@exception < class-name>
@param < parameter-name>
@return < description>
@see < reference>
@serial < field-description or include/exclude>
@serialData < data-description>
@serialField < field-name>
@since < description>
@throws < class-name>
@version < version-text>
{@code < text>
{@docRoot} 1026
{@inheritDoc} 1026
{@link < package.class#member>
{@linkplain < package.class#member>
{@literal < text>
{@value < package.class#field>
@hidden 1029
{@index < keyword>
@provides < service-type>
@uses < service-type>
Documenting Packages 1030
com/jdojo/utility/package-info.java file 1031
com/jdojo/utility/package.html file 1031
Overview Documentation 1032
Including Unprocessed Files in Documentation 1032
Skipping Source Files Processing 1032
An Example of Documentation Comments 1032
Running the javadoc Tool 1035
Generated Documentation Files 1037
Viewing Generated HTML Documentation 1037
Searching Javadoc 1039
Summary 1040
Index 1042
Erscheint lt. Verlag | 1.11.2017 |
---|---|
Zusatzinfo | XXXVI, 1037 p. 128 illus., 45 illus. in color. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Informatik ► Programmiersprachen / -werkzeuge ► Java |
Informatik ► Software Entwicklung ► Objektorientierung | |
Schlagworte | Arrays • Data Types • Java • language • modules • objects • OOP • programming • regex • Regular Expressions • Software • Syntax |
ISBN-10 | 1-4842-2902-9 / 1484229029 |
ISBN-13 | 978-1-4842-2902-6 / 9781484229026 |
Haben Sie eine Frage zum Produkt? |
![PDF](/img/icon_pdf_big.jpg)
Größe: 11,9 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich