Head First Java, 3rd Edition

Head First Java, 3rd Edition

Read it now on the O’Reilly learning platform with a 10-day free trial.

O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

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.

Show and hide more

Publisher resources

Table of contents Product information

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?
      1. The way Java works
      2. What you’ll do in Java
      3. A very brief history of Java
        1. Speed and memory usage
        1. What goes in a source file?
        2. What goes in a class?
        3. What goes in a method?
        1. Simple boolean tests
        2. Example of a while loop
        1. How it works
        1. Code Magnets
        2. BE the Compiler
        3. JavaCross
        4. Mixed Messages
        5. Pool Puzzle
        1. Sharpen your pencil
        2. Code Magnets
        3. BE the Compiler
        4. Pool Puzzle
        5. JavaCross
        6. Mixed Messages
        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:
          1. A class is not an object (but it’s used to construct them)
          1. The two uses of main:
          2. The Guessing Game
          1. BE the Compiler
          2. Code Magnets
          3. Pool Puzzle
          4. Who Am I?
          1. Code Magnets
          2. BE the Compiler
          3. Puzzle Solutions
          4. Who Am I?
          1. Declaring a variable
          2. “I’d like a double mocha, no, make it an int.”
            1. Primitive Types
            1. Life on the garbage-collectible heap
            2. Life and death on the heap
            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
            1. Sharpen your pencil
            2. Code Magnets
            3. BE the Compiler
            4. Puzzle Solutions
            5. Five-Minute Mystery
            6. A Heap o’ Trouble
            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?
              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
              1. Do it or risk humiliation and ridicule.
              2. Hide the data
              1. BE the Compiler
              2. Who Am I?
              3. Mixed Messages
              4. Pool Puzzle
              5. Five-Minute Mystery
              1. Sharpen your pencil
              2. BE the Compiler
              3. Who Am I?
              4. Puzzle Solutions
              5. Five-Minute Mystery
              6. Mixed Messages
              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:
                1. Based on this prep code:
                2. Here’s what we should test:
                1. Gasp!
                1. Regular (non-enhanced) for loops
                1. Difference between for and while
                1. BE the JVM
                2. Code Magnets
                3. JavaCross
                4. Mixed Messages
                1. Be the JVM
                2. Code Magnets
                3. Puzzle Solutions
                4. JavaCross
                5. Mixed Messages
                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
                  1. Sharpen your pencil
                  1. Code Magnets
                  2. JavaCross
                  1. Code Magnets
                  2. JavaCross
                  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?
                    1. Mixed Messages
                    2. BE the Compiler
                    1. BE the Compiler
                    2. Mixed Messages
                    3. Pool Puzzle
                    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.
                      1. Pool Puzzle
                      1. What’s the Picture ?
                      2. What’s the Declaration?
                      3. Pool Puzzle
                      1. The Stack and the Heap: where things live
                      2. Methods are stacked
                        1. A stack scenario
                        1. Be sure you have a no-arg constructor
                        1. BE the Garbage Collector
                        2. Popular Objects
                        3. Five-Minute Mystery
                        1. Be the Garbage Collector
                        2. Popular Objects
                        3. Five-Minute Mystery
                        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
                          1. BE the compiler
                          2. True or False
                          1. Sharpen your pencil
                          2. BE the compiler
                          3. True or False
                          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
                            1. Using polymorphic arguments and generics
                            1. BE the Compiler, advanced
                            1. Fill-in-the-blanks
                            2. “Reverse Engineer” lambdas exercise
                            3. Sorting with lambdas
                            4. TreeSet exercise
                            5. BE the Compiler solution
                            1. Tell the computer WHAT you want
                            2. Fireside Chats
                            3. When for loops go wrong
                              1. Mixed Messages
                              1. Correct! Stream operations don’t change the original collection.
                              1. Code Magnets
                              1. Passing behavior around
                              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
                              1. BE the Compiler, advanced
                              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
                              1. Yes, but now we have a way to ask if we have a result
                              1. The Unexpected Coffee
                              2. Five-Minute Mystery
                              3. Pool Puzzle
                              4. Mixed Messages
                              5. Who Does What?
                              1. Code Magnets
                              2. BE the Compiler
                              3. Sharpen your pencil
                              4. Five-Minute Mystery
                              5. Pool Puzzle
                              1. Let’s make a Music Machine
                                1. The finished BeatBox looks something like this:
                                1. The JavaSound API
                                1. Something’s wrong!
                                1. Catching multiple exceptions
                                1. Exception Rules
                                1. Anatomy of a message
                                1. Where we’re headed with the rest of the CodeKitchens
                                1. True or False
                                2. Code Magnets
                                3. JavaCross
                                4. Sharpen your pencil
                                1. True or False
                                2. Code Magnets
                                3. JavaCross
                                1. It all starts with a window
                                  1. Put widgets in the window
                                  1. How do you get action events for two different buttons when each button needs to do something different?
                                  1. Java Exposed
                                  1. Who Am I?
                                  2. BE the Compiler
                                  3. Pool Puzzle
                                  1. Who am I?
                                  2. BE the Compiler
                                  3. Pool Puzzle
                                  1. Swing components
                                    1. Components can be nested
                                    1. Different layout managers have different policies
                                    1. Which code goes with which layout?
                                    2. Code Fragments
                                    3. GUI-Cross
                                    1. Which code goes with which layout?
                                    2. GUI-Cross
                                    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
                                      1. True or False
                                      2. Code Magnets
                                      1. True or False
                                      2. Code Magnets
                                      1. Real-time BeatBox chat
                                      2. Connecting, sending, and receiving
                                      3. 1. Connect
                                      4. 2. Receive
                                        1. Reading from the network with BufferedReader
                                        1. Writing to the network with PrintWriter
                                        1. Using a Socket
                                        1. The three states of a new thread
                                        1. How did we end up with different results?
                                        1. Who Am I?
                                        1. Code Magnets
                                        1. Who Am I?
                                        2. Code Magnets
                                        3. Code Kitchen
                                        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
                                          1. BE the JVM
                                          2. Five-Minute Mystery
                                          1. BE the JVM
                                          2. Five-Minute Mystery
                                          1. Final BeatBox client program
                                          2. Final BeatBox server program
                                          1. #11 JShell (Java REPL)
                                            1. Why do you care?
                                            1. Packages prevent class name conflicts
                                            2. Preventing package name conflicts
                                            3. Compiling and running with packages
                                            1. Why do you care that Strings are immutable?
                                            2. Why do you care that Wrappers are immutable?
                                            1. Why do you care?
                                            1. Why do you care?
                                            1. Why do you care?
                                            1. Going parallel
                                            2. OK now what?
                                            3. Multithreading is taken care of
                                            4. Do not use parallel everywhere!
                                            1. Who’s in the band?
                                            2. The old way to fake an “enum”:
                                            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
                                            1. Why do you care?
                                            Show and hide more

                                            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