Learn Java with Projects

Book description

Refine your Java skills by seamlessly blending foundational core concepts with hands-on coding applications

Key Features

  • Gain a deep understanding of essential topics that will help you progress with Java
  • Learn by working on mini-projects to help reinforce the concepts you’ve learned
  • Gain comprehensive knowledge of the core concepts of Java
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

Learn Java with Projects stands out in the world of Java guides; while some books skim the surface and others get lost in too much detail, this one finds a nice middle ground.

You’ll begin by exploring the fundamentals of Java, from its primitive data types through to loops and arrays. Next, you’ll move on to object-oriented programming (OOP), where you’ll get to grips with key topics such as classes, objects, encapsulation, inheritance, polymorphism, interfaces, and more. The chapters are designed in a way that focuses on topics that really matter in real-life work situations. No extra fluff here, so that you get more time to spend on the basics and form a solid foundation. As you make progress, you’ll learn advanced topics including generics, collections, lambda expressions, streams and concurrency. This book doesn't just talk about theory—it shows you how things work with little projects, which eventually add up to one big project that brings it all together.

By the end of this Java book, you’ll have sound practical knowledge of Java and a helpful guide to walk you through the important parts of Java.

What you will learn

  • Get a clear understanding of Java fundamentals such as primitive types, operators, scope, conditional statements, loops, exceptions, and arrays
  • Master OOP constructs such as classes, objects, enums, interfaces, and records
  • Develop a deep understanding of OOP principles such as polymorphism, inheritance, and encapsulation
  • Delve into the advanced topics of generics, collections, lambdas, streams, and concurrency
  • Visualize what is happening in memory when you call a method or create an object
  • Appreciate how effective learning-by-doing is

Who this book is for

This book is for anyone looking to learn the core concepts of Java. If you’re learning programming (and Java) for the first time or want to upskill to Java (with experience in a different language), then this book is for you. Prior knowledge of programming is helpful but not necessary.

Publisher resources

Download Example Code

Table of contents

  1. Dr. Seán Kennedy
  2. Maaike van Putten
  3. Learn Java with Projects
  4. Contributors
  5. About the authors
  6. About the reviewers
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Share Your Thoughts
    10. Download a free PDF copy of this book
  8. Part 1: Java Fundamentals
  9. Chapter 1: Getting Started with Java
    1. Technical requirements
    2. Exploring Java features
      1. OOP in Java
      2. Working with OOP
      3. Compiled language
      4. Write once, run anywhere
      5. Automatic memory management
    3. Installing Java
      1. Checking whether Java is installed on your system
      2. Installing Java on Windows
      3. Installing Java on macOS
      4. Installing Java on Linux
      5. Running Java online
    4. Writing our first program
      1. Hello world
      2. Steps to create the program
      3. Understanding the program
    5. Compiling and running Java programs
      1. Understanding the compilation process
      2. Compiling the code with javac on the command line
      3. Running the compiled code with Java on the command line
    6. Working with an IDE
      1. What is an IDE?
      2. Choosing an IDE
    7. Creating and running a program with an IDE
      1. Creating a program in an IDE
      2. Running your program
      3. Debugging a program
    8. Exercises
    9. Project
    10. Summary
  10. Chapter 2: Variables and Primitive Data Types
    1. Technical requirements
    2. Understanding and declaring variables
      1. What is a variable?
      2. Declaring a variable
      3. Naming a variable
      4. Accessing a variable
      5. Accessing a variable that you have not declared
    3. Understanding Java’s primitive data types
      1. Java’s primitive data types
    4. Screen output
    5. Exercises
    6. Project – dinosaur profile generator
    7. Summary
  11. Chapter 3: Operators and Casting
    1. Technical requirements
    2. Learning how Java’s operators cooperate
      1. Order of precedence
      2. Associativity
    3. Understanding Java’s operators
      1. Unary operators
      2. Arithmetic operators
      3. Relational operators
      4. Logical operators
      5. Bitwise operators
      6. Ternary operator
      7. Compound assignment operators
    4. Explaining Java casting
      1. Widening
      2. Narrowing
    5. Exercises
    6. Project – Dino meal planner
    7. Summary
  12. Chapter 4: Conditional Statements
    1. Technical requirements
    2. Understanding scope
      1. What is a block?
    3. Exploring if statements
      1. The if statement itself
      2. else if statements
      3. else statements
    4. Mastering switch statements and expressions
      1. switch statements
      2. switch expressions
    5. Exercises
    6. Project – Task allocation system
    7. Summary
  13. Chapter 5: Understanding Iteration
    1. Technical requirements
    2. while loops
    3. do-while loops
      1. while versus do-while
    4. for loops
      1. Traditional for loop
      2. Enhanced for loop
      3. Nested loops
    5. break and continue statements
      1. break statements
      2. continue statements
    6. Exercises
    7. Project – Dino meal planner
    8. Summary
  14. Chapter 6: Working with Arrays
    1. Technical requirements
    2. Arrays – what, when, and why?
      1. Arrays explained
      2. When to use arrays
    3. Declaring and initializing arrays
      1. Declaring arrays
      2. Initializing arrays
    4. Short syntax for array initialization
    5. Accessing elements in an array
      1. Understanding indexing
      2. Accessing array elements
      3. Modifying array elements
    6. Working with length and bounds
      1. Determining the length of an array
      2. Dealing with the bounds of an array
    7. Iterating over arrays
      1. Using the for loop
      2. Using the for each loop
      3. Choosing between the regular loop and the enhanced for loop
    8. Handling multidimensional arrays
      1. Declaring and initializing multidimensional arrays
      2. Accessing and modifying elements of multidimensional arrays
      3. Iterating over multidimensional arrays
    9. Using Java’s built-in methods for arrays
      1. Built-in Arrays class for working with arrays
    10. Exercises
    11. Project – Dino tracker
    12. Summary
  15. Chapter 7: Methods
    1. Technical requirements
    2. Explaining why methods are important
      1. Flow of control
      2. Abstraction
      3. Code duplication
    3. Understanding the difference between method definition and method execution
      1. Method definition
      2. Method execution
    4. Exploring method overloading
      1. Method signature
      2. Overloading a method
    5. Explaining varargs
    6. Mastering call by value
      1. Primitives versus references in memory
    7. Exercises
    8. Project – Mesozoic Eden assistant
    9. Summary
  16. Part 2: Object-Oriented Programming
  17. Chapter 8: Classes, Objects, and Enums
    1. Technical requirements
    2. Understanding the differences between classes and objects
      1. Classes
      2. Objects
      3. Getting familiar with the new keyword
    3. Contrasting instance with class members
      1. Instance members (methods/data)
      2. Class members (methods/data)
    4. Exploring the “this” reference
      1. Associating an instance with the “this” reference
      2. Shadowing or hiding an instance variable
    5. Applying access modifiers
      1. private
      2. Package-private
      3. protected
      4. public
      5. packages
    6. Encapsulation
      1. Achieving encapsulation
    7. Mastering advanced encapsulation
      1. Call By value revisited
      2. The issue
      3. The solution
    8. Delving into the object life cycle
      1. Garbage collection
      2. Object life cycle example
    9. Explaining the instanceof keyword
    10. Understanding enums
      1. Simple enums
      2. Complex enums
    11. Appreciating records
      1. Record patterns
    12. Exercises
    13. Project – Mesozoic Eden park manager
    14. Summary
  18. Chapter 9: Inheritance and Polymorphism
    1. Technical requirements
    2. Understanding inheritance
      1. Advantages of inheritance
      2. Disadvantages of inheritance
      3. Base class
      4. Subclass
      5. The “is-a” relationship
    3. Applying inheritance
      1. extends
      2. implements
    4. Exploring polymorphism
      1. Separating the reference type from the object type
      2. Applying polymorphism
      3. JVM – object type versus reference type usage
    5. Contrasting method overriding and method overloading
      1. Method overloading
      2. Method overriding
    6. Exploring the super keyword
      1. super()
      2. super.
      3. An example of using super
    7. Revisiting the protected access modifier
      1. The UML diagram
      2. The package with the protected member
      3. The other package
      4. Pattern matching for switch
    8. Explaining the abstract and final keywords
      1. The abstract keyword
      2. The final keyword
    9. Applying sealed classes
      1. sealed and permits
      2. non-sealed
      3. Example using sealed, permits, and non-sealed
    10. Understanding instance and static blocks
      1. Instance blocks
      2. static blocks
    11. Mastering upcasting and downcasting
      1. Upcasting
      2. Downcasting
    12. Exercises
    13. Project
    14. Summary
  19. Chapter 10: Interfaces and Abstract Classes
    1. Technical requirements
    2. Understanding abstract classes
    3. Mastering interfaces
      1. Abstract methods in interfaces
      2. Interface constants
      3. Multiple interface inheritance
    4. Examining default and static interface methods
      1. ‘default’ interface methods
      2. ‘static’ interface methods
      3. Multiple interface inheritance
    5. Explaining ‘private’ interface methods
    6. Exploring sealed interfaces
    7. Exercises
    8. Project – unified park management system
    9. Summary
  20. Chapter 11: Dealing with Exceptions
    1. Technical requirements
    2. Understanding exceptions
      1. What are exceptions?
      2. Need for exception handling
      3. Common situations that require exception handling
    3. Understanding the exception hierarchy
      1. Checked exceptions
      2. Unchecked exceptions
    4. Working with basic I/O operations
      1. Reading from a file using FileReader
      2. Writing to a file using FileWriter
    5. Throwing exceptions
      1. The throw keyword
      2. Creating and throwing custom exceptions
    6. The catch or declare principle
      1. Understanding the principle
      2. Declaring exceptions using throws
      3. Handling exceptions with try-catch
      4. Handling exceptions with try-with-resources
    7. Working with inheritance and exceptions
      1. Declaring exceptions in method signatures
      2. Overriding methods and exception handling
    8. Exercises
    9. Project – dinosaur care system
    10. Summary
  21. Chapter 12: Java Core API
    1. Technical requirements
    2. Understanding the Scanner class
      1. Using Scanner to read from the keyboard
      2. Using Scanner to read from a file
      3. Using Scanner to read from a string
    3. Comparing String with StringBuilder
      1. String class
      2. StringBuilder class
      3. String versus StringBuilder example
    4. Designing a custom immutable type
      1. The checklist
    5. Examining List and ArrayList
      1. List properties
    6. Exploring the Date API
      1. Dates and times
      2. Duration and Period
      3. Additional interesting types
      4. Formatting dates and times
    7. Exercises
    8. Project – dinosaur care system
    9. Summary
  22. Part 3: Advanced Topics
  23. Chapter 13: Generics and Collections
    1. Technical requirements
    2. Getting to know collections
      1. Overview of different collection types
    3. List
      1. ArrayList
      2. LinkedList
      3. Exploring the basic operations for lists
    4. Set
      1. HashSet
      2. TreeSet
      3. LinkedHashSet
      4. Performing basic operations on a set
    5. Map
      1. HashMap
      2. TreeMap
      3. LinkedHashMap
    6. Basic operations on maps
    7. Queue
      1. Queue implementations
      2. Basic operations on the Queue interface
    8. Sorting collections
      1. Natural ordering
      2. The Comparable and Comparator interfaces
    9. Working with generics
      1. Life before generics – objects
      2. Use case of generics
      3. Syntax generics
      4. Bounded generics
    10. Hashing and overriding hashCode()
      1. Understanding basic hashing concepts
      2. hashCode() and its role in collections
      3. Overriding hashCode() and best practices
      4. Using hashCode() in custom generic types
    11. Exercises
    12. Project – advanced dinosaur care system
    13. Summary
  24. Chapter 14: Lambda Expressions
    1. Technical requirements
    2. Understanding lambda expressions
      1. Functional Interfaces
      2. Lambda expressions
      3. final or effectively final
    3. Exploring functional interfaces from the API
      1. Predicate and BiPredicate
      2. Supplier
      3. Consumer and BiConsumer
      4. Function and BiFunction
      5. UnaryOperator and BinaryOperator
    4. Mastering method references
      1. Bound method references
      2. Unbound method references
      3. Static method references
      4. Constructor method references
      5. Method references and context
    5. Exercises
    6. Project – agile dinosaur care system
    7. Summary
  25. Chapter 15: Streams – Fundamentals
    1. Technical requirements
    2. Understanding stream pipelines
      1. Stream pipeline
    3. Exploring stream laziness
    4. Creating streams
      1. Streaming from an array
      2. Streaming from a collection
      3. Stream.of()
      4. Streaming from a file
      5. Infinite streams
    5. Mastering terminal operations
      1. count()
      2. min() and max()
      3. findAny() and findFirst()
      4. anyMatch(), allMatch, and noneMatch()
      5. forEach()
      6. reduce()
      7. collect()
      8. collect(Collector)
    6. Exercises
    7. Project – dynamic dinosaur care system
    8. Summary
  26. Chapter 16: Streams: Advanced Concepts
    1. Technical requirements
    2. Examining intermediate operations
      1. filter(Predicate)
      2. distinct()
      3. limit(long)
      4. map(Function)
      5. flatMap(Function)
      6. sorted() and sorted(Comparator)
    3. Delving into primitive streams
      1. Creating primitive streams
      2. Common primitive stream methods
      3. New primitive stream interfaces
    4. Mapping streams
      1. Mapping from Object streams
      2. Mapping from primitive streams
    5. Explaining Optionals
      1. Creating Optionals
      2. Using the Optional API
      3. Primitive Optionals
    6. Understanding parallel streams
      1. Creating parallel streams
      2. Parallel decomposition
      3. Parallel reductions using reduce()
      4. Parallel reductions using collect()
    7. Exercises
    8. Project – dynamic dinosaur care system
    9. Summary
  27. Chapter 17: Concurrency
    1. Technical requirements
    2. Understanding concurrency
      1. Multiprocessing
      2. Multitasking
      3. Multithreading
      4. Importance of concurrency in modern applications
      5. Challenges in concurrent programming
    3. Working with threads
      1. The Thread class
      2. The Runnable interface
      3. Lambda expressions with Runnable
    4. Thread management – sleep() and join()
      1. The Thread.sleep() method
      2. Handling InterruptedException
      3. Using the join() method
    5. Atomic classes
      1. AtomicInteger, AtomicLong, and AtomicReference
    6. The synchronized keyword
      1. Using synchronized methods
      2. Using synchronized blocks
      3. Synchronized methods versus synchronized blocks
    7. The Lock interface
      1. ReentrantLock
      2. Best practices for working with locks
    8. Concurrent collections
      1. Concurrent collection interfaces
      2. Understanding SkipList collections
      3. Understanding CopyOnWrite collections
      4. Synchronized collections
    9. ExecutorService and thread pools
      1. Executing tasks using SingleThreadExecutor
      2. The Callable interface and Future
      3. Submitting tasks and handling results
      4. Future objects and their methods
      5. Invoking multiple tasks and handling the results
      6. Thread pools and task execution
      7. ScheduledExecutorServices
    10. Data races
    11. Threading problems
      1. Data races
      2. Race conditions
      3. Deadlocks
      4. Livelocks
      5. Starvation
    12. Exercises
    13. Project – Park Operations System – the calm before the storm
    14. Summary
  28. Index
    1. Why subscribe?
  29. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Learn Java with Projects
  • Author(s): Dr. Seán Kennedy, Maaike van Putten
  • Release date: November 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837637188