Head First Java (3rd Edition) summary

Dalibor Plavcic
9 min readMay 2, 2023

--

https://www.oreilly.com/library/view/head-first-java/9781492091646/

Head First Java, 3rd edition is an engaging, accessible, and comprehensive guide to Java programming that is suitable for both beginners and experienced programmers.

Table of Contents

  1. How to Use This Book: Intro
    How to trick you brain into thinking that your life depends on knowing Java? (start working as a Contractor)
  2. Chapter 1: Java takes you to a new places
    Object-oriented (OO) features, memory management, portability, friendly syntax.
    Write, compile and run some code (integer, loops, branching..)
  3. Chapter 2: I was told there would be objects
    What makes OO development in Java fun. Difference between a class and an object.
  4. Chapter 3: Variables come in two flavors: primitive and reference
    What can be declared as a variable. What can be put in a variable, and what can we do with a variable. Garbage collector overview.
  5. Chapter 4: State affects behaviour, behaviour affects state
    Objects are represented by instance variables (state) and methods (behaviour). Method use instance variable value.
  6. Chapter 5: Let’s put some muscle in our methods
    Describes operators and loops. Write Battleship like game
  7. Chapter 6: Java ships with hundreds of prebuilt classes
    Don’t reinvent the wheel and learn how to use the Java API.
  8. Chapter 7: Plan your program with the future in mind
    Write code that is easy to extend. Inheritance, polymorphism, and IS-A and HAS-A relationship.
  9. Chapter 8: Instance is just the beginning
    Use interfaces (100% abstract class) to explore polymorphism. Abstract vs. Concrete.
  10. Chapter 9: Objects are born and objects die
    How objects are created, where they live, and how to keep or abandon them efficiently. Heap, stack, scope, constructor, super constructor, null references, and GC.
  11. Chapter 10: Do the Math
    Math library, static vs non-static methods, auto-boxing, formatting.
  12. Chapter 11: Sorting is a snap in Java
    Collecting and manipulating data. The Java Collection Framework.
  13. Chapter 12: What if…you didn’t need to tell the computer HOW to do something?
    Streams API. Lambda expressions. Tell computer WHAT to do.
  14. Chapter 13: Stuff happens
    Handle exceptional situations.
  15. Chapter 14: Face it, you need to make GUIs.
    Swing, event handling and inner classes.
  16. Chapter 15: Swing is easy.
  17. Chapter 16: Objects can be flattened and inflated
    Preserve state by serializing and deserializing objects.
  18. Chapter 17: Connect with the outside world
    java.net library. Sockets. Clients and servers. Build fully functional, multithreaded chat client.
  19. Chapter 18: Doing two or more things at once is hard
    Writing multithreaded code that works is hard. Create immutable objects.
  20. Appending A
    Contains all the code for the full client-server chat beat box.
  21. Appendix B
    Top ten-ish topics that didn’t make it into the book.
    Jshell, Packages, Immutability in Strings and Wrappers, Access levels and access modifiers, varargs, annotations, lambdas and Maps, parallel streams, enumerations, local variable type inference (var), records.

How to Use This Book: Intro

Provides us with guidance on how to get the most out of the book.

Learning takes a lot more than text on pages. To help turn you brain on, following learning principles are applied:

  • make it visual — images are more memorable than words alone, making learning more effective, and easier to understand
  • use a conversational and personalised style — study shows students perform up to 40% better on post-learning tests if the content spoke directly to the student, using a first person, conversational style
  • get the learner to think more deeply — keep reader motivated, engaged, curious, and inspired to solve problems…by providing challenges exercises, thought-provoking questions
  • get and keep reader’s attention
  • touch their emotions

Metacognition — thinking about thinking

  • pay attention to how you pay attention, think about how you think
  • get you brain to see the new material as crucial to your well-being
  • repetition vs anything that increases brain activity
  • you learn more and remember more when you do things than when you read about things
  • multiple learning styles are applied to show the same content from multiple points of view
  • challenges in combination with exercises and questions

What you can do to bend your brain into submission

  1. Slow down. The more you understand, the less you have to memorize
  2. Do the exercise. Write your own notes
  3. Read the “There are No Dumb Questions”
  4. Don’t do all your reading in one place
  5. Make this the last thing you read before bed. Or at least the last challenging thing.
  6. Drink water. Lots of it.
  7. Talk about it. Out loud.
  8. Listen to your brain.
    If your brain is getting overloaded, take a break!!!
  9. Fell something!
    Get involved with the stories.
  10. Type and run the code

What you need for this book

  • Basic text editor
  • JDK 8 or JDK 11
  • add Java to a classpath (you should be able to use javac from the command line

Last-minute things you need to know

  • simple UML-like diagrams are used
Source / Author: https://learning.oreilly.com/library/view/head-first-java/9781492091646/preface03.html#hereapostrophes_what_you_can_do_to_bend
  • no need to worry about organising and packaging code
  • no need to know Maven or Grade
  • the end-of-chapter exercises are mandatory, puzzles are optional
  • the “Sharpen your pencil” exercises don’t all have answers
  • the code examples are as lean as possible

Technical Reviewers for the 3rd edition

  • Marc Loy —
  • Abraham Marin-Perez —
  • Zan McQuade
  • Nicole Tache
  • and many others

Chapter 1. Breaking the Surface: Dive In: A Quick Dip

The way Java works
- write once / run-anywhere
- source -> compiler -> output -> virtual machines

- old code can still run on modern JVMs
- almost as fast as C or Rust, but uses more memory*
- JVM can optimize your code while it’s running
- Java is released every 6 months
- in source file we put class (typically one class definition)
- in class we put methods
- in methods we put statements (instructions how something should be done)
- JVM looks for a special class called public static void main(String[] args)
- running a program means telling the JVM to “load” class and execute it’s main method
- compiler is a first line of defence against violations and produces byte-code
- JVM runs produced byte-code
- looping constructs include while, do-while and for
- assignment operator ( = ) vs equals operator ( ==)
- Java is object-oriented (OO) language, everything is in a class, which is a blueprint for an object
- boolean and integer are not compatible types in Java (even though JVM spec treats bool as int)
- while loop ( while (condition == true) { do... } )
- conditional branching ( if/elseif/else )
- you can use just a tiny part of the Java platform to run on smaller devices (e.g. IoT)
- implement random word generator (3 lists, pick one random word from each list and concatenate words)

Exercise
- code magnets → arrange code
- BE the Compiler → determine whether each file compiles
- JavaCross → crossword
- Mixed Messages → update code with proposed values
- Pool Puzzle → place code snippets into right place

Chapter 2. A Trip to Objectville: Classes and Objects

— difference between a class and an object
- procedural vs. object oriented
- procedural -> what procedures do we need (rotate and playSound AIF sound)
- object oriented -> what are the things/classes in this program…who are the key players (The Shapes , User , Sound , Click event)
- specification change impact -> we need to add new shape and play .mp3
sound
— procedural → becomes messy
— object oriented → flexible, extensible, inheritance (superclass — Shape, subclass — Square, Circle, Triangle, Amoeba)
— overriding a method changes it’s inherited behaviour (at runtime, JVM knows which method to invoke)

Source/Author: https://www.oreilly.com/library/view/head-first-java/9781492091646/
Source/Author: https://www.oreilly.com/library/view/head-first-java/9781492091646/

— when designing a class think about things the object knows about itself (instance variables — represent state), and things the object does (methods)
- a class is a blueprint for an object
- object has instance variables and method which are designed as part of the class

Source/Author: https://www.oreilly.com/library/view/head-first-java/9781492091646/

— a class tells JVM how to make an object of particular type
- each object has it’s own values for the instance variables
- create a new class + create a new test class (class with the main method)

class DogTestDrive {
public static void main(String[] args) {
Dog d = new Dog();
d.size = 40;
d.bark();
}
}

. operator gives you access to an object’s state and behaviour
- two uses of main are to test your real class and/or to launch/start your Java application
- Java application is objects talking to other objects
- Guessing game implementation that generates a random number between 0 and 9, and the three players try to guess
- GuesGame.class , Player.class , GameLauncher.class

Source/Author: https://www.oreilly.com/library/view/head-first-java/9781492091646/

— each object is created on The Heap (aka. Garbage-Collectible Heap)
- when object is no logner used, it becomes eligable for garbage collection
- if JVM is running low on memory, Garbage Collector will run to free up the space
- “global” variables/methods are represented as public static
- JAR (Java ARchive) file is used to deliver multiple files to the end user
- manifest file inside JAR file defines which class in the jar holds the main() method that should run

Chapter 3. Know Your Variables: Primitives and References

-variables (container) store two things, primitives and reference
- variables
— instance (object state), local (within a method), arguments (sent to a method by the calling code), return types (sent back..)

- primitive vars
holds fundamental values (int, boolean, float..)
— have fixed size (number of bits)
— be sure the value can fit into the variable
I'd like an int please, with the value of 222, and name the variable height
— naming rules (start with a letter, underscore or dollar sign, can’t start with a number, no Java reserver words as var name)

- object reference variable holds the references to objects
— think of a reference variable as a remote control. You use it to get the object to do something (invoke methods)
. operator is used on the reference variable to make object do something myDog.bark() -> like pressing a button on the remote controller for that object

-Object Reference interview
— object reference is a remote control, and can be programmed to control different objects
— once declared, can’t be redeclared
— once declared, can be redirected to control different object (unless marked as final )
null obj. reference is like a remote control that is not programmed to control any specific TV
— assigning the value of existing variable to a new variable copies existing variable bits and assigns them to a new variable (e.g. two remotes programmed for one TV)

-Life on the garbage-collectible heap
— if no references point to the object, object is eligible to be GC

-An array is like a tray of cups (`array` is an object)
— quick, ordered, efficient list of things
— fast random access by using index position
— every element in the array is just a variable (might be reference variable)
int[] nums is a remote controller to an array object
— arrays are always objects, whether they’re declared to hold primitive or object references
— once declared, you can put things that are of a compatible array type in it

Source/Author: https://www.oreilly.com/library/view/head-first-java/9781492091646/

— *string is a special type of object that can be created and assigned as if it is primitive

PS:

  • This is work in progress that is updated on weekly basis.

--

--

Dalibor Plavcic

Delivering custom-built software solutions | Contractor | Senior Java Software Engineer (Java/Spring/AWS) | Malmö, Sweden