This is the up-to-date, practical guide to Java you've been looking for! Whether you're a beginner, you're switching to Java from another language, or you're just looking to brush up on your Java skills, this is the only book you need. You'll get a thorough grounding in the basics of the Java language, including classes, objects, arrays, strings, and exceptions. You'll also learn about more advanced topics: threads, algorithms, XML, JUnit testing, and much more. This book belongs on every Java programmer's shelf!
Highlights:
- Classes and objects
- Arrays
- Exception handling
- Object-oriented programming
- Characters and strings
- Generics
- Class library
- Concurrent programming
- Data structures
- Lambda expressions
- JUnit testing
- JDK tools
Inhaltsverzeichnis
. . . Preface . . . 31
. . . Target Group . . . 31
. . . What This Book Is Not . . . 31
. . . My Life and Java, Or Why a Java Book? . . . 32
. . . Software and Versions . . . 33
. . . Which Java Version to Use . . . 33
. . . Using This Book to Learn . . . 33
. . . Personal Learning Strategies . . . 34
. . . Focusing on the Essentials . . . 35
. . . Special Sections . . . 35
. . . Tasks . . . 36
. . . Structure of This Book . . . 36
. . . Conventions . . . 38
. . . Program Listings . . . 40
. . . Application Programming Interface Documentation in This Book . . . 40
. . . Executable Programs . . . 41
. . . Acknowledgments . . . 41
. . . Resources for This Book . . . 42
. . . Feedback . . . 42
1 . . . Introduction . . . 43
1. 1 . . . Historical Background . . . 43
1. 2 . . . On the Popularity of Java: The Key Features . . . 45
1. 3 . . . Java versus Other Languages* . . . 56
1. 4 . . . Further Development and Losses . . . 59
1. 5 . . . Java Platforms . . . 62
1. 6 . . . Java Platform, Standard Edition, Implementations . . . 67
1. 7 . . . Installing the Java Development Kit . . . 69
1. 8 . . . Compiling and Testing the First Program . . . 71
1. 9 . . . Development Environments . . . 75
1. 10 . . . Further Reading . . . 81
2 . . . Imperative Language Concepts . . . 83
2. 1 . . . Elements of the Java Programming Language . . . 83
2. 2 . . . From Classes to Statements . . . 91
2. 3 . . . Data Types, Typing, Variables, and Assignments . . . 102
2. 4 . . . Expressions, Operands, and Operators . . . 119
2. 5 . . . Conditional Statements or Case Distinctions . . . 142
2. 6 . . . Always the Same with Loops . . . 160
2. 7 . . . Methods of a Class . . . 177
2. 8 . . . Further Reading . . . 200
3 . . . Classes and Objects . . . 201
3. 1 . . . Object-Oriented Programming . . . 201
3. 2 . . . Members of a Class . . . 203
3. 3 . . . Natural Modeling Using Unified Modeling Language* . . . 204
3. 4 . . . Creating New Objects . . . 206
3. 5 . . . ZZZZZnake . . . 218
3. 6 . . . Tying Packages, Imports, and Compilation Units . . . 220
3. 7 . . . Using References, Diversity, Identity, and Equality . . . 228
3. 8 . . . Further Reading . . . 239
4 . . . Arrays and Their Areas of Use . . . 241
4. 1 . . . Simple Field Work . . . 241
4. 2 . . . The Extended for Loop . . . 252
4. 3 . . . A Method with a Variable Number of Arguments . . . 256
4. 4 . . . Multidimensional Arrays* . . . 259
4. 5 . . . Library Support for Arrays . . . 264
4. 6 . . . Using the Arrays Class for Comparing, Filling, Searching, and Sorting . . . 267
4. 7 . . . The Entry Point for the Runtime System: main(. . .) . . . 281
4. 8 . . . Further Reading . . . 285
5 . . . Handling Characters and Strings . . . 287
5. 1 . . . From ASCII via ISO-8859-1 to Unicode . . . 287
5. 2 . . . Data Types for Characters and Strings . . . 295
5. 3 . . . The Character Class . . . 296
5. 4 . . . Strings . . . 301
5. 5 . . . The String Class and Its Methods . . . 303
5. 6 . . . Mutable Strings with StringBuilder and StringBuffer . . . 333
5. 7 . . . CharSequence as Base Type . . . 342
5. 8 . . . Converting Primitives and Strings . . . 345
5. 9 . . . Concatenating Strings . . . 353
5. 10 . . . Decomposing Strings . . . 355
5. 11 . . . Formatting Outputs . . . 360
5. 12 . . . Further Reading . . . 367
6 . . . Writing Custom Classes . . . 369
6. 1 . . . Declaring Custom Classes with Members . . . 369
6. 2 . . . Privacy and Visibility . . . 380
6. 3 . . . One for All: Static Methods and Class Variables . . . 390
6. 4 . . . Constants and Enumerations . . . 397
6. 5 . . . Creating and Destroying Objects . . . 405
6. 6 . . . Class and Object Initialization* . . . 418
6. 7 . . . Conclusion . . . 426
7 . . . Object-Oriented Relationship . . . 427
7. 1 . . . Associations between Objects . . . 427
7. 2 . . . Inheritance . . . 436
7. 3 . . . Types in Hierarchies . . . 447
7. 4 . . . Overriding Methods . . . 457
7. 5 . . . Testing Dynamic Bindings . . . 463
7. 6 . . . Final Classes and Final Methods . . . 466
7. 7 . . . Abstract Classes and Abstract Methods . . . 468
7. 8 . . . Further Information on Overriding and Dynamic Binding . . . 476
7. 9 . . . A Programming Task . . . 482
8 . . . Interfaces, Enumerations, Sealed Classes, Records . . . 483
8. 1 . . . Interfaces . . . 483
8. 2 . . . Enumeration Types . . . 513
8. 3 . . . Sealed Classes and Interfaces . . . 523
8. 4 . . . Records . . . 529
9 . . . There Must Be Exceptions . . . 539
9. 1 . . . Fencing In Problem Areas . . . 539
9. 2 . . . Redirecting Exceptions and throws at the Head of Methods/Constructors . . . 549
9. 3 . . . The Class Hierarchy of Exceptions . . . 550
9. 4 . . . Final Handling Using finally . . . 558
9. 5 . . . Triggering Custom Exceptions . . . 564
9. 6 . . . try with Resources (Automatic Resource Management) . . . 579
9. 7 . . . Special Features of Exception Handling* . . . 588
9. 8 . . . Hard Errors: Error* . . . 593
9. 9 . . . Assertions* . . . 594
9. 10 . . . Conclusion . . . 597
10 . . . Nested Types . . . 599
10. 1 . . . Nested Classes, Interfaces, and Enumerations . . . 599
10. 2 . . . Static Nested Types . . . 601
10. 3 . . . Non-Static Nested Types . . . 603
10. 4 . . . Local Classes . . . 605
10. 5 . . . Anonymous Inner Classes . . . 607
10. 6 . . . Nests . . . 613
10. 7 . . . Conclusion . . . 614
11 . . . Special Types of Java SE . . . 615
11. 1 . . . Object Is the Mother of All Classes . . . 616
11. 2 . . . Weak References and Cleaners . . . 638
11. 3 . . . The java. util. Objects Utility Class . . . 639
11. 4 . . . Comparing Objects and Establishing Order . . . 643
11. 5 . . . Wrapper Classes and Autoboxing . . . 651
11. 6 . . . Iterator, Iterable* . . . 670
11. 7 . . . Annotations in Java Platform, Standard Edition . . . 677
11. 8 . . . Further Reading . . . 682
12 . . . Generics . . . 683
12. 1 . . . Introduction to Java Generics . . . 683
12. 2 . . . Implementing Generics, Type Erasure, and Raw Types . . . 699
12. 3 . . . Restricting Types via Bounds . . . 706
12. 4 . . . Type Parameters in the throws Clause* . . . 710
12. 5 . . . Inheritance and Invariance with Generics . . . 713
12. 6 . . . Consequences of Type Erasure: Type Tokens, Arrays* . . . 725
12. 7 . . . Further Reading . . . 729
13 . . . Lambda Expressions and Functional Programming . . . 731
13. 1 . . . Functional Interfaces and Lambda Expressions . . . 731
13. 2 . . . Method References . . . 755
13. 3 . . . Constructor References . . . 759
13. 4 . . . Functional Programming . . . 762
13. 5 . . . Functional Interfaces from the java. util. function Package . . . 770
13. 6 . . . Optional Is Not a Non-Starter . . . 784
13. 7 . . . What Is So Functional Now? . . . 795
13. 8 . . . Further Reading . . . 797
14 . . . Architecture, Design, and Applied Object Orientation . . . 799
14. 1 . . . SOLID Modeling . . . 799
14. 2 . . . Architecture, Design, and Implementation . . . 803
14. 3 . . . Design Patterns . . . 803
14. 4 . . . Further Reading . . . 811
15 . . . Java Platform Module System . . . 813
15. 1 . . . Class Loader and Module/Classpath . . . 813
15. 2 . . . Importing Modules . . . 819
15. 3 . . . Developing Custom Modules . . . 825
15. 4 . . . Further Reading . . . 833
16 . . . The Class Library . . . 835
16. 1 . . . The Java Class Philosophy . . . 835
16. 2 . . . Simple Time Measurement and Profiling* . . . 842
16. 3 . . . The Class Class . . . 843
16. 4 . . . The Utility Classes System and Members . . . 846
16. 5 . . . The Languages of Different Countries . . . 853
16. 6 . . . Overview of Important Date Classes . . . 857
16. 7 . . . Date-Time API . . . 860
16. 8 . . . Logging with Java . . . 864
16. 9 . . . Maven: Resolving Build Management and Dependencies . . . 867
16. 10 . . . Further Reading . . . 869
17 . . . Introduction to Concurrent Programming . . . 871
17. 1 . . . Concurrency and Parallelism . . . 871
17. 2 . . . Generating Existing Threads and New Threads . . . 875
17. 3 . . . Thread Members and States . . . 882
17. 4 . . . Enter the Executor . . . 893
17. 5 . . . Further Reading . . . 907
18 . . . Introduction to Data Structures and Algorithms . . . 909
18. 1 . . . Lists . . . 909
18. 2 . . . Sets . . . 928
18. 3 . . . Associative Memory . . . 938
18. 4 . . . The Stream API . . . 944
18. 5 . . . Creating a Stream . . . 947
18. 6 . . . Terminal Operations . . . 951
18. 7 . . . Intermediary Operations . . . 962
18. 8 . . . Further Reading . . . 968
19 . . . Files and Data Streams . . . 969
19. 1 . . . Old and New Worlds in java. io and java. nio . . . 969
19. 2 . . . File Systems and Paths . . . 970
19. 3 . . . Random Access Files . . . 980
19. 4 . . . Base Classes for Input/Output . . . 985
19. 5 . . . Reading from Files and Writing to Files . . . 996
19. 6 . . . Further Reading . . . 1003
20 . . . Introduction to Database Management with JDBC . . . 1005
20. 1 . . . Relational Databases and Java Access . . . 1005
20. 2 . . . A Sample Query . . . 1008
20. 3 . . . Further Reading . . . 1009
21 . . . Bits and Bytes, Mathematics and Money . . . 1011
21. 1 . . . Bits and Bytes . . . 1011
21. 2 . . . Floating Point Arithmetic in Java . . . 1025
21. 3 . . . The Members of the Math Class . . . 1031
21. 4 . . . Accuracy and the Value Range of Type and Overflow Control* . . . 1042
21. 5 . . . Random Numbers: Random, ThreadLocalRandom, and SecureRandom . . . 1046
21. 6 . . . Large Numbers* . . . 1051
21. 7 . . . Money and Currency . . . 1064
21. 8 . . . Further Reading . . . 1066
22 . . . Testing with JUnit . . . 1067
22. 1 . . . Software Tests . . . 1067
22. 2 . . . The JUnit Testing Framework . . . 1068
22. 3 . . . Java Assertion Libraries and AssertJ . . . 1081
22. 4 . . . Structure of Large Test Cases . . . 1083
22. 5 . . . Good Design Enables Effective Testing . . . 1085
22. 6 . . . Dummy, Fake, Stub, and Mock . . . 1087
22. 7 . . . JUnit Extensions and Testing Add-Ons . . . 1089
22. 8 . . . Further Reading . . . 1089
23 . . . The Tools of the JDK . . . 1091
23. 1 . . . Overview . . . 1091
23. 2 . . . Translating Java Sources . . . 1092
23. 3 . . . The Java Runtime Environment . . . 1093
23. 4 . . . Documentation Comments with Javadoc . . . 1096
23. 5 . . . The JAR Archive Format . . . 1105
23. 6 . . . Further Reading . . . 1107
. . . The Author . . . 1109
. . . Index . . . 1111