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
- Other books in OâReillyâs Head First series
- Table of Contents (the real thing)
- How to Use This Book: Intro
- Who is this book for?
- Who should probably back away from this book?
- The way Java works
- What youâll do in Java
- A very brief history of Java
- Speed and memory usage
- What goes in a source file?
- What goes in a class?
- What goes in a method?
- Simple boolean tests
- Example of a while loop
- How it works
- Code Magnets
- BE the Compiler
- JavaCross
- Mixed Messages
- Pool Puzzle
- Sharpen your pencil
- Code Magnets
- BE the Compiler
- Pool Puzzle
- JavaCross
- Mixed Messages
- Chair Wars
- (or How Objects Can Change Your Life)
- At Lauraâs desk
- At Bradâs laptop at the cafe
- Back at Lauraâs desk
- At Bradâs laptop at the beach
- Back at Lauraâs desk
- At Bradâs laptop on his lawn chair at the Telluride Bluegrass Festival
- So, Brad the OO guy got the chair and desk, right?
- What about the Amoeba rotate()?
- The suspense is killing me. Who got the chair and desk?
- When you design a class, think about the objects that will be created from that class type. Think about:
- A class is not an object (but itâs used to construct them)
- The two uses of main:
- The Guessing Game
- BE the Compiler
- Code Magnets
- Pool Puzzle
- Who Am I?
- Code Magnets
- BE the Compiler
- Puzzle Solutions
- Who Am I?
- Declaring a variable
- âIâd like a double mocha, no, make it an int.â
- Primitive Types
- Life on the garbage-collectible heap
- Life and death on the heap
- BE the Compiler
- Code Magnets
- Pool Puzzle
- A Heap oâ Trouble
- The case of the pilfered references
- Five-Minute Mystery
- Sharpen your pencil
- Code Magnets
- BE the Compiler
- Puzzle Solutions
- Five-Minute Mystery
- A Heap oâ Trouble
- Remember: a class describes what an object knows and what an object does
- Can every object of that type have different method behavior?
- Calling a two-parameter method and sending it two arguments
- You can pass variables into a method, as long as the variable type matches the parameter type
- Do it or risk humiliation and ridicule.
- Hide the data
- BE the Compiler
- Who Am I?
- Mixed Messages
- Pool Puzzle
- Five-Minute Mystery
- Sharpen your pencil
- BE the Compiler
- Who Am I?
- Puzzle Solutions
- Five-Minute Mystery
- Mixed Messages
- Letâs build a Battleship-style game: âSink a Startupâ
- First, a high-level design
- The âSimple Startup Gameâ a gentler introduction
- Developing a Class
- The three things weâll write for each class:
- Based on this prep code:
- Hereâs what we should test:
- Gasp!
- Regular (non-enhanced) for loops
- Difference between for and while
- BE the JVM
- Code Magnets
- JavaCross
- Mixed Messages
- Be the JVM
- Code Magnets
- Puzzle Solutions
- JavaCross
- Mixed Messages
- In our last chapter, we left you with the cliff-hanger: a bug
- How itâs supposed to look
- How the bug looks
- Sharpen your pencil
- Code Magnets
- JavaCross
- Code Magnets
- JavaCross
- Chair Wars Revisited.
- What about the Amoeba rotate()?
- Understanding Inheritance
- An inheritance example:
- Letâs design the inheritance tree for an Animal simulation program
- Using inheritance to avoid duplicating code in subclasses
- Do all animals eat the same way?
- Which methods should we override?
- Mixed Messages
- BE the Compiler
- BE the Compiler
- Mixed Messages
- Pool Puzzle
- Did we forget about something when we designed this?
- The compiler wonât let you instantiate an abstract class
- Abstract vs. Concrete
- Abstract methods
- You MUST implement all abstract methods
- Polymorphism in action
- Uh-oh, now we need to keep Cats, too
- What about non-Animals? Why not make a class generic enough to take anything?
- So whatâs in this ultra-super-megaclass Object?
- Using polymorphic references of type Object has a price.
- When a Dog wonât act like a Dog
- Objects donât bark
- Get in touch with your inner Object
- Polymorphism means âmany forms.â
- Casting an object reference back to its real type.
- Pool Puzzle
- Whatâs the Picture ?
- Whatâs the Declaration?
- Pool Puzzle
- The Stack and the Heap: where things live
- Methods are stacked
- A stack scenario
- Be sure you have a no-arg constructor
- BE the Garbage Collector
- Popular Objects
- Five-Minute Mystery
- Be the Garbage Collector
- Popular Objects
- Five-Minute Mystery
- MATH methods: as close as youâll ever get to a global method
- The difference between regular (non-static) and static methods
- What it means to have a class with static methods
- Static methods canât use non-static (instance) variables!
- Static methods canât use non-static methods, either!
- Static variable: value is the same for ALL instances of the class
- Initializing a static variable
- BE the compiler
- True or False
- Sharpen your pencil
- BE the compiler
- True or False
- Tracking song popularity on your jukebox
- Your first job, sort the songs in alphabetical order
- Great question! You spotted the diamond operator
- Exploring the java.util API, List and Collections
- In the âReal-Worldâ⢠there are lots of ways to sort
- âNatural Ordering,â what Java means by alphabetical
- But now you need Song objects, not just simple Strings
- Changing the Jukebox code to use Songs instead of Strings
- It wonât compile!
- The sort() method declaration
- Generics means more type-safety
- Learning generics
- Using generic CLASSES
- Using type parameters with ArrayList
- Using generic METHODS
- Hereâs where it gets weird.
- Revisiting the sort() method
- In generics, âextendsâ means âextends or implementsâ
- Finally we know whatâs wrong.
- The Song class needs to implement Comparable
- Using polymorphic arguments and generics
- BE the Compiler, advanced
- Fill-in-the-blanks
- âReverse Engineerâ lambdas exercise
- Sorting with lambdas
- TreeSet exercise
- BE the Compiler solution
- Tell the computer WHAT you want
- Fireside Chats
- When for loops go wrong
- Mixed Messages
- Correct! Stream operations donât change the original collection.
- Code Magnets
- Passing behavior around
- A lambda might have more than one line
- Single-line lambdas donât need ceremony
- A lambda might not return anything
- A lambda might have zero, one, or many parameters
- BE the Compiler, advanced
- Checking if something exists
- Find a specific thing
- Count the items
- Well, some operations may return something, or may not return anything at all
- Yes, but now we have a way to ask if we have a result
- The Unexpected Coffee
- Five-Minute Mystery
- Pool Puzzle
- Mixed Messages
- Who Does What?
- Code Magnets
- BE the Compiler
- Sharpen your pencil
- Five-Minute Mystery
- Pool Puzzle
- Letâs make a Music Machine
- The finished BeatBox looks something like this:
- The JavaSound API
- Somethingâs wrong!
- Catching multiple exceptions
- Exception Rules
- Anatomy of a message
- Where weâre headed with the rest of the CodeKitchens
- True or False
- Code Magnets
- JavaCross
- Sharpen your pencil
- True or False
- Code Magnets
- JavaCross
- It all starts with a window
- Put widgets in the window
- How do you get action events for two different buttons when each button needs to do something different?
- Java Exposed
- Who Am I?
- BE the Compiler
- Pool Puzzle
- Who am I?
- BE the Compiler
- Pool Puzzle
- Swing components
- Components can be nested
- Different layout managers have different policies
- Which code goes with which layout?
- Code Fragments
- GUI-Cross
- Which code goes with which layout?
- GUI-Cross
- Capture the beat
- Saving state
- Writing a serialized object to a file
- Data moves in streams from one place to another
- What really happens to an object when itâs serialized?
- But what exactly IS an objectâs state? What needs to be saved?
- If you want your class to be serializable, implement Serializable
- Deserialization: restoring an object
- What happens during deserialization?
- Saving and restoring the game characters
- The GameCharacter class
- True or False
- Code Magnets
- True or False
- Code Magnets
- Real-time BeatBox chat
- Connecting, sending, and receiving
- 1. Connect
- 2. Receive
- Reading from the network with BufferedReader
- Writing to the network with PrintWriter
- Using a Socket
- The three states of a new thread
- How did we end up with different results?
- Who Am I?
- Code Magnets
- Who Am I?
- Code Magnets
- Code Kitchen
- What could possibly go wrong?
- The Ryan and Monica problem, in code
- The Ryan and Monica example
- We need to check the balance and spend the money as one atomic thing
- Using an objectâs lock
- Using synchronized methods
- Itâs important to lock the correct object
- The dreaded âLost Updateâ problem
- Letâs run this code.
- Make the increment() method atomic. Synchronize it!
- Deadlock, a deadly side of synchronization
- You donât always have to use synchronized
- Atomic variables
- BE the JVM
- Five-Minute Mystery
- BE the JVM
- Five-Minute Mystery
- Final BeatBox client program
- Final BeatBox server program
- #11 JShell (Java REPL)
- Why do you care?
- Packages prevent class name conflicts
- Preventing package name conflicts
- Compiling and running with packages
- Why do you care that Strings are immutable?
- Why do you care that Wrappers are immutable?
- Why do you care?
- Why do you care?
- Why do you care?
- Going parallel
- OK now what?
- Multithreading is taken care of
- Do not use parallel everywhere!
- Whoâs in the band?
- The old way to fake an âenumâ:
- Type inference, NOT dynamic types
- Someone has to read your code
- Tip: Better with useful variable names
- Tip: Variable will be the concrete type
- Tip: Donât use var with the diamond operator
- 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