O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java 9 Programming By Example

Book Description

Get the steps you need to discover the world of Java 9 programming using real-world examples

About This Book

  • We bridge the gap between “learning” and “doing” by providing real-world examples that will improve your software development
  • Our example-based approach will get you started quickly with software programming, get you up-to-speed with Java 9, and improve your Java skills
  • This book will show you the best practices of Java coding and improve your productivity

Who This Book Is For

This book is for anyone who wants to learn the Java programming language. You are expected to have some prior programming experience with another language, such as JavaScript or Python, but no knowledge of earlier versions of Java is assumed.

What You Will Learn

  • Compile, package and run a trivial program using a build management tool
  • Get to know the principles of test-driven development and dependency management
  • Separate the wiring of multiple modules from the application logic into an application using dependency injection
  • Benchmark Java execution using Java 9 microbenchmarking
  • See the workings of the Spring framework and use Java annotations for the configuration
  • Master the scripting API built into the Java language and use the built-in JavaScript interpreter
  • Understand static versus dynamic implementation of code and high-order reactive programming in Java

In Detail

This book gets you started with essential software development easily and quickly, guiding you through Java’s different facets. By adopting this approach, you can bridge the gap between learning and doing immediately. You will learn the new features of Java 9 quickly and experience a simple and powerful approach to software development. You will be able to use the Java runtime tools, understand the Java environment, and create Java programs.

We then cover more simple examples to build your foundation before diving to some complex data structure problems that will solidify your Java 9 skills. With a special focus on modularity and HTTP 2.0, this book will guide you to get employed as a top notch Java developer.

By the end of the book, you will have a firm foundation to continue your journey towards becoming a professional Java developer.

Style and approach

Throughout this book, our aim is to build Java programs. We will be building multiple applications ranging from simpler ones to more complex ones. Learning by doing has its advantages as you will immediately see the concepts explained in action.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers 
    2. What you need for this book 
    3. Who this book is for 
    4. Conventions 
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Getting Started with Java 9
    1. Getting started with Java
    2. Installing Java
      1. Installation on Windows
      2. Installation on MAC OS X
      3. Installation on Linux
      4. Setting JAVA_HOME
    3. Executing jshell
      1. Looking at the byte code
    4. Packaging classes into a JAR file
    5. Managing the running Java application
    6. Using an IDE
      1. NetBeans
      2. Eclipse
      3. IntelliJ
      4. IDE services
        1. IDE screen structure
        2. Editing files
        3. Managing projects
        4. Build the code and run it
        5. Debugging Java
    7. Summary
  3. The First Real Java Program - Sorting Names
    1. Getting started with sorting
      1. Bubble sort
    2. Getting started with project structure and build tools
      1. Make
      2. Ant
        1. Installing Ant
        2. Using Ant
      3. Maven
        1. Installing Maven
        2. Using Maven
      4. Gradle
        1. Installing Gradle
    3. Setting up the project with Maven
    4. Coding the sort
    5. Understanding the algorithm and language constructs
      1. Blocks
      2. Variables
      3. Types
      4. Arrays
      5. Expressions
      6. Loops
      7. Conditional execution
      8. Final variables
      9. Classes
      10. Inner, nested, local, and anonymous classes
      11. Packages
      12. Methods
      13. Interfaces
      14. Argument passing
      15. Fields
      16. Modifiers
      17. Object initializers and constructors
    6. Compiling and running the program
    7. Summary
  4. Optimizing the Sort - Making Code Professional
    1. The general sorting program
    2. A brief overview of various sorting algorithms
      1. Quick sort
    3. Project structure and build tools
      1. Maven dependency management
    4. Code the sort
      1. Creating the interfaces
      2. Creating BubbleSort
      3. Amending the interfaces
      4. Architectural considerations
    5. Creating unit tests
      1. Adding JUnit as dependency
      2. Writing the BubbleSortTest class
      3. Good unit tests
        1. A good unit test is readable
        2. Unit tests are fast
        3. Unit tests are deterministic
        4. Assertions should be as simple as possible
        5. Unit tests are isolated
        6. Unit tests cover the code
        7. Refactor the test
    6. Collections with wrong elements
    7. Handling exceptions
    8. Generics
    9. Test Driven Development
    10. Implementing QuickSort
      1. The partitioning class
      2. Recursive sorting
      3. Non-recursive sorting
      4. Implementing the API class
    11. Creating modules
      1. Why modules are needed
      2. What is a Java module
    12. Summary
  5. Mastermind - Creating a Game
    1. The Game
    2. The model of the game
    3. Java collections
      1. Interface collection
      2. Set
        1. Hash functions
        2. Method equals
        3. Method hashCode
        4. Implementing equals and hashCode
        5. HashSet
        6. EnumSet
        7. LinkedHashSet
      3. SortedSet
      4. NavigableSet
        1. TreeSet
      5. List
        1. LinkedList
        2. ArrayList
      6. Queue
      7. Deque
      8. Map
        1. HashMap
        2. IdentityHashMap
    4. Dependency injection
    5. Implementing the game
      1. ColorManager
      2. The class color
      3. JavaDoc and code comments
      4. Row
      5. Table
      6. Guesser
        1. UniqueGuesser
        2. GeneralGuesser
    6. The Game class
    7. Creating an integration test
    8. Summary
  6. Extending the Game - Run Parallel, Run Faster
    1. How to make Mastermind parallel
    2. Refactoring
    3. Processes
    4. Threads
    5. Fibers
    6. java.lang.Thread
    7. Pitfalls
      1. Deadlocks
      2. Race conditions
      3. Overused locks
      4. Starving
    8. ExecutorService
      1. ForkJoinPool
    9. Variable access
    10. The CPU heartbeat
    11. Volatile variables
    12. Synchronized block
    13. Wait and notify
    14. Lock
      1. Condition
      2. ReentrantLock
      3. ReentrantReadWriteLock
    15. Atomic classes
    16. BlockingQueue
      1. LinkedBlockingQueue
      2. LinkedBlockingDeque
      3. ArrayBlockingQueue
      4. LinkedTransferQueue
    17. IntervalGuesser
    18. ParallelGamePlayer
    19. Microbenchmarking
    20. Summary
  7. Making Our Game Professional - Do it as a Webapp
    1. Web and network
      1. IP
      2. TCP/IP
      3. DNS
    2. The HTTP protocol
      1. HTTP methods
      2. Status codes
    3. HTTP/2.0
    4. Cookies
    5. Client server and web architecture
    6. Writing servlets
      1. Hello world servlet
    7. Java Server Pages
    8. HTML, CSS, and JavaScript
    9. Mastermind servlet
      1. Storing state
      2. HTTP session
      3. Storing state on the client
      4. Dependency injection with Guice
      5. The MastermindHandler class
      6. Storing state on the server
      7. The GameSessionSaver class
    10. Running the Jetty web servlet
    11. Logging
      1. Configurability
      2. Performance
      3. Log frameworks
      4. Java 9 logging
      5. Logging practice
    12. Other technologies
    13. Summary
  8. Building a Commercial Web Application Using REST
    1. The MyBusiness web shop
    2. Sample business architecture
    3. Microservices
    4. Service interface design
    5. JSON
    6. REST
    7. Model View Controller
    8. Spring framework
      1. Architecture of Spring
      2. Spring core
    9. Service classes
    10. Compiling and running the application
    11. Testing the application
      1. Integration test
      2. Application test
    12. Servlet filters
    13. Audit logging and AOP
    14. Dynamic proxy-based AOP
    15. Summary
  9. Extending Our E-Commerce Application
    1. The MyBusiness ordering
    2. Setting up the project
    3. Order controller and DTOs
    4. Consistency checker
    5. Annotations
      1. Annotation retention
      2. Annotation target
      3. Annotation parameters
      4. Repeatable annotations
      5. Annotation inheritance
      6. @Documented annotations
      7. JDK annotations
    6. Using reflection
      1. Getting annotations
      2. Invoking methods
      3. Setting fields
    7. Functional programming in Java
      1. Lambda
      2. Streams
      3. Functional interfaces
      4. Method references
    8. Scripting in Java 9
    9. Summary
  10. Building an Accounting Application Using Reactive Programming
    1. Reactive... what?
    2. Reactive programming in a nutshell
    3. Reactive systems
      1. Responsive
      2. Resilient
      3. Elastic
      4. Message-driven
      5. Back-pressure
    4. Reactive streams
    5. Reactive programming in Java
      1. Implementing inventory
    6. Summary
  11. Finalizing Java Knowledge to a Professional Level
    1. Java deep technologies
      1. Java agent
      2. Polyglot programming
        1. Polyglot configuration
        2. Polyglot scripting
        3. Business DSL
        4. Problems with polyglot
      3. Annotation processing
    2. Programming in the enterprise
      1. Static code analysis
      2. Source code version control
      3. Software versioning
      4. Code review
      5. Knowledge base
      6. Issue tracking
      7. Testing
        1. Types of tests
        2. Test automation
        3. Black box versus white box
      8. Selecting libraries
        1. Fit for the purpose
        2. License
        3. Documentation
        4. Project alive
        5. Maturity
        6. Number of users
        7. The "I like it" factor
      9. Continuous integration and deployment
      10. Release management
      11. Code repository
      12. Walking up the ladder
    3. Summary