HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
The professional programmer’s Deitel® guide to Java™ SE 7 and SE 8 (Java 8) development with the powerful Java™ platform
Written for programmers with a background in high-level language programming, this book applies the Deitel signature live-code approach to teaching programming and explores the Java™ language and Java™ APIs in depth. The book presents¿ concepts in the context of fully tested programs, complete with syntax shading, code highlighting, line-by-line code walkthroughs and program outputs. The book features hundreds of complete Java™ programs with thousands of lines of proven Java™ code, and hundreds of tips that will help you build robust applications.
Start with an introduction to Java™ using an early classes and objects approach, then rapidly move on to more advanced topics, including GUI, graphics, exception handling, lambdas, streams, functional interfaces, object serialization, concurrency, generics, generic collections, JDBC™ and more. You’ll enjoy the Deitels’ classic treatment of object-oriented programming and the object-oriented design ATM case study, including a complete Java™ implementation. When you’re finished, you’ll have everything you need to build industrial-strength object-oriented¿¿ Java™ SE 7 and SE 8 (Java 8) applications.
Practical, Example-Rich Coverage of:
• Java™ SE 7 and SE 8 (Java 8)
• Lambdas, Streams, Functional Interfaces with Default and Static Methods
• Classes, Objects, Encapsulation,¿ Inheritance, Polymorphism, Interfaces
• Swing and JavaFX GUIs; Graphics
• Integrated Exception Handling
• Files, Streams, Object Serialization
• Multithreading and Concurrency for Optimal Multi-Core Performance
• Generics and Generic Collections
• Database (JDBC™, SQL and JavaDB)
• Using the Debugger and the API Docs
• Industrial-Strength, Object-Oriented Design ATM Case Study and more.
Visit www.deitel.com
• Download code examples
• For information on Deitel’s Dive Into® Series programming training courses delivered at organizations worldwide visit www.deitel.com/training or¿ write to deitel@deitel.com
• Join the Deitel social networking communities on Facebook® at¿ facebook.com/DeitelFan,¿ Twitter® @deitel, Google+™ at google.com/+DeitelFan,¿¿ LinkedIn® at bit.ly/DeitelLinkedIn, YouTube™ at¿ youtube.com/user/DeitelTV
• Subscribe to the Deitel® Buzz Online e-mail newsletter at¿ www.deitel.com/newsletter/ subscribe.html
Foreword xxiii
Preface xxv
Before You Begin xxxvii
Chapter 1: Introduction to Java and Test-Driving a Java Application 1
1.1 Introduction 2
1.2 Object Technology Concepts 4
1.3 Open Source Software 7
1.4 Java 8
1.5 A Typical Java Development Environment 9
1.6 Test-Driving a Java Application 12
1.7 Software Technologies 16
1.8 Keeping Up-to-Date with Information Technologies 18
Chapter 2: Introduction to Java Applications; Input/Output and Operators 20
2.1 Introduction 21
2.2 Your First Program in Java: Printing a Line of Text 21
2.3 Modifying Your First Java Program 26
2.4 Displaying Text with printf 28
2.5 Another Application: Adding Integers 29
2.6 Arithmetic 33
2.7 Decision Making: Equality and Relational Operators 34
2.8 Wrap-Up 37
Chapter 3: Introduction to Classes, Objects, Methods and Strings 38
3.1 Introduction 39
3.2 Instance Variables, set Methods and get Methods 39
3.3 Primitive Types vs. Reference Types 49
3.4 Account Class: Initializing Objects with Constructors 50
3.5 Account Class with a Balance; Floating-Point Numbers 53
3.6 Wrap-Up 58
Chapter 4: Control Statements: Part 1; Assignment, ++ and --Operators 59
4.1 Introduction 60
4.2 Control Structures 60
4.3 if Single-Selection Statement 62
4.4 if…else Double-Selection Statement 63
4.5 Student Class: Nested if…else Statements 67
4.6 while Repetition Statement 69
4.7 Counter-Controlled Repetition 71
4.8 Sentinel-Controlled Repetition 74
4.9 Nested Control Statements 79
4.10 Compound Assignment Operators 81
4.11 Increment and Decrement Operators 81
4.12 Primitive Types 84
4.13 Wrap-Up 85
Chapter 5: Control Statements: Part 2; Logical Operators 86
5.1 Introduction 87
5.2 Essentials of Counter-Controlled Repetition 87
5.3 for Repetition Statement 88
5.4 Examples Using the for Statement 92
5.5 do…while Repetition Statement 97
5.6 switch Multiple-Selection Statement 98
5.7 Class AutoPolicy Case Study: Strings in switch Statements 104
5.8 break and continue Statements 108
5.9 Logical Operators 110
5.10 Wrap-Up 115
Chapter 6: Methods: ADeeperLook 117
6.1 Introduction 118
6.2 Program Modules in Java 118
6.3 static Methods, static Fields and Class Math 119
6.4 Declaring Methods with Multiple Parameters 121
6.5 Notes on Declaring and Using Methods 124
6.6 Argument Promotion and Casting 125
6.7 Java API Packages 127
6.8 Case Study: Secure Random-Number Generation 128
6.9 Case Study: A Game of Chance; Introducing enum Types 133
6.10 Scope of Declarations 138
6.11 Method Overloading 140
6.12 Wrap-Up 142
Chapter 7: Arrays and ArrayLists 144
7.1 Introduction 145
7.2 Arrays 146
7.3 Declaring and Creating Arrays 147
7.4 Examples Using Arrays 148
7.5 Exception Handling: Processing the Incorrect Response 157
7.6 Case Study: Card Shuffling and Dealing Simulation 158
7.7 Enhanced for Statement 163
7.8 Passing Arrays to Methods 164
7.9 Pass-By-Value vs. Pass-By-Reference 166
7.10 Case Study: Class GradeBook Using an Array to Store Grades 167
7.11 Multidimensional Arrays 173
7.12 Case Study: Class GradeBook Using a Two-Dimensional Array 176
7.13 Variable-Length Argument Lists 182
7.14 Using Command-Line Arguments 184
7.15 Class Arrays 186
7.16 Introduction to Collections and Class ArrayList 188
7.17 Wrap-Up 192
Chapter 8: Classes and Objects: ADeeperLook 193
8.1 Introduction 194
8.2 Time Class Case Study 194
8.3 Controlling Access to Members 199
8.4 Referring to the Current Object’s Members with the this Reference 200
8.5 Time Class Case Study: Overloaded Constructors 202
8.6 Default and No-Argument Constructors 208
8.7 Notes on Set and Get Methods 208
8.8 Composition 210
8.9 enum Types 213
8.10 Garbage Collection 215
8.11 static Class Members 216
8.12 static Import 220
8.13 final Instance Variables 221
8.14 Time Class Case Study: Creating Packages 222
8.15 Package Access 228
8.16 Using BigDecimal for Precise Monetary Calculations 230
8.17 Wrap-Up 232
Chapter 9: Object-Oriented Programming: Inheritance 234
9.1 Introduction 235
9.2 Superclasses and Subclasses 236
9.3 protected Members 238
9.4 Relationship Between Superclasses and Subclasses 239
9.5 Constructors in Subclasses 261
9.6 Class Object 261
9.7 Wrap-Up 262
Chapter 10: Object-Oriented Programming: Polymorphism and Interfaces 264
10.1 Introduction 265
10.2 Polymorphism Examples 267
10.3 Demonstrating Polymorphic Behavior 268
10.4 Abstract Classes and Methods 270
10.5 Case Study: Payroll System Using Polymorphism 273
10.6 Allowed Assignments Between Superclass and Subclass Variables 288
10.7 final Methods and Classes 288
10.8 A Deeper Explanation of Issues with Calling Methods from Constructors 289
10.9 Creating and Using Interfaces 290
10.10 Java SE 8 Interface Enhancements 301
10.11 Wrap-Up 302
Chapter 11: Exception Handling: A Deeper Look 304
11.1 Introduction 305
11.2 Example: Divide by Zero without Exception Handling 306
11.3 Exception Handling: ArithmeticExceptions and InputMismatchExceptions 308
11.4 When to Use Exception Handling 314
11.5 Java Exception Hierarchy 314
11.6 finally Block 317
11.7 Stack Unwinding and Obtaining Information from an Exception Object 322
11.8 Chained Exceptions 324
11.9 Declaring New Exception Types 327
11.10 Preconditions and Postconditions 327
11.11 Assertions 328
11.12 try-with-Resources: Automatic Resource Deallocation 330
11.13 Wrap-Up 330
Chapter 12: Swing GUI Components: Part 1 332
12.1 Introduction 333
12.2 Java’s Nimbus Look-and-Feel 334
12.3 Simple GUI-Based Input/Output with JOptionPane 335
12.4 Overview of Swing Components 338
12.5 Displaying Text and Images in a Window 340
12.6 Text Fields and an Introduction to Event Handling with Nested Classes 344
12.7 Common GUI Event Types and Listener Interfaces 350
12.8 How Event Handling Works 352
12.9 JButton 354
12.10 Buttons That Maintain State 357
12.11 JComboBox; Using an Anonymous Inner Class for Event Handling 363
12.12 JList 367
12.13 Multiple-Selection Lists 370
12.14 Mouse Event Handling 372
12.15 Adapter Classes 377
12.16 JPanel Subclass for Drawing with the Mouse 381
12.17 Key Event Handling 384
12.18 Introduction to Layout Managers 387
12.19 Using Panels to Manage More Complex Layouts 397
12.20 JTextArea 398
12.21 Wrap-Up 401
Chapter 13: Graphics and Java 2D 402
13.1 Introduction 403
13.2 Graphics Contexts and Graphics Objects 405
13.3 Color Control 406
13.4 Manipulating Fonts 413
13.5 Drawing Lines, Rectangles and Ovals 418
13.6 Drawing Arcs 422
13.7 Drawing Polygons and Polylines 425
13.8 Java 2D API 428
13.9 Wrap-Up 435
Chapter 14: Strings, Characters and Regular Expressions 436
14.1 Introduction 437
14.2 Fundamentals of Characters and Strings 437
14.3 Class String 438
14.4 Class StringBuilder 451
14.5 Class Character 458
14.6 Tokenizing Strings 463
14.7 Regular Expressions, Class Pattern and Class Matcher 464
14.8 Wrap-Up 473
Chapter 15: Files, Streams and Object Serialization 474
15.1 Introduction 475
15.2 Files and Streams 475
15.3 Using NIO Classes and Interfaces to Get File and Directory Information 477
15.4 Sequential-Access Text Files 481
15.5 Object Serialization 492
15.6 Opening Files with JFileChooser 500
15.7 (Optional) Additional java.io Classes 503
15.8 Wrap-Up 506
Chapter 16: Generic Collections 507
16.1 Introduction 508
16.2 Collections Overview 508
16.3 Type-Wrapper Classes 510
16.4 Autoboxing and Auto-Unboxing 510
16.5 Interface Collection and Class Collections 510
16.6 Lists 511
16.7 Collections Methods 519
16.8 Stack Class of Package java.util 531
16.9 Class PriorityQueue and Interface Queue 533
16.10 Sets 534
16.11 Maps 537
16.12 Properties Class 541
16.13 Synchronized Collections 544
16.14 Unmodifiable Collections 544
16.15 Abstract Implementations 545
16.16 Wrap-Up 545
Chapter 17: Java SE 8 Lambdas and Streams 547
17.1 Introduction 548
17.2 Functional Programming Technologies Overview 549
17.3 IntStream Operations 554
17.4 Stream<Integer> Manipulations 561
17.5 Stream<String> Manipulations 564
17.6 Stream<Employee> Manipulations 566
17.7 Creating a Stream<String> from a File 576
17.8 Generating Streams of Random Values 579
17.9 Lambda Event Handlers 581
17.10 Additional Notes on Java SE 8 Interfaces 581
17.11 Java SE 8 and Functional Programming Resources 582
17.12 Wrap-Up 582
Chapter 18: Generic Classes and Methods 584
18.1 Introduction 585
18.2 Motivation for Generic Methods 585
18.3 Generic Methods: Implementation and Compile-Time Translation 587
18.4 Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type 590
18.5 Overloading Generic Methods 593
18.6 Generic Classes 594
18.7 Raw Types 601
18.8 Wildcards in Methods That Accept Type Parameters 605
18.9 Wrap-Up 609
Chapter 19: Swing GUI Components: Part 2 611
19.1 Introduction 612
19.2 JSlider 612
19.3 Understanding Windows in Java 616
19.4 Using Menus with Frames 617
19.5 JPopupMenu 625
19.6 Pluggable Look-and-Feel 628
19.7 JDesktopPane and JInternalFrame 633
19.8 JTabbedPane 636
19.9 BoxLayout Layout Manager 638
19.10 GridBagLayout Layout Manager 642
19.11 Wrap-Up 652
Chapter 20: Concurrency 653
20.1 Introduction 654
20.2 Thread States and Life Cycle 656
20.3 Creating and Executing Threads with the Executor Framework 659
20.4 Thread Synchronization 663
20.5 Producer/Consumer Relationship without Synchronization 672
20.6 Producer/Consumer Relationship: ArrayBlockingQueue 680
20.7 (Advanced) Producer/Consumer Relationship with synchronized, wait, notify and notifyAll 683
20.8 (Advanced) Producer/Consumer Relationship: Bounded Buffers 690
20.9 (Advanced) Producer/Consumer Relationship: The Lock and Condition Interfaces 698
20.10 Concurrent Collections 705
20.11 Multithreading with GUI: SwingWorker 707
20.12 sort/parallelSort Timings with the Java SE 8 Date/Time API 721
20.13 Java SE 8: Sequential vs. Parallel Streams 723
20.14 (Advanced) Interfaces Callable and Future 726
20.15 (Advanced) Fork/Join Framework 730
20.16 Wrap-Up 730
Chapter 21: Accessing Databases with JDBC 732
21.1 Introduction 733
21.2 Relational Databases 734
21.3 A books Database 735
21.4 SQL 739 21.4.1 Basic SELECT Query 739
21.5 Setting up a Java DB Database 747
21.6 Manipulating Databases with JDBC 750
21.7 RowSet Interface 767
21.8 PreparedStatements 769
21.9 Stored Procedures 785
21.10 Transaction Processing 785
21.11 Wrap-Up 786
Chapter 22: JavaFX GUI 787
22.1 Introduction 788
22.2 JavaFX Scene Builder and the NetBeans IDE 789
22.3 JavaFX App Window Structure 790
22.4 Welcome App–Displaying Text and an Image 791
22.5 Tip Calculator App–Introduction to Event Handling 798
22.6 Wrap-Up 813
Chapter 23: ATM Case Study, Part 1: Object-Oriented Design with the UML 815
23.1 Case Study Introduction 816
23.2 Examining the Requirements Document 816
23.3 Identifying the Classes in a Requirements Document 824
23.4 Identifying Class Attributes 830
23.5 Identifying Objects’ States and Activities 835
23.6 Identifying Class Operations 839
23.7 Indicating Collaboration Among Objects 845
23.8 Wrap-Up 852
Chapter 24: ATM Case Study Part 2: Implementing an Object-Oriented Design 856
24.1 Introduction 857
24.2 Starting to Program the Classes of the ATM System 857
24.3 Incorporating Inheritance and Polymorphism into the ATM System 862
24.4 ATM Case Study Implementation 868
24.5 Wrap-Up 893
Appendix A: Operator Precedence Chart 895
Appendix B: ASCII Character Set 897
Appendix C: Keywords and Reserved Words 898
Appendix D: Primitive Types 899
Appendix E: Using the Debugger 900
E.1 Introduction 901
E.2 Breakpoints and the run, stop, cont and print Commands 901
E.3 The print and set Commands 905
E.4 Controlling Execution Using the step, step up and next Commands 907
E.5 The watch Command 909
E.6 The clear Command 912
E.7 Wrap-Up 914
Appendix F: Using the Java API Documentation 915
F.1 Introduction 915
F.2 Navigating the Java API 916
Appendix G: Creating Documentation with javadoc 924
G.1 Introduction 924
G.2 Documentation Comments 924
G.3 Documenting Java Source Code 925
G.4 javadoc 932
G.5 Files Produced by javadoc 933
Appendix H: Unicode® 937
H.1 Introduction 937
H.2 Unicode Transformation Formats 938
H.3 Characters and Glyphs 939
H.4 Advantages/Disadvantages of Unicode 940
H.5 Using Unicode 940
H.6 Character Ranges 942
Appendix I: Formatted Output 944
I.1 Introduction 945
I.2 Streams 945
I.3 Formatting Output with printf 945
I.4 Printing Integers 946
I.5 Printing Floating-Point Numbers 947
I.6 Printing Strings and Characters 949
I.7 Printing Dates and Times 950
I.8 Other Conversion Characters 952
I.9 Printing with Field Widths and Precisions 954
I.10 Using Flags in the printf Format String 956
I.11 Printing with Argument Indices 960
I.12 Printing Literals and Escape Sequences 960
I.13 Formatting Output with Class Formatter 961
I.14 Wrap-Up 962
Appendix J: Number Systems 963
J.1 Introduction 964
J.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers 967
J.3 Converting Octal and Hexadecimal Numbers to Binary Numbers 968
J.4 Converting from Binary, Octal or Hexadecimal to Decimal 968
J.5 Converting from Decimal to Binary, Octal or Hexadecimal 969
J.6 Negative Binary Numbers: Two’s Complement Notation 971
Appendix K: Bit Manipulation 973
K.1 Introduction 973
K.2 Bit Manipulation and the Bitwise Operators 973
K.3 BitSet Class 983
Appendix L: Labeled break and continue Statements 987
L.1 Introduction 987
L.2 Labeled break Statement 987
L.3 Labeled continue Statement 988
Appendix M: UML 2: Additional Diagram Types 990
M.1 Introduction 990
M.2 Additional Diagram Types 990
Appendix N: Design Patterns 992
N.1 Introduction 992
N.2 Creational, Structural and Behavioral Design Patterns 993
N.3 Design Patterns in Packages java.awt and javax.swing 998
N.4 Concurrency Design Patterns 1005
N.5 Design Patterns Used in Packages java.io and java.net 1006
N.6 Design Patterns Used in Package java.util 1010
N.7 Wrap-Up 1011
Index 1013