Head First Java (3rd Edition) summary
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
- 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) - 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..) - Chapter 2: I was told there would be objects
What makes OO development in Java fun. Difference between a class and an object. - 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. - Chapter 4: State affects behaviour, behaviour affects state
Objects are represented by instance variables (state) and methods (behaviour). Method use instance variable value. - Chapter 5: Let’s put some muscle in our methods
Describes operators and loops. Write Battleship like game - Chapter 6: Java ships with hundreds of prebuilt classes
Don’t reinvent the wheel and learn how to use the Java API. - 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. - Chapter 8: Instance is just the beginning
Use interfaces (100% abstract class) to explore polymorphism. Abstract vs. Concrete. - 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. - Chapter 10: Do the Math
Math library, static vs non-static methods, auto-boxing, formatting. - Chapter 11: Sorting is a snap in Java
Collecting and manipulating data. The Java Collection Framework. - 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. - Chapter 13: Stuff happens
Handle exceptional situations. - Chapter 14: Face it, you need to make GUIs.
Swing, event handling and inner classes. - Chapter 15: Swing is easy.
- Chapter 16: Objects can be flattened and inflated
Preserve state by serializing and deserializing objects. - Chapter 17: Connect with the outside world
java.net
library. Sockets. Clients and servers. Build fully functional, multithreaded chat client. - Chapter 18: Doing two or more things at once is hard
Writing multithreaded code that works is hard. Create immutable objects. - Appending A
Contains all the code for the full client-server chat beat box. - 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
- Slow down. The more you understand, the less you have to memorize
- Do the exercise. Write your own notes
- Read the “There are No Dumb Questions”
- Don’t do all your reading in one place
- Make this the last thing you read before bed. Or at least the last challenging thing.
- Drink water. Lots of it.
- Talk about it. Out loud.
- Listen to your brain.
If your brain is getting overloaded, take a break!!! - Fell something!
Get involved with the stories. - 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
- 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)
— 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
— 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
— 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
— *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.