Ch 03: Primitives and References
Source: Head First Java, Second Edition | Pages: 83-104
π― Learning Objectives
Understanding variables, memory, and references
π Key Concepts
Primitive types (int, boolean, etc.)
Reference variables
Object references vs primitives
Arrays fundamentals
Heap vs Stack memory
Garbage collection intro
null references
π Detailed Notes
1. Primitive types (int, boolean, etc.)
Essential concept for mastering Java and OOP.
Example:
2. Reference variables
Essential concept for mastering Java and OOP.
Example:
3. Object references vs primitives
Essential concept for mastering Java and OOP.
Example:
4. Arrays fundamentals
Essential concept for mastering Java and OOP.
Example:
5. Heap vs Stack memory
Essential concept for mastering Java and OOP.
Example:
6. Garbage collection intro
Essential concept for mastering Java and OOP.
Example:
7. null references
Essential concept for mastering Java and OOP.
Example:
π‘ Important Points to Remember
that a reference variable
that the compiler wonβt let you
We have to
When you see a statement like: βan object
the βfβ. Gotta have that
β
Self-Check Questions
Test your understanding:
Can you explain the main concepts covered in this chapter?
Can you write code examples demonstrating these concepts?
Do you understand when and why to use these features?
Can you explain the benefits and tradeoffs?
π Quick Revision Points
π Practice Exercises
Write your own code examples for each key concept
Modify existing examples to test edge cases
Explain concepts to someone else
Create a small project using these concepts
π Related Chapters
Review related concepts from other chapters to build comprehensive understanding.
For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 83-104.
Chapter 3: Know Your Variables β Study Notes
This chapter explores how Java manages data using Variables. It covers the two main types of variables (primitives and references), how they are stored in memory, and the rules for declaring them.
1. Variable Fundamentals
A variable is essentially a container (think of it as a "cup") that holds something. Every variable in Java must follow two strict rules:
Must have a type: Java is a "strongly-typed" language. You cannot put a "Giraffe" value into a "Rabbit" variable.
Must have a name: This name allows you to use and track the variable in your code.
2. Primitive Types
Primitives hold fundamental values as simple bit patterns. They come in different "cup sizes" depending on the data they need to store.
Type
Bit Depth
Value Range
boolean
JVM-specific
true or false
char
16 bits
0 to 65535
byte
8 bits
-128 to 127
short
16 bits
-32,768 to 32,767
int
32 bits
-2,147,483,648 to 2,147,483,647
long
64 bits
Extremely large range
float
32 bits
Floating point (use 'f' suffix, e.g., 3.2f)
double
64 bits
Large floating point (standard default)
3. Object Reference Variables
There is no such thing as an "object variable." There are only object reference variables.
The Remote Control Analogy: Think of a reference variable as a remote control to an object.
Storage: While the reference variable lives on the Stack (if it's a local variable), the actual object always lives on the Heap.
Reference Value: The "bits" inside a reference variable represent a way to get to a specific object on the heap.
Example:
Java
Dog myDog: Declares a reference variable (the remote control).new Dog(): Creates a new Dog object on the Heap.=: Links the reference variable to the new object.
4. Array Variables
An array is an object, even if it is an array of primitives.
Declaration:
int[] nums;(declares a reference to an array).Creation:
nums = new int[7];(creates an array object that can hold 7 ints).Access: Use the index starting at 0 (e.g.,
nums[0] = 6;).
5. Memory Management: The Heap and The Stack
The Stack: Where method calls and local variables live. When a method is called, its "stack frame" is pushed onto the stack. When it finishes, the frame is popped off, and all local variables in that frame die.
The Heap: Where all objects live.
Garbage Collection (GC): An object is eligible for GC when it has no more "live" references. This happens if:
The reference goes out of scope.
The reference is assigned to another object.
The reference is explicitly set to
null.
6. Revision Checklist
Naming Rules: Variables must start with a letter, underscore (
_), or dollar sign ($); they cannot start with a digit.Type Safety: You cannot assign a large type (like
long) to a smaller type (likeint) without an explicit cast, as this could cause "spillage".Primitive vs. Reference: Remember that primitives hold the actual value, while references hold the address of the object.
Wrapper Classes: If you need to treat a primitive like an object (e.g., for an
ArrayList), use its Wrapper class (e.g.,Integerforint).
Last updated