The Java Workshop

Book description

Cut through the noise and get real results with a step-by-step approach to learning Java programming

Key Features

  • Ideal for the Java beginner who is getting started for the first time
  • A step-by-step Java tutorial with exercises and activities that help build key skills
  • Structured to let you progress at your own pace, on your own terms
  • Use your physical copy to redeem free access to the online interactive edition

Book Description

You already know you want to learn Java, and a smarter way to learn Java 12 is to learn by doing. The Java Workshop focuses on building up your practical skills so that you can develop high-performance Java applications that work flawlessly within the JVM across web, mobile and desktop. You'll learn from real examples that lead to real results.

Throughout The Java Workshop, you'll take an engaging step-by-step approach to understanding Java. You won't have to sit through any unnecessary theory. If you're short on time you can jump into a single exercise each day or spend an entire weekend learning about Reactive programming and Unit testing. It's your choice. Learning on your terms, you'll build up and reinforce key skills in a way that feels rewarding.

Every physical copy of The Java Workshop unlocks access to the interactive edition. With videos detailing all exercises and activities, you'll always have a guided solution. You can also benchmark yourself against assessments, track progress, and receive free content updates. You'll even earn a secure credential that you can share and verify online upon completion. It's a premium learning experience that's included with your printed copy. To redeem, follow the instructions located at the start of your Java book.

Fast-paced and direct, The Java Workshop is the ideal companion for Java beginners. You'll build and iterate on your code like a software developer, learning along the way. This process means that you'll find that your new skills stick, embedded as best practice. A solid foundation for the years ahead.

What you will learn

  • Get to grips with fundamental concepts and conventions of Java 12
  • Write clean and well-commented code that's easy to maintain
  • Debug and compile logical errors and handle exceptions in your programs
  • Understand how to work with Java APIs and Java streams
  • Learn how to use third-party libraries and software development kits (SDKs)
  • Discover how you can 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 in Java

Who this book is for

Our goal at Packt is to help you be successful, in whatever it is you choose to do. The Java Workshop is an ideal Java tutorial for the Java beginner who is just getting started. Pick up a Workshop today, and let Packt help you develop skills that stick with you for life.

Table of contents

  1. Preface
    1. About the Book
      1. About the Chapters
      2. Conventions
      3. Before You Begin
      4. Installing the Code Bundle
  2. 1. Getting Started
    1. Introduction
    2. Writing, Compiling, and Executing Your Hello World Program
      1. Exercise 1: Creating Your Hello World Program in Java
      2. Basic Syntax and Naming Conventions
      3. Printing Out Different Data Types
      4. Variables and Variable Types
      5. Exercise 2: Printing Different Types of Data
      6. Primitive versus Reference Data Types
      7. Null
      8. Chars and Strings
      9. Exercise 3: Declaring Strings
      10. Doing Some Math
      11. Exercise 4: Solving a Simple Trigonometry Problem
      12. Comments Help You to Share Code
      13. CLI versus GUI
      14. Exercise 5: Running the Code from the CLI
      15. Activity 1: Obtaining the Minimum of Two Numbers
    3. Summary
  3. 2. Learning the Basics
    1. Introduction
    2. Controlling the Flow of Your Programs
      1. Exercise 1: Creating a Basic if Statement
      2. Comparison Operators
      3. Exercise 2: Using Java Comparison Operators
      4. Nested if Statements
      5. Exercise 3: Implementing a Nested if Statement
      6. Branching Two Ways with if and else
      7. Exercise 4: Using if and else Statements
      8. Using Complex Conditionals
      9. Exercise 5: Using Logical Operators to Create Complex Conditionals
      10. Using Arithmetic Operators in an if Condition
      11. The Ternary Operator
      12. Exercise 6: Using the Ternary Operator
      13. Equality Can Be Tricky
      14. Exercise 7: Comparing Decimal Values
      15. Comparing Strings
      16. Using switch Statements
      17. Exercise 8: Using switch
      18. Exercise 9: Allowing Cases to Fall Through
      19. Using Java 12 Enhanced switch Statements
      20. Exercise 10: Using Java 12 switch Statements
    3. Looping and Performing Repetitive Tasks
      1. Looping with the for Loop
      2. Exercise 11: Using a Classic for Loop
      3. Exercise 12: Using an Enhanced for Loop
      4. Jumping Out of Loops with Break and Continue
      5. Exercise 13: Using break and continue
      6. Using the while Loop
      7. Exercise 14: Using a while Loop
      8. Using the do-while Loop
    4. Handling Command-Line Arguments
      1. Exercise 15: Testing Command-Line Arguments
      2. Converting Command-Line Arguments
      3. Exercise 16: Converting String to Integers and Doubles
      4. Diving Deeper into Variables — Immutability
      5. Comparing Final and Immutable
      6. Using Static Values
      7. Using Local Variable Type Inference
      8. Activity 1: Taking Input and Comparing Ranges
    5. Summary
  4. 3. Object-Oriented Programming
    1. Introduction
      1. The Anatomy of a Class
    2. Working with Objects in Java
    3. Checking the Precedence of a Class with instanceof
      1. Exercise 1: Creating the WordTool Class
      2. Activity 1: Adding the Frequency-of-Symbol Calculation to WordTool
    4. Inheritance in Java
    5. Overriding and Hiding Methods
      1. Avoiding Overriding: Final Classes and Methods
    6. Overloading Methods and Constructors
    7. Recursion
    8. Annotations
    9. Interfaces
    10. Inner Classes
    11. Documenting with JavaDoc
      1. Activity 2: Adding Documentation to WordTool
    12. Summary
  5. 4. Collections, Lists and Java's Built-In APIs
    1. Introduction
    2. Arrays
      1. Activity 1: Searching for Multiple Occurrences in an Array
    3. Sets
    4. Lists
      1. Exercise 1: Creating the AnalyzeInput Application
    5. Maps
    6. Iterating through Collections
      1. Exercise 2: Bringing Analytics into the AnalyzeInput Application
    7. Sorting Collections
      1. Exercise 3: Sort the Results from the AnalyzeInput Application
    8. Properties
      1. Activity 2: Iterating through Large Lists
    9. Summary
  6. 5. Exceptions
    1. Introduction
    2. A Simple Exception Example
    3. NullPointerException – Have No Fear
    4. Catching Exceptions
      1. Exercise 1: Logging Exceptions
    5. Throws and Throw
      1. Exercise 2: Breaking the Law (and Fixing It)
    6. The finally Block
      1. Activity 1: Designing an Exception Class Logging Data
    7. Best Practices for Handling Exceptions
    8. Where Do Exceptions Come from?
    9. Summary
  7. 6. Libraries, Packages, and Modules
    1. Introduction
    2. Organizing Code into Packages
      1. Importing Classes
      2. Exercise 1: Importing Classes
      3. Fully Qualified Class Names
      4. Importing All Classes in a Package
      5. Dealing with Duplicated Names
      6. Static Imports
      7. Creating a Package
      8. Naming Your Package
      9. Directories and Packages
      10. Exercise 2: Creating a Package for a Fitness Tracking App
      11. Building JAR Files
      12. Exercise 3: Building a JAR File
      13. Defining the Manifest
      14. Exercise 4: Building an Executable JAR File
      15. Build Tools
      16. Maven
      17. Exercise 5: Creating a Maven Project
      18. Exercise 6: Adding Java Sources to the Maven Project
      19. Exercise 7: Building the Maven Project
      20. Exercise 8: Creating an Executable JAR with Maven
      21. Using Gradle
      22. Exercise 9: Creating a Gradle Project
      23. Exercise 10: Building an Executable JAR with Gradle
      24. Using Third-Party Libraries
      25. Finding the Libraries
      26. Adding a Project Dependency
      27. Exercise 11: Adding a Third-Party Library Dependency
      28. Using the Apache Commons Lang Library
      29. Exercise 12: Using the Apache Commons Lang Library
      30. Using Modules
      31. Creating Modules
      32. Exercise 13: Creating a Project for a Module
      33. Exercise 14: Creating a Second Module Using the First One
      34. Activity 1: Tracking Summer High Temperatures
    3. Summary
  8. 7. Databases and JDBC
    1. Introduction
    2. Relational Databases
    3. Relational Database Management Systems
      1. Installing a Database
      2. Exercise 1: Running the H2 Database
    4. Introducing SQL
      1. Exercise 2: Creating the customer Table
      2. Inserting Data into a Table
      3. Exercise 3: Inserting Data
      4. Retrieving Data
      5. Relating Tables
      6. Exercise 4: Creating the email Table
      7. Selecting Data from Multiple Tables
      8. Modifying Existing Rows
      9. Exercise 5: Modifying email Data
      10. Deleting Data
      11. JDBC—Accessing Databases from Java
      12. Connecting to Databases
      13. Querying Data with JDBC
      14. Exercise 6: Querying Data with JDBC
      15. Sanitizing User Input
      16. Using Prepared Statements
      17. Transactions and Rollback
      18. Exercise 7: Using Prepared Statements with Transactions
      19. Simplifying JDBC Programming
      20. Using Object-Relational Mapping Software
      21. Database Connection Pooling
      22. Non-Relational, or NoSQL, Databases
      23. Activity 1: Track Your Progress
    5. Summary
  9. 8. Sockets, Files, and Streams
    1. Introduction
    2. Listing Files and Directories
      1. Separating Directories from Files
      2. Exercise 1: Listing the Contents of Subdirectories
    3. Creating and Writing to a File
      1. Activity 1: Writing the Directory Structure to a File
    4. Reading an Existing File
    5. Reading a Properties File
      1. Exercise 2: Creating a Properties File from the CLI
      2. What are Streams?
      3. The Different Streams of the Java Language
      4. What are Sockets?
      5. Creating a SocketServer
      6. Writing Data on and Reading Data from a Socket
      7. Activity 2: Improving the EchoServer and EchoClient Programs
      8. Blocking and Non-Blocking Calls
    6. Summary
  10. 9. Working with HTTP
    1. Introduction
    2. Exploring HTTP
      1. HTTP Request Methods
      2. Representational State Transfer
      3. Request Headers
    3. Using HttpUrlConnection
      1. Exercise 1: Creating a HEAD Request
      2. Reading the Response Data with a GET Request
      3. Exercise 2: Creating a GET Request
    4. Dealing with Slow Connections
      1. Requesting Parameters
      2. Handling Redirects
    5. Creating HTTP POST Requests
      1. Exercise 3: Sending JSON Data with POST Requests
    6. Parsing HTML Data
      1. Exercise 4: Using jsoup to Extract Data from HTML
    7. Delving into the java.net.http Module
      1. Exercise 5: Getting HTML Contents Using the java.net.http Module
      2. Activity 1: Using the jsoup Library to Download Files from the Web
    8. Summary
  11. 10. Encryption
    1. Introduction
      1. Plaintext
      2. Ciphertext
      3. Ciphers
      4. Keys
    2. Symmetric Key Encryption
      1. Exercise 1: Encrypting the String Using Advanced Encryption Standard
    3. Block Ciphers
      1. Initialization Vectors
      2. Stream Ciphers
    4. Asymmetric Key Encryption
      1. Exercise 2: Encrypting the String Using the RSA Asymmetric Key Encryption
    5. Encrypting Files
      1. Exercise 3: Encrypting a File
    6. Summary
  12. 11. Processes
    1. Introduction
    2. Launching a Process
      1. Sending Input to a Child Process
    3. Capturing the Output of a Child Process
    4. Storing the Output of a Child Process in a File
      1. Activity 1: Making a Parent Process to Launch a Child Process
    5. Summary
  13. 12. Regular Expressions
    1. Introduction
    2. Decrypting Regular Expressions
      1. Character Classes
      2. Character Sets
      3. Quantifiers
      4. Anchors
      5. Capturing Groups
      6. Escaped Characters
      7. Flags
      8. Exercise 1: Implementing Regular Expressions
      9. Activity 1: Regular Expressions to Check If the Entrance is Entered in the Desired Format
    3. Regular Expressions in Java
      1. Exercise 2: Extracting the Domain Using Pattern Matching
      2. Exercise 3: Extracting Links Using Pattern Matching
    4. Summary
  14. 13. Functional Programming with Lambda Expressions
    1. Introduction
      1. Background
    2. Functional Programming
      1. Side Effects
      2. Deterministic Functions
    3. Pure Functions
      1. Exercise 1: Writing Pure Functions
      2. Immutability of State
      3. Exercise 2: Creating an Immutable Class
      4. Activity 1: Modifying Immutable Lists
      5. Immutable Collections
      6. Exercise 3: Overriding the String Method
      7. Functional Interfaces
    4. Lambda Expressions
      1. Exercise 4: Listing Spare Tires
    5. Summary
  15. 14. Recursion
    1. Introduction
    2. Delving into Recursion
      1. Exercise 1: Using Recursion to Overflow the Stack
      2. Trying Tail Recursion
      3. Exercise 2: Using Recursion to Calculate Factorials
      4. Processing an XML Document
      5. Exercise 3: Creating an XML File
      6. Introducing the DOM XML API
      7. Exercise 4: Traversing an XML Document
      8. Activity 1: Calculating the Fibonacci Sequence
    3. Summary
  16. 15. Processing Data with Streams
    1. Introduction
    2. Creating Streams
      1. Parallel Streams
      2. Encounter Order
      3. Closing Streams
      4. Terminal Operations
    3. Intermediate Operations
      1. Exercise 1: Using the Stream API
      2. Activity 1: Applying Discount on the Items
    4. Using Collectors
      1. I/O Streams
      2. Exercise 2: Converting CSV to a List
      3. Activity 2: Searching for Specifics
    5. Summary
  17. 16. Predicates and Other Functional Interfaces
    1. Introduction
    2. Predicate Interface
      1. Exercise 1: Defining a predicate
    3. Activity 1: Toggling the Sensor States
      1. Consumer Interface
      2. Exercise 2: Producing Side Effects
    4. Function
      1. Exercise 3: Extracting Data
      2. Activity 2: Using a Recursive Function
      3. Activity 3: Using a Lambda Function
    5. Summary
  18. 17. Reactive Programming with Java Flow
    1. Introduction
    2. Publisher
      1. SubmissionPublisher
    3. Subscriber
      1. Subscription
      2. Exercise 1: A Simple Application with a Single Publisher and a Single Subscriber
    4. Processor
      1. Exercise 2: Using a Processor to Convert a Stream of Strings to Numbers
      2. Activity 1: Let NumberProcessor Format Values as Integers
    5. Summary
  19. 18. Unit Testing
    1. Introduction
    2. Getting Started with Unit Tests
      1. Introducing JUnit
      2. Writing Unit Tests with JUnit
      3. Exercise 1: Writing a First Unit Test
      4. Exercise 2: Writing a Successful Test
      5. Deciding What to Test
      6. Writing Parameterized Tests
      7. Exercise 3: Writing a Parameterized Test
      8. When Tests Won't Work—Disabling Tests
      9. Test Setup
      10. Exercise 4: Using Test Setup and Cleanup Methods
      11. Mocking
      12. Testing with Mocks Using Mockito
      13. Exercise 5: Using Mocks when Testing
      14. Activity 1: Counting the Words in the String
    3. Summary
  20. Appendix
    1. Chapter 1: Getting Started
      1. Activity 1: Obtaining the Minimum of Two Numbers
    2. Chapter 2: Learning the Basics
      1. Activity 1: Taking Input and Comparing Ranges
    3. Chapter 3: Object-Oriented Programming
      1. Activity 1: Adding the Frequency-of-Symbol Calculation to WordTool
      2. Activity 2: Adding Documentation to WordTool
    4. Chapter 4: Collections, List, and Java's Built-In APIs
      1. Activity 1: Searching for Multiple Occurrences in an Array
      2. Activity 2: Iterating through Large Lists
    5. Chapter 5: Exceptions
      1. Activity 1: Designing an Exception Class Logging Data
    6. Chapter 6: Libraries, Packages, and Modules
      1. Activity 1: Tracking Summer High Temperatures
    7. Chapter 7: Databases and JDBC
      1. Activity 1: Track Your Progress
    8. Chapter 8: Sockets, Files, and Streams
      1. Activity 1: Writing the Directory Structure to a File
      2. Activity 2: Improving the EchoServer and EchoClient Programs
    9. Chapter 9: Working with HTTP
      1. Activity 1: Using the jsoup Library to Download Files from the Web
    10. Chapter 11: Processes
      1. Activity 1: Making a Parent Process to Launch a Child Process
    11. Chapter 12: Regular Expressions
      1. Activity 1: Regular Expressions to Check If the Entrance is Entered in the Desired Format
    12. Chapter 13: Functional Programming with Lambda Expressions
      1. Activity 1: Modifying Immutable Lists
    13. Chapter 14: Recursion
      1. Activity 1: Calculating the Fibonacci Sequence
    14. Chapter 15: Processing Data with Streams
      1. Activity 1: Applying Discount on the Items
      2. Activity 2: Searching for Specifics
    15. Chapter 16: Predicates and Other Functional Interfaces
      1. Activity 1: Toggling the Sensor States
      2. Activity 2: Using a Recursive Function
      3. Activity 3: Using a Lambda Function
    16. Chapter 17: Reactive Programming with Java Flow
      1. Activity 1: Let NumberProcessor Format Values as Integers
    17. Chapter 18: Unit Testing
      1. Activity 1: Counting the Words in the String

Product information

  • Title: The Java Workshop
  • Author(s): David Cuartielles, Andreas Goransson, Eric Foster-Johnson
  • Release date: October 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838986698