Key Features:
* Structured to let you progress and pause learning at your own pace, on your own terms
* Complete step-by-step exercises and open-ended activities to help build and reinforce key skills
* Ideal for beginners who are looking to get started with Java for the first time
What You Will Learn:
* Get to grips with the fundamental concepts of the Java language
* Write clean and well-commented code that's easy to maintain
* Build a Java project with JAR files and learn about tools like Maven and Gradle
* Debug and compile logical errors and handle exceptions in your programs
* Learn how to use third-party libraries and software development kits (SDKs)
* Use Java Database Connectivity (JDBC) to work with information stored in databases
* Understand how you can keep data secure with cryptography and encryption
* Learn how to keep your development process bug-free with unit testing
About:
You already know you want to learn Java, but which approach works best for you? Whether you want to build web applications, enterprise software, or work on your own side projects The Java Workshop is heavily geared towards getting you build-ready for the real world. A step-by-step approach to learning helps you develop and reinforce key skills in a way that feels engaging and rewarding, without dragging you down into long-running lectures on dry, underlying theory. We use real examples that lead to real results.
As you progress through The Java Workshop, you’ll find key concepts broken down into convenient, individual sections. Designed to accommodate short reading sessions as well as intense, laser-focused study, you’ll find that the included step-by-step exercises and open-ended activities help you embed and reinforce what you’ve learned in a hands-on, practical context. With every chapter carefully designed to let you learn at your own pace, you get to decide how quickly you progress and how you fit learning within your schedule.
Author(s): David Cuartielles, Andreas Göransson, Eric Foster-Johnson
Edition: 1
Publisher: Packt
Year: 2020
Language: English
Pages: 954
Tags: java jdk 13 2020
Cover......Page 1
FM......Page 2
Copyright......Page 3
Table of Contents......Page 4
Preface......Page 22
Chapter 1: Getting Started......Page 28
Writing, Compiling, and Executing Your Hello World Program......Page 29
Exercise 1: Creating Your Hello World Program in Java......Page 30
Basic Syntax and Naming Conventions......Page 36
Printing Out Different Data Types......Page 38
Exercise 2: Printing Different Types of Data......Page 39
Primitive versus Reference Data Types......Page 42
Null......Page 43
Chars and Strings......Page 44
Exercise 3: Declaring Strings......Page 45
Doing Some Math......Page 46
Exercise 4: Solving a Simple Trigonometry Problem......Page 47
Comments Help You to Share Code......Page 49
Exercise 5: Running the Code from the CLI......Page 50
Activity 1: Obtaining the Minimum of Two Numbers......Page 51
Summary......Page 52
Chapter 2: Learning the Basics......Page 54
Controlling the Flow of Your Programs......Page 55
Exercise 1: Creating a Basic if Statement......Page 56
Comparison Operators......Page 59
Exercise 2: Using Java Comparison Operators......Page 60
Exercise 3: Implementing a Nested if Statement......Page 62
Branching Two Ways with if and else......Page 63
Exercise 4: Using if and else Statements......Page 64
Using Complex Conditionals......Page 66
Exercise 5: Using Logical Operators to Create Complex Conditionals......Page 67
Using Arithmetic Operators in an if Condition......Page 69
Exercise 6: Using the Ternary Operator......Page 70
Exercise 7: Comparing Decimal Values......Page 72
Comparing Strings......Page 73
Using Switch Statements......Page 75
Exercise 8: Using Switch......Page 76
Exercise 9: Allowing Cases to Fall Through......Page 77
Exercise 10: Using Java 12 Switch Statements......Page 79
Looping and Performing Repetitive Tasks......Page 81
Exercise 11: Using a Classic for Loop......Page 82
Exercise 12: Using an Enhanced for Loop......Page 83
Jumping Out of Loops with Break and Continue......Page 84
Exercise 13: Using Break and Continue......Page 85
Using the While Loop......Page 86
Exercise 14: Using a While Loop......Page 87
Using the Do-While Loop......Page 88
Exercise 15: Testing Command-Line Arguments......Page 89
Exercise 16: Converting String to Integers and Doubles......Page 91
Diving Deeper into Variables – Immutability......Page 92
Using Static Values......Page 93
Using Local Variable Type Inference......Page 94
Activity 1: Taking Input and Comparing Ranges......Page 95
Summary......Page 96
Chapter 3: Object-Oriented Programming......Page 98
The Anatomy of a Class......Page 99
Working with Objects in Java......Page 101
Checking the Precedence of a Class with instanceof......Page 105
Exercise 1: Creating the WordTool Class......Page 106
Activity 1: Adding the Frequency-of-Symbol Calculation to WordTool......Page 114
Inheritance in Java......Page 115
Overriding and Hiding Methods......Page 117
Avoiding Overriding: Final Classes and Methods......Page 120
Overloading Methods and Constructors......Page 121
Recursion......Page 123
Annotations......Page 125
Interfaces......Page 129
Inner Classes......Page 130
Documenting with JavaDoc......Page 132
Activity 2: Adding Documentation to WordTool......Page 137
Summary......Page 138
Chapter 4: Collections, Lists, and Java's Built-In APIs......Page 140
Introduction......Page 141
Arrays......Page 142
Activity 1: Searching for Multiple Occurrences in an Array......Page 150
Sets......Page 151
Lists......Page 157
Exercise 1: Creating the AnalyzeInput Application......Page 163
Maps......Page 165
Iterating through Collections......Page 168
Exercise 2: Bringing Analytics into the AnalyzeInput Application......Page 172
Sorting Collections......Page 175
Exercise 3: Sort the Results from the AnalyzeInput Application......Page 177
Properties......Page 182
Activity 2: Iterating through Large Lists......Page 187
Summary......Page 188
Chapter 5: Exceptions......Page 190
A Simple Exception Example......Page 191
NullPointerException – Have No Fear......Page 196
Catching Exceptions......Page 199
Exercise 1: Logging Exceptions......Page 204
Throws and Throw......Page 206
Exercise 2: Breaking the Law (and Fixing It)......Page 208
The finally Block......Page 211
Activity 1: Designing an Exception Class Logging Data......Page 212
Best Practices for Handling Exceptions......Page 213
Summary......Page 217
Chapter 6: Libraries, Packages, and Modules......Page 220
Importing Classes......Page 221
Exercise 1: Importing Classes......Page 222
Fully Qualified Class Names......Page 224
Importing All Classes in a Package......Page 225
Static Imports......Page 226
Naming Your Package......Page 227
Directories and Packages......Page 228
Exercise 2: Creating a Package for a Fitness Tracking App......Page 229
Building JAR Files......Page 235
Exercise 3: Building a JAR File......Page 236
Defining the Manifest......Page 237
Exercise 4: Building an Executable JAR File......Page 238
Build Tools......Page 239
Maven......Page 240
Exercise 5: Creating a Maven Project......Page 241
Exercise 6: Adding Java Sources to the Maven Project......Page 244
Exercise 7: Building the Maven Project......Page 251
Exercise 8: Creating an Executable JAR with Maven......Page 252
Using Gradle......Page 253
Exercise 9: Creating a Gradle Project......Page 254
Exercise 10: Building an Executable JAR with Gradle......Page 256
Finding the Libraries......Page 258
Exercise 11: Adding a Third-Party Library Dependency......Page 259
Using the Apache Commons Lang Library......Page 262
Exercise 12: Using the Apache Commons Lang Library......Page 263
Using Modules......Page 264
Creating Modules......Page 266
Exercise 13: Creating a Project for a Module......Page 267
Exercise 14: Creating a Second Module Using the First One......Page 269
Activity 1: Tracking Summer High Temperatures......Page 272
Summary......Page 275
Chapter 7: Databases and JDBC......Page 278
Relational Databases......Page 279
Relational Database Management Systems......Page 280
Exercise 1: Running the H2 Database......Page 281
Exercise 2: Creating the customer Table......Page 283
Inserting Data into a Table......Page 285
Retrieving Data......Page 286
Exercise 4: Creating the email Table......Page 289
Selecting Data from Multiple Tables......Page 290
Exercise 5: Modifying email Data......Page 291
JDBC – Accessing Databases from Java......Page 292
Connecting to Databases......Page 293
Querying Data with JDBC......Page 294
Exercise 6: Querying Data with JDBC......Page 295
Using Prepared Statements......Page 299
Transactions and Rollback......Page 301
Exercise 7: Using Prepared Statements with Transactions......Page 302
Simplifying JDBC Programming......Page 304
Using Object-Relational Mapping Software......Page 305
Database Connection Pooling......Page 306
Activity 1: Track Your Progress......Page 307
Summary......Page 308
Chapter 8: Sockets, Files, and Streams......Page 310
Introduction......Page 311
Listing Files and Directories......Page 312
Separating Directories from Files......Page 316
Exercise 1: Listing the Contents of Subdirectories......Page 319
Creating and Writing to a File......Page 324
Activity 1: Writing the Directory Structure to a File......Page 329
Reading an Existing File......Page 331
Reading a Properties File......Page 333
Exercise 2: Creating a Properties File from the CLI......Page 336
What Are Streams?......Page 339
The Different Streams of the Java Language......Page 343
What Are Sockets?......Page 347
Creating a SocketServer......Page 348
Writing Data on and Reading Data from a Socket......Page 350
Activity 2: Improving the EchoServer and EchoClient Programs......Page 353
Summary......Page 355
Chapter 9: Working with HTTP......Page 358
Exploring HTTP......Page 359
Representational State Transfer......Page 361
Request Headers......Page 362
Using HttpUrlConnection......Page 363
Exercise 1: Creating a HEAD Request......Page 365
Reading the Response Data with a GET Request......Page 367
Exercise 2: Creating a GET Request......Page 368
Requesting Parameters......Page 369
Exercise 3: Sending JSON Data with POST Requests......Page 370
Parsing HTML Data......Page 373
Exercise 4: Using jsoup to Extract Data from HTML......Page 375
Delving into the java.net.http Module......Page 377
Exercise 5: Getting HTML Contents Using the java.net.http Module......Page 379
Activity 1: Using the jsoup Library to Download Files from the Web......Page 380
Summary......Page 381
Chapter 10: Encryption......Page 384
Ciphers......Page 385
Symmetric Key Encryption......Page 387
Exercise 1: Encrypting the String using Advanced Encryption Standard......Page 388
Initialization Vectors......Page 393
Exercise 2: Encrypting the String Using the RSA Asymmetric Key Encryption......Page 394
Encrypting Files......Page 398
Exercise 3: Encrypting a file......Page 399
Summary......Page 407
Chapter 11: Processes......Page 410
Launching a Process......Page 411
Sending Input to a Child Process......Page 414
Capturing the Output of a Child Process......Page 417
Storing the Output of a Child Process in a File......Page 418
Activity 1: Making a Parent Process to Launch a Child Process......Page 420
Summary......Page 422
Chapter 12: Regular Expressions......Page 424
Decrypting Regular Expressions......Page 425
Character Classes......Page 426
Quantifiers......Page 427
Escaped Characters......Page 428
Exercise 1: Implementing Regular Expressions......Page 429
Activity 1: Regular Expressions to Check If the Entrance is Entered in the Desired Format......Page 433
Regular Expressions in Java......Page 434
Exercise 2: Extracting the Domain Using Pattern Matching......Page 435
Exercise 3: Extracting Links Using Pattern Matching......Page 442
Summary......Page 449
Chapter 13: Functional Programming with Lambda Expressions......Page 452
Side Effects......Page 453
Deterministic Functions......Page 455
Exercise 1: Writing Pure Functions......Page 456
Immutability of State......Page 460
Exercise 2: Creating an Immutable Class......Page 461
Activity 1: Modifying Immutable Lists......Page 466
Immutable Collections......Page 467
Exercise 3: Overriding the String Method......Page 468
Functional Interfaces......Page 470
Lambda Expressions......Page 471
Exercise 4: Listing Spare Tires......Page 472
Summary......Page 479
Chapter 14: Recursion......Page 482
Delving into Recursion......Page 483
Exercise 1: Using Recursion to Overflow the Stack......Page 484
Exercise 2: Using Recursion to Calculate Factorials......Page 486
Processing an XML Document......Page 488
Exercise 3: Creating an XML File......Page 489
Introducing the DOM XML API......Page 492
Exercise 4: Traversing an XML Document......Page 494
Activity 1: Calculating the Fibonacci Sequence......Page 501
Summary......Page 502
Chapter 15: Processing Data with Streams......Page 504
Introduction......Page 505
Creating Streams......Page 506
Parallel Streams......Page 508
Closing Streams......Page 509
Terminal Operations......Page 511
Intermediate Operations......Page 516
Exercise 1: Using the Stream API......Page 522
Using Collectors......Page 526
I/O Streams......Page 535
Exercise 2: Converting CSV to a List......Page 537
Summary......Page 543
Chapter 6: Predicates and Other Functional Interfaces......Page 546
Predicate Interface......Page 547
Exercise 1: Defining a predicate......Page 548
Consumer Interface......Page 557
Exercise 2: Producing Side Effects......Page 558
Exercise 3: Extracting Data......Page 565
Summary......Page 569
Chapter 17: Reactive Programming with Java Flow......Page 572
Introduction......Page 573
SubmissionPublisher......Page 575
Subscriber......Page 576
Exercise 1: A Simple Application with a Single Publisher and a Single Subscriber......Page 577
Processor......Page 587
Exercise 2: Using a Processor to Convert a Stream of Strings to Numbers......Page 588
Summary......Page 615
Chapter 18: Unit Testing......Page 618
Getting Started with Unit Tests......Page 619
Writing Unit Tests with JUnit......Page 620
Exercise 1: Writing a First Unit Test......Page 623
Exercise 2: Writing a Successful Test......Page 627
Deciding What to Test......Page 629
Writing Parameterized Tests......Page 630
Exercise 3: Writing a Parameterized Test......Page 632
When Tests Won't Work – Disabling Tests......Page 634
Exercise 4: Using Test Setup and Cleanup Methods......Page 635
Testing with Mocks Using Mockito......Page 638
Exercise 5: Using Mocks when Testing......Page 641
Summary......Page 647
Chapter 19: Reflection......Page 650
Reflection Basics......Page 651
Exercise 1: Getting Class Information......Page 653
Determining Constructors......Page 661
Exercise 2: Extracting a List of Constructors......Page 662
Instantiating New Objects......Page 664
Exercise 3: Instantiating Objects......Page 665
Exercise 4: Extracting Method Information......Page 667
Calling Methods......Page 670
Exercise 5: Calling Methods......Page 671
Getting Fields......Page 673
Exercise 6: Getting Field Information......Page 674
Setting Field Values......Page 675
Reflection in the Real World – Dependency Injection......Page 677
Exercise 7: Using Dependency Injection......Page 679
Reflection in the Real World – JSON Data and RESTful Services......Page 681
Using the Jackson Library......Page 683
Exercise 8: Converting JSON Data Using Jackson......Page 685
Activity 1: Take-Out Food Delivery Using Drones and JSON......Page 692
Summary......Page 694
Chapter 20: Optionals......Page 696
Instantiating Optional Objects......Page 697
Getting Values......Page 699
The Presence Actions......Page 703
Exercise 1: Create Your Own Class Using Optionals......Page 705
map versus flatMap......Page 710
Returning with filter......Page 712
Activity 1: Experimenting with Filters......Page 714
Chaining Optionals to the Stream......Page 715
Summary......Page 717
Chapter 21: References......Page 720
Java Memory Management......Page 721
Strong References......Page 722
Weak References......Page 723
Exercise 1: Weak References......Page 724
Reference Queues......Page 729
Exercise 2: Creating a Shopping Cart......Page 730
WeakHashMap......Page 734
Exercise 3: WeakHashMap......Page 735
Activity 3: Solving the Memory Problem......Page 738
Exercise 4: Soft References......Page 739
Exercise 5: Phantom Reference......Page 742
Summary......Page 752
Chapter 22: Concurrent Tasks......Page 754
Thread Class versus Runnable Interfaces......Page 755
Some Thread Properties......Page 758
Activity 1 – Prioritize Tasks......Page 763
Sleep, Join, Interrupt......Page 765
Shared Resources......Page 773
Synchronized......Page 774
Exercise 1: Making a Thread That Counts and Discounts......Page 775
Atomic Variables/Operations......Page 784
Thread Pools......Page 788
Activity 2 – Random Amounts of Tasks......Page 793
Actor Model......Page 794
Summary......Page 795
Chapter 23: Using the Future API......Page 798
Futures......Page 799
The Executor Interface......Page 800
Employing the Thread Pool......Page 802
Exercise 1: Implementing Bubble Sort......Page 803
Exercise 2: Implementing Merge Sort......Page 810
Activity 2: Improving the MergeSort algorithm......Page 819
Working with Futures......Page 820
Exercise 3: Comparing Merge and Bubble Sorts......Page 821
Scheduling Futures......Page 839
Summary......Page 840
Appendix......Page 842
Index......Page 948