Head First Java, 3rd Edition

Book description

What will you learn from this book?

Head First Java is a complete learning experience in Java and object-oriented programming. With this book, you'll learn the Java language with a unique method that goes beyond how-to manuals and helps you become a great programmer. Through puzzles, mysteries, and soul-searching interviews with famous Java objects, you'll quickly get up to speed on Java's fundamentals and advanced topics including lambdas, streams, generics, threading, networking, and the dreaded desktop GUI. If you have experience with another programming language, Head First Java will engage your brain with more modern approaches to coding--the sleeker, faster, and easier to read, write, and maintain Java of today.

What's so special about this book?

If you've read a Head First book, you know what to expect--a visually rich format designed for the way your brain works. If you haven't, you're in for a treat. With Head First Java, you'll learn Java through a multisensory experience that engages your mind, rather than by means of a text-heavy approach that puts you to sleep.

Publisher resources

View/Submit Errata

Table of contents

  1. Other books in O’Reilly’s Head First series
  2. Table of Contents (the real thing)
  3. How to Use This Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. And we know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did:
    6. Here’s what YOU can do to bend your brain into submission
    7. What you need for this book:
    8. Last-minute things you need to know:
    9. Technical Reviewers for the 3rd Edition
    10. Other people to acknowledge for the 3rd Edition
    11. Technical Editors for the 2nd Edition
    12. Other people to credit, for the 2nd Edition
    13. Just when you thought there wouldn’t be any more acknowledgments*
  4. 1. Breaking the Surface: Dive In: A Quick Dip
    1. The way Java works
    2. What you’ll do in Java
    3. A very brief history of Java
      1. Speed and memory usage
    4. Code structure in Java
      1. What goes in a source file?
      2. What goes in a class?
      3. What goes in a method?
    5. Anatomy of a class
    6. Writing a class with a main()
    7. What can you say in the main method?
    8. Looping and looping and...
      1. Simple boolean tests
      2. Example of a while loop
    9. Conditional branching
    10. Coding a serious business application
    11. Monday morning at Bob’s Java-enabled house
    12. Phrase-O-Matic
      1. How it works
    13. Exercise
      1. Code Magnets
      2. BE the Compiler
      3. JavaCross
      4. Mixed Messages
      5. Pool Puzzle
    14. Exercise Solutions
      1. Sharpen your pencil
      2. Code Magnets
      3. BE the Compiler
      4. Pool Puzzle
      5. JavaCross
      6. Mixed Messages
  5. 2. A Trip to Objectville: Classes and Objects
    1. Chair Wars
      1. (or How Objects Can Change Your Life)
      2. At Laura’s desk
      3. At Brad’s laptop at the cafe
      4. Back at Laura’s desk
      5. At Brad’s laptop at the beach
      6. Back at Laura’s desk
      7. At Brad’s laptop on his lawn chair at the Telluride Bluegrass Festival
      8. So, Brad the OO guy got the chair and desk, right?
      9. What about the Amoeba rotate()?
      10. The suspense is killing me. Who got the chair and desk?
      11. When you design a class, think about the objects that will be created from that class type. Think about:
    2. What’s the difference between a class and an object?
      1. A class is not an object (but it’s used to construct them)
    3. Making your first object
    4. Making and testing Movie objects
    5. Quick! Get out of main!
      1. The two uses of main:
      2. The Guessing Game
    6. Running the Guessing Game
    7. Exercise
      1. BE the Compiler
      2. Code Magnets
      3. Pool Puzzle
      4. Who Am I?
    8. Exercise Solutions
      1. Code Magnets
      2. BE the Compiler
      3. Puzzle Solutions
      4. Who Am I?
  6. 3. Know Your Variables: Primitives and References
    1. Declaring a variable
    2. “I’d like a double mocha, no, make it an int.”
      1. Primitive Types
    3. You really don’t want to spill that...
    4. Back away from that keyword!
    5. This table reserved
    6. Controlling your Dog object
    7. An object reference is just another variable value
    8. Java Exposed
      1. Life on the garbage-collectible heap
      2. Life and death on the heap
    9. An array is like a tray of cups
    10. Arrays are objects too
    11. Make an array of Dogs
    12. Control your Dog (with a reference variable)
    13. What happens if the Dog is in a Dog array?
    14. A Dog example
    15. Exercise
      1. BE the Compiler
      2. Code Magnets
      3. Pool Puzzle
      4. A Heap o’ Trouble
      5. The case of the pilfered references
      6. Five-Minute Mystery
    16. Exercise Solutions
      1. Sharpen your pencil
      2. Code Magnets
      3. BE the Compiler
      4. Puzzle Solutions
      5. Five-Minute Mystery
      6. A Heap o’ Trouble
  7. 4. How Objects Behave: Methods Use Instance Variables
    1. Remember: a class describes what an object knows and what an object does
      1. Can every object of that type have different method behavior?
    2. The size affects the bark
    3. You can send things to a method
    4. You can get things back from a method
    5. You can send more than one thing to a method
      1. Calling a two-parameter method and sending it two arguments
      2. You can pass variables into a method, as long as the variable type matches the parameter type
    6. Java is pass-by-value. That means pass-by-copy.
    7. Reminder: Java cares about type!
    8. Cool things you can do with parameters and return types
    9. Encapsulation
      1. Do it or risk humiliation and ridicule.
      2. Hide the data
    10. Java Exposed
    11. Encapsulating the GoodDog class
    12. How do objects in an array behave?
    13. Declaring and initializing instance variables
    14. The difference between instance and local variables
    15. Comparing variables (primitives or references)
    16. Exercise
      1. BE the Compiler
      2. Who Am I?
      3. Mixed Messages
      4. Pool Puzzle
      5. Five-Minute Mystery
    17. Exercise Solutions
      1. Sharpen your pencil
      2. BE the Compiler
      3. Who Am I?
      4. Puzzle Solutions
      5. Five-Minute Mystery
      6. Mixed Messages
  8. 5. Extra-Strength Methods: Writing A Program
    1. Let’s build a Battleship-style game: “Sink a Startup”
    2. First, a high-level design
    3. The “Simple Startup Game” a gentler introduction
    4. Developing a Class
      1. The three things we’ll write for each class:
    5. Writing the method implementations
    6. Writing test code for the SimpleStartup class
      1. Based on this prep code:
      2. Here’s what we should test:
    7. Test code for the SimpleStartup class
    8. The checkYourself() method
    9. Just the new stuff
    10. Final code for SimpleStartup and SimpleStartupTestDrive
    11. Prep code for the SimpleStartupGame class Everything happens in main()
    12. The game’s main() method
    13. random() and getUserInput()
    14. One last class: GameHelper
    15. Let’s play
    16. What’s this? A bug ?
      1. Gasp!
    17. More about for loops
      1. Regular (non-enhanced) for loops
    18. Trips through a loop
      1. Difference between for and while
    19. The enhanced for loop
    20. Casting primitives
    21. Exercise
      1. BE the JVM
      2. Code Magnets
      3. JavaCross
      4. Mixed Messages
    22. Exercise Solutions
      1. Be the JVM
      2. Code Magnets
      3. Puzzle Solutions
      4. JavaCross
      5. Mixed Messages
  9. 6. Using the Java Library: Get to Know the Java API
    1. In our last chapter, we left you with the cliff-hanger: a bug
      1. How it’s supposed to look
      2. How the bug looks
    2. So what happened?
    3. How do we fix it?
    4. Option one is too clunky
    5. Option two is a little better, but still pretty clunky
    6. Wake up and smell the library
    7. Some things you can do with ArrayList
    8. Java Exposed
    9. Solution
      1. Sharpen your pencil
    10. Comparing ArrayList to a regular array
    11. Let’s fix the Startup code
    12. New and improved Startup class
    13. Let’s build the REAL game: “Sink a Startup”
    14. What needs to change?
    15. Who does what in the StartupBust game (and when)
    16. Prep code for the real StartupBust class
    17. The final version of the Startup class
    18. Super powerful Boolean expressions
    19. Ready-Bake Code
    20. Using the Library (the Java API)
    21. You have to know the full name* of the class you want to use in your code.
    22. How to discover the API
    23. Browse a book
    24. Use the HTML API docs
    25. Using the class documentation
    26. Exercise
      1. Code Magnets
      2. JavaCross
    27. Exercise Solutions
      1. Code Magnets
      2. JavaCross
  10. 7. Better Living in Objectville: Inheritance and Polymorphism
    1. Chair Wars Revisited...
    2. What about the Amoeba rotate()?
    3. Understanding Inheritance
    4. An inheritance example:
    5. Let’s design the inheritance tree for an Animal simulation program
    6. Using inheritance to avoid duplicating code in subclasses
    7. Do all animals eat the same way?
      1. Which methods should we override?
    8. Looking for more inheritance opportunities
    9. Which method is called?
    10. Designing an Inheritance Tree
    11. Using IS-A and HAS-A
    12. But wait! There’s more!
    13. How do you know if you’ve got your inheritance right?
    14. When designing with inheritance, are you using or abusing?
    15. So what does all this inheritance really buy you?
    16. Inheritance lets you guarantee that all classes grouped under a certain supertype have all the methods that the supertype has*
    17. Keeping the contract: rules for overriding
    18. Overloading a method
    19. Exercise
      1. Mixed Messages
      2. BE the Compiler
    20. Exercise Solutions
      1. BE the Compiler
      2. Mixed Messages
      3. Pool Puzzle
  11. 8. Serious Polymorphism: Interfaces and Abstract Classes
    1. Did we forget about something when we designed this?
    2. The compiler won’t let you instantiate an abstract class
    3. Abstract vs. Concrete
    4. Abstract methods
    5. You MUST implement all abstract methods
    6. Polymorphism in action
    7. Uh-oh, now we need to keep Cats, too
    8. What about non-Animals? Why not make a class generic enough to take anything?
    9. So what’s in this ultra-super-megaclass Object?
    10. Using polymorphic references of type Object has a price...
    11. When a Dog won’t act like a Dog
    12. Objects don’t bark
    13. Get in touch with your inner Object
    14. Polymorphism means “many forms.”
      1. Casting an object reference back to its real type.
    15. What if you need to change the contract?
    16. Let’s explore some design options for reusing some of our existing classes in a PetShop program
    17. Interface to the rescue!
    18. Making and implementing the Pet interface
    19. Invoking the superclass version of a method
    20. Exercise
      1. Pool Puzzle
    21. Exercise Solutions
      1. What’s the Picture ?
      2. What’s the Declaration?
      3. Pool Puzzle
  12. 9. Life and Death of an Object: Constructors and Garbage Collection
    1. The Stack and the Heap: where things live
    2. Methods are stacked
      1. A stack scenario
    3. What about local variables that are objects?
    4. If local variables live on the stack, where do instance variables live?
    5. The miracle of object creation
    6. Construct a Duck
    7. Initializing the state of a new Duck
    8. Using the constructor to initialize important Duck state*
    9. Make it easy to make a Duck
      1. Be sure you have a no-arg constructor
    10. Doesn’t the compiler always make a no-arg constructor for you? No!
    11. Nanoreview: four things to remember about constructors
    12. Wait a minute...we never DID talk about superclasses and inheritance and how that all fits in with constructors
    13. The role of superclass constructors in an object’s life
    14. Making a Hippo means making the Animal and Object parts too...
    15. How do you invoke a superclass constructor?
    16. Can the child exist before the parents?
    17. Superclass constructors with arguments
    18. Invoking one overloaded constructor from another
    19. Now we know how an object is born, but how long does an object live?
    20. What about reference variables?
    21. Exercise
      1. BE the Garbage Collector
      2. Popular Objects
      3. Five-Minute Mystery
    22. Exercise Solutions
      1. Be the Garbage Collector
      2. Popular Objects
      3. Five-Minute Mystery
  13. 10. Numbers Matter: Numbers and Statics
    1. MATH methods: as close as you’ll ever get to a global method
    2. The difference between regular (non-static) and static methods
    3. What it means to have a class with static methods
    4. Static methods can’t use non-static (instance) variables!
    5. Static methods can’t use non-static methods, either!
    6. Static variable: value is the same for ALL instances of the class
      1. Initializing a static variable
    7. static final variables are constants
    8. final isn’t just for static variables...
    9. Math methods
    10. Wrapping a primitive
    11. Java will Autobox primitives for you
    12. Autoboxing works almost everywhere
    13. But wait! There’s more! Wrappers have static utility methods too!
    14. And now in reverse...turning a primitive number into a String
    15. Number formatting
    16. Formatting deconstructed...
    17. The percent (%) says, “insert argument here” (and format it using these instructions)
    18. The format String uses its own little language syntax
    19. The format specifier
    20. The only required specifier is for TYPE
    21. What happens if I have more than one argument?
    22. Just one more thing...static imports
    23. Fireside Chats
    24. Exercise
      1. BE the compiler
      2. True or False
    25. Exercise Solution
      1. Sharpen your pencil
      2. BE the compiler
      3. True or False
  14. 11. Data Structures: Collections and Generics
    1. Tracking song popularity on your jukebox
    2. Your first job, sort the songs in alphabetical order
    3. Great question! You spotted the diamond operator
    4. Exploring the java.util API, List and Collections
    5. In the “Real-World”™ there are lots of ways to sort
    6. “Natural Ordering,” what Java means by alphabetical
    7. But now you need Song objects, not just simple Strings
    8. Changing the Jukebox code to use Songs instead of Strings
    9. It won’t compile!
    10. The sort() method declaration
    11. Generics means more type-safety
    12. Learning generics
    13. Using generic CLASSES
    14. Using type parameters with ArrayList
    15. Using generic METHODS
    16. Here’s where it gets weird...
    17. Revisiting the sort() method
    18. In generics, “extends” means “extends or implements”
    19. Finally we know what’s wrong...
      1. The Song class needs to implement Comparable
    20. The new, improved, comparable Song class
    21. We can sort the list, but...
    22. Using a custom Comparator
    23. Updating the Jukebox to use a Comparator
    24. Fill-in-the-blanks
    25. But wait! We’re sorting in two different ways!
    26. Sorting using only Comparators
    27. Just the code that matters
    28. What do we REALLY need in order to sort?
    29. Enter lambdas! Leveraging what the compiler can infer
    30. Where did all that code go?
    31. Some interfaces have only ONE method to implement
    32. Updating the Jukebox code with lambdas
    33. Reverse Engineer
    34. Uh-oh. The sorting all works, but now we have duplicates...
    35. We need a Set instead of a List
    36. The Collection API (part of it)
    37. Using a HashSet instead of ArrayList
    38. What makes two objects equal?
    39. How a HashSet checks for duplicates: hashCode() and equals()
    40. The Song class with overridden hashCode() and equals()
    41. If we want the set to stay sorted, we’ve got TreeSet
    42. What you MUST know about TreeSet...
    43. TreeSet elements MUST be comparable
    44. We’ve seen Lists and Sets, now we’ll use a Map
    45. Creating and filling collections
    46. Convenience Factory Methods for Collections
    47. Finally, back to generics
      1. Using polymorphic arguments and generics
    48. But will it work with List<Dog>?
    49. What could happen if it were allowed...?
    50. We can do this with wildcards
    51. Using the method’s generic type parameter
    52. Exercise
      1. BE the Compiler, advanced
    53. Exercise Solution
      1. Fill-in-the-blanks
      2. “Reverse Engineer” lambdas exercise
      3. Sorting with lambdas
      4. TreeSet exercise
      5. BE the Compiler solution
  15. 12. Lambdas and Streams: What, Not How: Lambdas and Streams
    1. Tell the computer WHAT you want
    2. Fireside Chats
    3. When for loops go wrong
      1. Mixed Messages
    4. Small errors in common code can be hard to spot
    5. Building blocks of common operations
    6. Introducing the Streams API
    7. Getting started with Streams
    8. Streams are like recipes: nothing’s going to happen until someone actually cooks them
    9. Getting a result from a Stream
    10. Stream operations are building blocks
    11. Building blocks can be stacked and combined
    12. Customizing the building blocks
    13. Create complex pipelines block by block
    14. Yes, because Streams are lazy
    15. Terminal operations do all the work
    16. Collecting to a List
    17. Guidelines for working with streams
      1. Correct! Stream operations don’t change the original collection.
    18. Exercise
      1. Code Magnets
    19. Hello Lambda, my (not so) old friend
      1. Passing behavior around
    20. The shape of lambda expressions
    21. Anatomy of a lambda expression
    22. Variety is the spice of life
      1. A lambda might have more than one line
      2. Single-line lambdas don’t need ceremony
      3. A lambda might not return anything
      4. A lambda might have zero, one, or many parameters
    23. How can I tell if a method takes a lambda?
    24. Exercise
      1. BE the Compiler, advanced
    25. Spotting Functional Interfaces
    26. Functional interfaces in the wild
    27. Lou’s back!
    28. Exercise
    29. Ready-Bake Code
    30. Lou’s Challenge #1: Find all the “rock” songs
    31. Filter a stream to keep certain elements
    32. Let’s Rock!
    33. Getting clever with filters
    34. Lou’s Challenge #2: List all the genres
    35. Mapping from one type to another
    36. Removing duplicates
    37. Only one of every genre
    38. Just keep building!
    39. Sometimes you don’t even need a lambda expression
    40. Collecting results in different ways
    41. But wait, there’s more!
      1. Checking if something exists
      2. Find a specific thing
      3. Count the items
      4. Well, some operations may return something, or may not return anything at all
    42. Optional is a wrapper
      1. Yes, but now we have a way to ask if we have a result
    43. Don’t forget to talk to the Optional wrapper
      1. The Unexpected Coffee
      2. Five-Minute Mystery
      3. Pool Puzzle
      4. Mixed Messages
      5. Who Does What?
    44. Exercise Solutions
      1. Code Magnets
      2. BE the Compiler
      3. Sharpen your pencil
      4. Five-Minute Mystery
      5. Pool Puzzle
  16. 13. Risky Behavior: Exception Handling
    1. Let’s make a Music Machine
      1. The finished BeatBox looks something like this:
    2. We’ll start with the basics
      1. The JavaSound API
    3. First we need a Sequencer
      1. Something’s wrong!
    4. What happens when a method you want to call (probably in a class you didn’t write) is risky?
    5. Methods in Java use exceptions to tell the calling code, “Something Bad Happened. I failed.”
    6. The compiler needs to know that YOU know you’re calling a risky method
    7. An exception is an object... of type Exception
    8. If it’s your code that catches the exception, then whose code throws it?
    9. Flow control in try/catch blocks
    10. Finally: for the things you want to do no matter what
    11. Flow Control
    12. Did we mention that a method can throw more than one exception?
      1. Catching multiple exceptions
    13. Exceptions are polymorphic
    14. Multiple catch blocks must be ordered from smallest to biggest
    15. You can’t put bigger baskets above smaller baskets
    16. When you don’t want to handle an exception...
    17. Ducking (by declaring) only delays the inevitable
    18. Getting back to our music code...
      1. Exception Rules
    19. Code Kitchen
    20. Making actual sound
    21. Version 1: Your very first sound player app
    22. Making a MidiEvent (song data)
    23. MIDI message: the heart of a MidiEvent
      1. Anatomy of a message
    24. Change a message
    25. Version 2: Using command-line args to experiment with sounds
      1. Where we’re headed with the rest of the CodeKitchens
    26. Exercise
      1. True or False
      2. Code Magnets
      3. JavaCross
      4. Sharpen your pencil
    27. Exercise Solution
      1. True or False
      2. Code Magnets
      3. JavaCross
  17. 14. A Very Graphic Story: Getting Gui
    1. It all starts with a window
      1. Put widgets in the window
    2. Your first GUI: a button on a frame
    3. But nothing happens when I click it...
    4. Getting a user event
    5. Listeners, Sources, and Events
    6. Getting back to graphics...
    7. Make your own drawing widget
    8. Fun things to do in paintComponent()
    9. Behind every good Graphics reference is a Graphics2D object
    10. Because life’s too short to paint the circle a solid color when there’s a gradient blend waiting for you
    11. We can get an event. We can paint graphics. But can we paint graphics when we get an event?
    12. GUI layouts: putting more than one widget on a frame
    13. Let’s try it with TWO buttons
    14. So now we need FOUR widgets
    15. And we need to get TWO events
      1. How do you get action events for two different buttons when each button needs to do something different?
    16. Inner class to the rescue!
    17. An inner class instance must be tied to an outer class instance*
    18. How to make an instance of an inner class
      1. Java Exposed
    19. Lambdas to the rescue! (again)
    20. ActionListener is a Functional Interface
    21. Lambdas, clearer and more concise
    22. Using an inner class for animation
    23. Code Kitchen
    24. Listening for a non-GUI event
    25. An easier way to make messages/events
    26. Version One: using the new static makeEvent() method
    27. Version Two: registering and getting ControllerEvents
    28. Version Three: drawing graphics in time with the music
    29. Exercise
      1. Who Am I?
      2. BE the Compiler
      3. Pool Puzzle
    30. Exercise Solutions
      1. Who am I?
      2. BE the Compiler
      3. Pool Puzzle
  18. 15. Work on Your Swing: Using Swing
    1. Swing components
      1. Components can be nested
    2. Layout Managers
    3. How does the layout manager decide?
      1. Different layout managers have different policies
    4. The Big Three layout managers: border, flow, and box
    5. Playing with Swing components
    6. Code Kitchen
    7. Making the BeatBox
    8. Exercise
      1. Which code goes with which layout?
      2. Code Fragments
      3. GUI-Cross
    9. Exercise Solutions
      1. Which code goes with which layout?
      2. GUI-Cross
  19. 16. Saving Objects (and Text): Serialization and File I/O
    1. Capture the beat
    2. Saving state
    3. Writing a serialized object to a file
    4. Data moves in streams from one place to another
    5. What really happens to an object when it’s serialized?
    6. But what exactly IS an object’s state? What needs to be saved?
    7. If you want your class to be serializable, implement Serializable
    8. Deserialization: restoring an object
    9. What happens during deserialization?
    10. Saving and restoring the game characters
      1. The GameCharacter class
    11. Version ID: A big serialization gotcha
    12. Using the serialVersionUID
    13. Object Serialization
    14. Writing a String to a Text File
    15. Text file example: e-Flashcards
    16. Quiz Card Builder (code outline)
    17. The java.io.File class
    18. Reading from a text file
    19. Quiz Card Player (code outline)
    20. Parsing with String split()
    21. NIO.2 and the java.nio.file package
    22. Path, Paths, and Files (messing with directories)
    23. Finally, a closer look at finally
    24. Remember, finally ALWAYS runs!!
    25. Finally, a closer look at finally, cont.
    26. There IS a better way!
    27. The try-with-resources (TWR), statement
    28. Autocloseable, the very small catch
    29. Autocloseable, it’s everywhere you do I/O
    30. Code Kitchen
    31. Saving a BeatBox pattern
    32. Restoring a BeatBox pattern
    33. Can they be saved?
    34. Exercise
      1. True or False
      2. Code Magnets
    35. Exercise Solutions
      1. True or False
      2. Code Magnets
  20. 17. Make a Connection: Networking and Threads
    1. Real-time BeatBox chat
    2. Connecting, sending, and receiving
    3. 1. Connect
    4. 2. Receive
      1. Reading from the network with BufferedReader
    5. 3. Send
      1. Writing to the network with PrintWriter
    6. There’s more than one way to make a connection
      1. Using a Socket
    7. The DailyAdviceClient
    8. DailyAdviceClient code
    9. Writing a simple server application
    10. DailyAdviceServer code
    11. Writing a Chat Client
    12. The really, really simple Chat Server
    13. Java has multiple threads but only one Thread class
    14. What does it mean to have more than one call stack?
    15. To create a new call stack you need a job to run
    16. To make a job for your thread, implement the Runnable interface
    17. How NOT to run the Runnable
    18. How we used to launch a new thread
    19. A better alternative: don’t manage the Threads at all
    20. Running one job
      1. The three states of a new thread
    21. The thread scheduler
      1. How did we end up with different results?
    22. Putting a thread to sleep
    23. Using sleep to make our program more predictable
    24. There are downsides to forcing the thread to sleep
    25. Counting down until ready
    26. Making and starting two threads (or more!)
    27. Pooling Threads
    28. Running multiple threads
    29. What will happen?
    30. Closing time at the thread pool
    31. Um, yes. There IS a dark side. Multithreading can lead to concurrency “issues.”
    32. Exercise
      1. Who Am I?
    33. New and improved SimpleChatClient
    34. Exercise
      1. Code Magnets
    35. Exercise Solutions
      1. Who Am I?
      2. Code Magnets
      3. Code Kitchen
  21. 18. Dealing with Concurrency Issues: Race Conditions and Immutable Data
    1. What could possibly go wrong?
    2. The Ryan and Monica problem, in code
    3. The Ryan and Monica example
    4. We need to check the balance and spend the money as one atomic thing
    5. Using an object’s lock
    6. Using synchronized methods
    7. It’s important to lock the correct object
    8. The dreaded “Lost Update” problem
    9. Let’s run this code...
    10. Make the increment() method atomic. Synchronize it!
    11. Deadlock, a deadly side of synchronization
    12. You don’t always have to use synchronized
      1. Atomic variables
    13. Compare-and-swap with atomic variables
    14. Ryan and Monica, going atomic
    15. Writing a class for immutable data
    16. Using immutable objects
    17. Changing immutable data
    18. More problems with shared data
    19. Reading from a changing data structure causes an Exception
    20. Use a thread-safe data structure
    21. CopyOnWriteArrayList
    22. Exercise
      1. BE the JVM
      2. Five-Minute Mystery
    23. Exercise Solution
      1. BE the JVM
      2. Five-Minute Mystery
  22. A. Final Code Kitchen
    1. Final BeatBox client program
    2. Final BeatBox server program
  23. B. The top ten-ish topics that didn’t make it into the rest of the book...
    1. #11 JShell (Java REPL)
      1. Why do you care?
    2. #10 Packages
      1. Packages prevent class name conflicts
      2. Preventing package name conflicts
      3. Compiling and running with packages
    3. #9 Immutability in Strings and Wrappers
      1. Why do you care that Strings are immutable?
      2. Why do you care that Wrappers are immutable?
    4. #8 Access levels and access modifiers (who sees what)
    5. #7 Varargs
      1. Why do you care?
    6. #6 Annotations
      1. Why do you care?
    7. #5 Lambdas and Maps
      1. Why do you care?
    8. #4 Parallel Streams
      1. Going parallel
      2. OK now what?
      3. Multithreading is taken care of
      4. Do not use parallel everywhere!
    9. #3 Enumerations (also called enumerated types or enums)
      1. Who’s in the band?
      2. The old way to fake an “enum”:
    10. #2 Local Variable Type Inference (var)
      1. Type inference, NOT dynamic types
      2. Someone has to read your code
      3. Tip: Better with useful variable names
      4. Tip: Variable will be the concrete type
      5. Tip: Don’t use var with the diamond operator
    11. #1 Records
      1. Why do you care?
  24. Index
  25. Authors of Head First Java and Creators of the Head First series
  26. Co-author of Head First Java, 3rd Edition

Product information

  • Title: Head First Java, 3rd Edition
  • Author(s): Kathy Sierra, Bert Bates, Trisha Gee
  • Release date: May 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491910771