ebook img

The Java Workshop - A practical, no-nonsense guide to Java. PDF

954 Pages·2020·6.67 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Java Workshop - A practical, no-nonsense guide to Java.

The Java Workshop A practical, no-nonsense guide to Java David Cuartielles Andreas Göransson Eric Foster-Johnson The Java Workshop Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Authors: David Cuartielles, Andreas Göransson, and Eric Foster-Johnson Technical Reviewer: Darryl Pierce Managing Editor: Manasa Kumar Acquisitions Editor: Sarah Lawton Production Editor: Shantanu Zagade Editorial Board: Shubhopriya Banerjee, Mayank Bhardwaj, Ewan Buckingham, Mahesh Dhyani, Taabish Khan, Manasa Kumar, Alex Mazonowicz, Pramod Menon, Bridget Neale, Dominic Pereira, Shiny Poojary, Erol Staveley, Ankita Thakur, Nitesh Thakur, and Jonathan Wray First Published: August 2019 Production Reference: 1260819 ISBN: 978-1-83864-996-8 Published by Packt Publishing Ltd. Livery Place, 35 Livery Street Birmingham B3 2PB, UK Table of Contents Preface i Chapter 1: Getting Started 1 Introduction .................................................................................................... 2 Writing, Compiling, and Executing Your Hello World Program ................ 2 Exercise 1: Creating Your Hello World Program in Java .................................. 3 Basic Syntax and Naming Conventions ............................................................. 9 Printing Out Different Data Types ................................................................... 11 Variables and Variable Types ........................................................................... 12 Exercise 2: Printing Different Types of Data ................................................... 12 Primitive versus Reference Data Types .......................................................... 15 Null ...................................................................................................................... 16 Chars and Strings ............................................................................................... 17 Exercise 3: Declaring Strings ............................................................................. 18 Doing Some Math ............................................................................................... 19 Exercise 4: Solving a Simple Trigonometry Problem ..................................... 20 Comments Help You to Share Code ................................................................ 22 CLI versus GUI .................................................................................................... 23 Exercise 5: Running the Code from the CLI .................................................... 23 Activity 1: Obtaining the Minimum of Two Numbers .................................... 24 Summary ....................................................................................................... 25 Chapter 2: Learning the Basics 27 Introduction .................................................................................................. 28 Controlling the Flow of Your Programs ..................................................... 28 Exercise 1: Creating a Basic if Statement ........................................................ 29 Comparison Operators ...................................................................................... 32 Exercise 2: Using Java Comparison Operators ............................................... 33 Nested if Statements ......................................................................................... 35 Exercise 3: Implementing a Nested if Statement ........................................... 35 Branching Two Ways with if and else .............................................................. 36 Exercise 4: Using if and else Statements ......................................................... 37 Using Complex Conditionals ............................................................................. 39 Exercise 5: Using Logical Operators to Create Complex Conditionals ........ 40 Using Arithmetic Operators in an if Condition ............................................... 42 The Ternary Operator ....................................................................................... 43 Exercise 6: Using the Ternary Operator .......................................................... 43 Equality Can Be Tricky ....................................................................................... 45 Exercise 7: Comparing Decimal Values ........................................................... 45 Comparing Strings ............................................................................................. 46 Using Switch Statements .................................................................................. 48 Exercise 8: Using Switch .................................................................................... 49 Exercise 9: Allowing Cases to Fall Through ..................................................... 50 Using Java 12 Enhanced Switch Statements ................................................... 52 Exercise 10: Using Java 12 Switch Statements ................................................ 52 Looping and Performing Repetitive Tasks ................................................ 54 Looping with the For Loop ................................................................................ 55 Exercise 11: Using a Classic for Loop ............................................................... 55 Exercise 12: Using an Enhanced for Loop ....................................................... 56 Jumping Out of Loops with Break and Continue ........................................... 57 Exercise 13: Using Break and Continue ........................................................... 58 Using the While Loop ......................................................................................... 59 Exercise 14: Using a While Loop ....................................................................... 60 Using the Do-While Loop ................................................................................... 61 Handling Command-Line Arguments ........................................................ 62 Exercise 15: Testing Command-Line Arguments ............................................ 62 Converting Command-Line Arguments .......................................................... 64 Exercise 16: Converting String to Integers and Doubles ............................... 64 Diving Deeper into Variables – Immutability .................................................. 65 Comparing Final and Immutable ..................................................................... 66 Using Static Values ............................................................................................. 66 Using Local Variable Type Inference ............................................................... 67 Activity 1: Taking Input and Comparing Ranges ............................................ 68 Summary ....................................................................................................... 69 Chapter 3: Object-Oriented Programming 71 Introduction .................................................................................................. 72 The Anatomy of a Class ..................................................................................... 72 Working with Objects in Java ...................................................................... 74 Checking the Precedence of a Class with instanceof .............................. 78 Exercise 1: Creating the WordTool Class ......................................................... 79 Activity 1: Adding the Frequency-of-Symbol Calculation to WordTool ....... 87 Inheritance in Java ....................................................................................... 88 Overriding and Hiding Methods ................................................................. 90 Avoiding Overriding: Final Classes and Methods ........................................... 93 Overloading Methods and Constructors .................................................. 94 Recursion ...................................................................................................... 96 Annotations .................................................................................................. 98 Interfaces .................................................................................................... 102 Inner Classes .............................................................................................. 103 Documenting with JavaDoc ...................................................................... 105 Activity 2: Adding Documentation to WordTool ......................................... 110 Summary ..................................................................................................... 111 Chapter 4: Collections, Lists, and Java's Built-In APIs 113 Introduction ................................................................................................ 114 Arrays .......................................................................................................... 115 Activity 1: Searching for Multiple Occurrences in an Array ....................... 123 Sets .............................................................................................................. 124 Lists .............................................................................................................. 130 Exercise 1: Creating the AnalyzeInput Application ..................................... 136 Maps ............................................................................................................ 138 Iterating through Collections ................................................................... 141 Exercise 2: Bringing Analytics into the AnalyzeInput Application ............ 145 Sorting Collections ..................................................................................... 148 Exercise 3: Sort the Results from the AnalyzeInput Application ............... 150 Properties ................................................................................................... 155 Activity 2: Iterating through Large Lists ....................................................... 160 Summary ..................................................................................................... 161 Chapter 5: Exceptions 163 Introduction ................................................................................................ 164 A Simple Exception Example .................................................................... 164 NullPointerException – Have No Fear ..................................................... 169 Catching Exceptions .................................................................................. 172 Exercise 1: Logging Exceptions ...................................................................... 177 Throws and Throw ..................................................................................... 179 Exercise 2: Breaking the Law (and Fixing It) ................................................ 181 The finally Block ......................................................................................... 184 Activity 1: Designing an Exception Class Logging Data .............................. 185 Best Practices for Handling Exceptions .................................................. 186 Where Do Exceptions Come from? .......................................................... 190 Summary ..................................................................................................... 190 Chapter 6: Libraries, Packages, and Modules 193 Introduction ................................................................................................ 194 Organizing Code into Packages ................................................................ 194 Importing Classes ........................................................................................... 194 Exercise 1: Importing Classes ........................................................................ 195 Fully Qualified Class Names .......................................................................... 197 Importing All Classes in a Package ............................................................... 198 Dealing with Duplicated Names .................................................................... 199 Static Imports .................................................................................................. 199 Creating a Package ......................................................................................... 200 Naming Your Package .................................................................................... 200 Directories and Packages ............................................................................... 201 Exercise 2: Creating a Package for a Fitness Tracking App ........................ 202 Building JAR Files ............................................................................................. 208 Exercise 3: Building a JAR File ........................................................................ 209 Defining the Manifest ..................................................................................... 210 Exercise 4: Building an Executable JAR File .................................................. 211 Build Tools ....................................................................................................... 212 Maven ............................................................................................................... 213 Exercise 5: Creating a Maven Project ........................................................... 214 Exercise 6: Adding Java Sources to the Maven Project ............................... 217 Exercise 7: Building the Maven Project ........................................................ 224 Exercise 8: Creating an Executable JAR with Maven ................................... 225 Using Gradle .................................................................................................... 226 Exercise 9: Creating a Gradle Project ........................................................... 227 Exercise 10: Building an Executable JAR with Gradle ................................. 229 Using Third-Party Libraries ............................................................................ 231 Finding the Libraries ....................................................................................... 231 Adding a Project Dependency ....................................................................... 232 Exercise 11: Adding a Third-Party Library Dependency ............................. 232 Using the Apache Commons Lang Library ................................................... 235 Exercise 12: Using the Apache Commons Lang Library ............................. 236 Using Modules ................................................................................................. 237 Creating Modules ............................................................................................ 239 Exercise 13: Creating a Project for a Module ............................................... 240 Exercise 14: Creating a Second Module Using the First One ..................... 242 Activity 1: Tracking Summer High Temperatures ....................................... 245 Summary ..................................................................................................... 248 Chapter 7: Databases and JDBC 251 Introduction ................................................................................................ 252 Relational Databases ................................................................................. 252 Relational Database Management Systems ........................................... 253 Installing a Database ...................................................................................... 254 Exercise 1: Running the H2 Database ........................................................... 254 Introducing SQL ......................................................................................... 256 Exercise 2: Creating the customer Table ...................................................... 256 Inserting Data into a Table ............................................................................ 258 Exercise 3: Inserting Data .............................................................................. 259 Retrieving Data ................................................................................................ 259 Relating Tables ................................................................................................ 262 Exercise 4: Creating the email Table ............................................................. 262 Selecting Data from Multiple Tables ............................................................ 263 Modifying Existing Rows ................................................................................ 264 Exercise 5: Modifying email Data .................................................................. 264 Deleting Data ................................................................................................... 265 JDBC – Accessing Databases from Java ........................................................ 265 Connecting to Databases ............................................................................... 266 Querying Data with JDBC ............................................................................... 267 Exercise 6: Querying Data with JDBC ............................................................ 268 Sanitizing User Input ...................................................................................... 272 Using Prepared Statements ........................................................................... 272 Transactions and Rollback ............................................................................. 274 Exercise 7: Using Prepared Statements with Transactions ....................... 275 Simplifying JDBC Programming ..................................................................... 277 Using Object-Relational Mapping Software ................................................. 278 Database Connection Pooling ....................................................................... 279 Non-Relational, or NoSQL, Databases .......................................................... 280 Activity 1: Track Your Progress ...................................................................... 280 Summary ..................................................................................................... 281 Chapter 8: Sockets, Files, and Streams 283 Introduction ................................................................................................ 284 Listing Files and Directories ..................................................................... 285 Separating Directories from Files ................................................................. 289 Exercise 1: Listing the Contents of Subdirectories ..................................... 292 Creating and Writing to a File ................................................................... 297

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.