You are previewing Java Programming 24-Hour Trainer, 2nd Edition.
O'Reilly logo
Java Programming 24-Hour Trainer, 2nd Edition

Book Description

Quick and painless Java programming with expert multimedia instruction

Java Programming 24-Hour Trainer, 2nd Edition is your complete beginner's guide to the Java programming language, with easy-to-follow lessons and supplemental exercises that help you get up and running quickly. Step-by-step instruction walks you through the basics of object-oriented programming, syntax, interfaces, and more, before building upon your skills to develop games, web apps, networks, and automations. This second edition has been updated to align with Java SE 8 and Java EE 7, and includes new information on GUI basics, lambda expressions, streaming API, WebSockets, and Gradle. Even if you have no programming experience at all, the more than six hours of Java programming screencasts will demonstrate major concepts and procedures in a way that facilitates learning and promotes a better understanding of the development process.

This is your quick and painless guide to mastering Java, whether you're starting from scratch or just looking to expand your skill set.

  • Master the building blocks that go into any Java project

  • Make writing code easier with the Eclipse tools

  • Learn to connect Java applications to databases

  • Design and build graphical user interfaces and web applications

  • Learn to develop GUIs with JavaFX

  • If you want to start programming quickly, Java Programming 24-Hour Trainer, 2nd Edition is your ideal solution.

    Table of Contents

    1. Introduction
      1. Who This Book Is For
      2. What This Book Covers
      3. How This Book Is Structured
      4. What You Need to Use This Book
      5. How To Read This Book
      6. Conventions
      7. Source Code
      8. Errata
      9. P2P.Wrox.Com
    2. Lesson 1 Introducing Java
      1. Why Learn Java?
      2. Setting the Goals
      3. The Life Cycle of a Java Program
      4. JDK and JRE
      5. Downloading and Installing Java SE
        1. Installing JDK 8 for MAC OS
        2. Installing JDK 8 in Windows
      6. Your First Java Program: Hello World
        1. Compiling and Running Hello World
      7. Try It
        1. Lesson Requirements
        2. Step-by-Step
    3. Lesson 2 Eclipse IDE
      1. Introducing Eclipse IDE
      2. Downloading and Installing Eclipse
      3. Creating Hello Project in Eclipse
      4. Creating the HelloWorld Class in Eclipse
      5. Java Packages
      6. Completing Code Generation
      7. Additional Materials
      8. Try It
        1. Lesson Requirements
        2. Step-by-Step
    4. Lesson 3 Object-Oriented Programming with Java
      1. Classes and Objects
      2. Variables and Data Types
        1. Declaring Variables
        2. Final Variables
        3. Primitive Data Types
      3. Variable Scope
      4. Wrappers, Autoboxing, and Unboxing
      5. Program Comments
        1. First Useful Program
        2. Conditional Statement if
        3. switch Statement
        4. Inheritance
        5. Method Overriding
      6. Additional Materials
      7. Try It
        1. Lesson Requirements
        2. Hints
        3. Step-by-Step
    5. Lesson 4 Class Methods and Constructors
      1. Method Arguments
      2. Method Overloading
      3. Constructors
      4. The Keyword super
      5. The Keyword this
      6. Passing by Value or by Reference
      7. Variable Scopes
      8. The Keyword static
      9. Try It
        1. Lesson Requirements
        2. Step-by-Step
    6. Lesson 5 Java Syntax: Bits and Pieces
      1. Arrays
        1. More About Strings 
      2. Loops
      3. Debugging Java Programs
      4. More About if and switch Statements
        1. The Flavors of if Statements
      5. Command-Line Arguments
      6. Try It
        1. Lesson Requirements
        2. Step-by-Step
    7. Lesson 6 Packages, Interfaces, and Encapsulation
      1. Java Packages
      2. Encapsulation
        1. Access Levels
      3. The Keyword final
        1. final Variables
        2. final Methods
        3. final Classes
      4. Interfaces
        1. Marker Interfaces
        2. Default Methods in Interfaces
        3. Static Methods in Interfaces
      5. Casting
      6. Try It
        1. Lesson Requirements
        2. Step-by-Step
    8. Lesson 7 Programming with Abstract Classes and Interfaces
      1. Abstract Classes
        1. Assignment
        2. Solution with an Abstract Class
      2. Polymorphism
        1. Making the Interface Solution Polymorphic
      3. Interfaces Versus Abstract Classes
      4. Try It
        1. Lesson Requirements
        2. Step-by-Step
    9. Lesson 8 Introducing the GUI with Swing
      1. Swing Basics
      2. Layout Managers
        1. A Simple Calculator with FlowLayout
        2. A Brief Introduction to Layout Managers
        3. FlowLayout
        4. GridLayout
        5. BorderLayout
        6. Combining Layout Managers
        7. BoxLayout
        8. GridBagLayout
        9. CardLayout
        10. Containers with Absolute Layout
        11. More About Swing Widgets
      3. Swing GUI Builders
      4. Try It
        1. Lesson Requirements
        2. Step-by-Step
    10. Lesson 9 Event Handling in Swing GUI
      1. Introduction to Event Listeners
      2. Teaching the Calculator to Calculate
        1. Registering Components with <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ibooks="http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0">ActionListener</code>
        2. Finding the Source of an Event
        3. How to Pass Data Between Objects
      3. Design Pattern Model-View-Controller
      4. More Swing Listeners
      5. How to use Adapters
      6. Inner Classes
        1. Anonymous Inner Classes
        2. Try It
          1. Lesson Requirements
          2. Step-by-Step
    11. Lesson 10 Error Handling
      1. Stack Trace
      2. Java Exceptions
      3. Exception Hierarchy
      4. Try/Catch Blocks
      5. Using the throws Clause
      6. Using the finally Clause
        1. Try-With-Resources
      7. The throw Keyword
      8. Creating Your Own Exceptions
      9. Try It
        1. Lesson Requirements
        2. Step-by-Step
    12. Lesson 11 Introduction to Collections
      1. Arrays Revisited
      2. Collection Interfaces From <i xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ibooks="http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0">java.util</i>
      3. Dynamic Arrays with ArrayList
      4. Classes Hashtable and Hashmap
        1. Class Properties
      5. Classes Enumeration and Iterator
      6. Class LinkedList
      7. Class BitSet
      8. Choosing the Right Collection
      9. Try It
        1. Lesson Requirements
        2. Step-by-Step
    13. Lesson 12 Introduction to Generics
      1. Generics with Classes
      2. Declaring Generics
      3. Wildcards
      4. Creating Custom Parameterized Classes
      5. Bounded Type Parameters
      6. Generic Methods
      7. Try It
        1. Lesson Requirements
        2. Step-by-Step
    14. Lesson 13 Lambda Expressions and Functional Style Programming
      1. Imperative vs Functional Style
      2. What’s Lambda Expression 
        1. Functional Interfaces
      3. Methods Versus Functions 
        1. Passing Functions to Methods 
      4. Iterating Collections with forEach() 
      5. Lambdas Versus Inheritance and Polymorphism
        1. Eliminating Inheritance 
        2. Interfaces Function and BiFunction
      6. Try It
        1. Lesson Requirements
        2. Step-by-Step
    15. Lesson 14 Working with I/O Streams
      1. Byte Streams
      2. Buffered Streams
      3. Character Streams
      4. Bringing Together GUI and I/O Streams
      5. Data Streams
      6. Utility Classes for Working with Files
        1. The Class File
        2. NIO.2: Using Files, Path, and Paths
      7. What NIO Is About
      8. Try It
        1. Lesson Requirements
        2. Step-by-Step
    16. Lesson 15 Java Serialization
      1. The Class ObjectOutputStream
      2. The Class ObjectInputStream
      3. The Interface Externalizable
      4. Class Versioning
      5. Serializing into Byte Arrays
      6. Try It
        1. Lesson Requirements
        2. Step-by-Step
    17. Lesson 16 Network Programming Basics
      1. Reading Data from the Internet
      2. Connecting Through HTTP Proxy Servers
      3. How to Download Files from the Internet
        1. Specifying Command-Line Parameters for FileDownload
      4. The Stock Quote Program
      5. Socket Programming
        1. Why Use Sockets?
        2. The Stock Quote Server with Sockets
      6. Try It
        1. Lesson Requirements
        2. Hints
        3. Step-by-Step
    18. Lesson 17 Concurrency and Multithreading
      1. The Class Thread
      2. The Interface Runnable
        1. Eliminating Inheritance
      3. Sleeping Threads
      4. How to Kill a Thread
      5. Thread Priorities
      6. Thread Synchronization and Race Conditions
      7. Thread States
      8. Wait and Notify
        1. Closures in Java 
      9. Joining Threads
      10. Goodies From java.util.concurrent
        1. ReentrantLock Versus Synchronized
        2. Executor Framework
        3. A Brief Review of Concurrent Collections
      11. Swingworker Thread
      12. Try It
        1. Lesson Requirements
        2. Step-by-Step
    19. Lesson 18 Introduction to GUI with JavaFX
      1. JavaFX Application Basics
        1. Using the E(fx)clipse Plug-in
      2. Layouts
        1. A Sample Application with the HBox Layout
      3. A Sample Application with the GridPane Layout
      4. Skinning with CSS
      5. Event Handling
      6. Properties and Binding
      7. Try It
        1. Lesson Requirements
        2. Step-by-Step
    20. Lesson 19 Developing JavaFX Calculator and Tic-Tac-Toe 
      1. Designing a Calculator with Scene Builder
        1. Designing the Calculator GUI with Scene Builder
        2. Handling Events in the Controller Class
          1. Recognizing the Source of the Event
          2. Passing Data from View to Controller and Back
      2. Programming Tic-Tac-Toe
        1. The Game Strategy
        2. Designing Tic-Tac-Toe GUI with FXML and CSS
        3. Implementing Game Strategy in Tic-Tac-Toe Controller
        4. Handling the Tic-Tac-Toe Menu Play
        5. Tic-Tac-Toe: What to Try Next
      3. JavaFX on the Web and Mobile Devices
      4. Try It
        1. Lesson Requirements
        2. Step-by-Step
    21. Lesson 20 Stream API
      1. Stream Basics
        1. Intermediate and Terminal Operations
        2. Parallel Versus Sequential Processing
      2. Sorting Collections and Streams  
        1. Sorting Java Collections
        2. Sorting Streams
      3. Other Stream Sources
        1. Creating Finite Size Streams
        2. Creating Infinite-Size Streams
      4. Short-Circuit Operations
      5. Try It
        1. Lesson Requirements
        2. Step-by-Step
    22. Lesson 21 Working with Relational DBMS Using JDBC
      1. JDBC Driver Types
      2. Installing Derby DB and Creating a Database
      3. Sample JDBC Program
      4. Processing Result Sets
      5. The PreparedStatement Class
      6. The CallableStatement Class
      7. The ResultSetMetaData Class
      8. Scrollable Result Sets and Rowset
      9. Transactional Updates
      10. Connection Pools and DataSource
      11. Try It
        1. Lesson Requirements
        2. Hint
        3. Step-by-Step
    23. Lesson 22 Rendering Tabular Data in the GUI
      1. JTable and the MVC Paradigm
      2. The Model
        1. Mandatory Callbacks of Table Models
        2. Optional Callbacks of Table Models
      3. Introduction to Renderers
      4. Summary
      5. Try It
        1. Lesson Requirements
        2. Step-by-Step
    24. Lesson 23 Annotations and Reflection
      1. Javadoc Annotations
      2. Java Annotations Basics
        1. @Override
        2. @Deprecated
        3. @Inherited
        4. @FunctionalInterface
        5. @Documented
      3. Custom Annotations
      4. Reflection
      5. Run-Time Annotation Processing
      6. Summary
      7. Try It
        1. Lesson Requirements
        2. Step-by-Step
    25. Lesson 24 Remote Method Invocation
      1. Developing Applications with RMI
      2. Defining Remote Interfaces
      3. Implementing Remote Interfaces
      4. Registering Remote Objects
      5. Writing RMI Clients
      6. Security Considerations
      7. Finding Remote Objects
      8. Try It
        1. Lesson Requirements
        2. Hints
        3. Step-by-Step
    26. Lesson 25 Java EE 7 Overview
      1. The Big Picture
        1. JCP, JSR, and Other Acronyms
        2. Tiers of Java EE Applications
        3. Containers Versus Application Servers
      2. Profiles and Pruning
      3. Why Java EE?
      4. Try It
        1. Lesson Requirements
        2. Step-by-Step
    27. Lesson 26 Programming with Servlets
      1. The Big Picture
      2. The Thin Client
      3. How to Write a Servlet
      4. How to Deploy a Servlet
      5. Configuring Glassfish in Eclipse IDE
      6. How to Create a Servlet in Eclipse
        1. Deploying a Web Application as WAR
      7. Browser-Servlet Data Flow
      8. HTTP Get and Post Requests
      9. Session Tracking
        1. Cookies
        2. URL Rewriting
        3. Server-Side HttpSession
      10. Filters
      11. Asynchronous Servlets
      12. Try It
        1. Lesson Requirements
        2. Step-by-Step
    28. Lesson 27 JavaServer Pages
      1. Embedding Java Code into HTML
      2. Implicit JSP Objects
      3. Overview of the JSP Tags
        1. Directives
        2. Declarations
        3. Expressions
      4. Scriptlets
        1. Comments
        2. Standard Actions
      5. Error Pages
      6. Java Beans
        1. Using JavaBeans in JSP
        2. How Long Does a Bean Live?
      7. Loading JSP from Servlets
      8. Tag Libraries
      9. JSTL
      10. Try It
        1. Lesson Requirements
        2. Step-by-Step
    29. Lesson 28 Developing Web Applications with WebSockets
      1. HTTP Drawbacks
        1. HTTP Hacks for Server-Side Data Push
      2. Client-Server Communication with Websockets
        1. Web Browser as a WebSocket Client 
        2. Communication with the Server Using WebSockets
        3. Sending Messages
        4. Receiving Messages Using @OnMessage
      3. Encoders and Decoders
      4. Publishing to All Clients
      5. Try It
        1. Lesson Requirements
        2. Step-by-Step
    30. Lesson 29 Introducing JNDI
      1. Naming and Directory Services
      2. Using the Class InitialContext 
        1. Getting a Reference to InitialContext
        2. Injecting JNDI Resources
      3. Administering JNDI Objects in Glassfish
      4. Datasource and JNDI
      5. Lightweight Directory Access Protocol
      6. Try It
        1. Lesson Requirements
        2. Step-by-Step
    31. Lesson 30 Introducing JMS and MOM
      1. Messaging Concepts and Terminology
      2. Two Modes of Message Delivery
      3. Introducing OpenMQ MOM
      4. JMS API Overview
        1. Types of Messages
        2. How to Send a Message Directly to MOM
        3. How to Receive a Message Directly from MOM
        4. How to Publish a Message
        5. How to Subscribe for a Topic
        6. Message Acknowledgments and Transactions Support
        7. Message Selectors
      5. Sending Messages from Java EE Containers 
        1. Administering JMS Objects in GlassFish
      6. Try It
        1. Lesson Requirements
        2. Hints
        3. Step-by-Step
    32. Lesson 31 Introduction to Enterprise JavaBeans
      1. Who Needs EJB Containers?
      2. Types of EJBs
      3. Stateless Session Beans
        1. The Bean
        2. The Client’s View
        3. Asynchronous Methods and Concurrency
      4. Stateful Session Beans
      5. Singleton Beans
      6. Deploying EJB
      7. Message-Driven Beans
      8. EJB and Transactions 
      9. Timer Service
      10. Summary
      11. Try It
        1. Lesson Requirements
        2. Hint
        3. Step-by-Step
    33. Lesson 32 Overview of the Java Persistence API
      1. The Big Picture
      2. Mapping Objects to Database Tables
      3. Querying Entities
        1. JPQL
        2. Criteria API
      4. Entity Manager
      5. Bean Validation
      6. Try It
        1. Lesson Requirements
        2. Step-by-Step
    34. Lesson 33 Working with RESTful Web Services
      1. The Soap Web Services
      2. The RESTful Web Services
      3. Working with JSON-Formatted Data 
        1. Reading JSON with the Streaming API
        2. Writing JSON with the Streaming API
        3. Writing JSON with the Object Model API
      4. The RESTful Stock Server
        1. Creating the Application
        2. Creating the Java Bean Stock
        3. Creating the Endpoint StockService
        4. Creating RESTFful Clients
      5. Contexts and Dependency Injection
      6. Try It
        1. Lesson Requirements
        2. Hints
        3. Step-by-Step
    35. Lesson 34 Java Logging API
      1. Java Logging API
        1. Hello World with the Java Logging API
        2. Using Handlers and Setting Log Levels
        3. Formatters and Filters
      2. Logging Frameworks
      3. Try It
        1. Lesson Requirements
        2. Step-by-Step
    36. Lesson 35 Introduction to Unit Testing with JUnit Framework
      1. Introduction to JUnit
        1. Installing JUnit
        2. Changing the Default Directory Structure in Eclipse
        3. Your First JUnit Test Case 
        4. JUnit Annotations
        5. Applying Annotations for Testing Tax 
        6. Test Suites
        7. JUnit Test Runners
      2. Try It
        1. Lesson Requirements
        2. Step-by-Step
    37. Lesson 36 Build Automation with Gradle
      1. Hello World in Ant 
      2. Hello World in Maven
      3. Gradle Basics
        1. Hello World in Gradle
      4. Changing Gradle Conventions
      5. Managing Dependencies with Gradle
        1. Repositories
        2. Dependencies and Configurations
      6. Using Gradle in Eclipse IDE
        1. Gradle Eclipse Plug-ins
        2. Eclipse IDE and Gradle
      7. Try It
        1. Lesson Requirements
        2. Step-by-Step
    38. Lesson 37 Java Technical Interviews
      1. Getting the Interview
      2. Doing Well at the Interview
      3. Considering the Offer
      4. Interviewing Enterprise Developers
      5. To Get or Not to Get Certified?
      6. Technical Questions and Answers
      7. Epilogue
      8. End User License Agreement