Learn Java with Projects: A concise practical guide to learning everything a Java professional really needs to know [1 ed.] 1837637180, 9781837637188

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

228 59 26MB

English Pages 602 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Learn Java with Projects: A concise practical guide to learning everything a Java professional really needs to know [1 ed.]
 1837637180, 9781837637188

  • Commentary
  • Publisher EPUB | Published: November 2023

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

Polecaj historie