Ch 10: Numbers Matter
Source: Head First Java, Second Edition | Pages: 307-348
🎯 Learning Objectives
Static members and number handling
📚 Key Concepts
Static methods
Static variables
Math class
Wrapper classes
Autoboxing and unboxing
Number formatting
Static initialization
Constants with static final
📖 Detailed Notes
1. Static methods
Essential concept for mastering Java and OOP.
Example:
2. Static variables
Essential concept for mastering Java and OOP.
Example:
3. Math class
Essential concept for mastering Java and OOP.
Example:
4. Wrapper classes
Essential concept for mastering Java and OOP.
Example:
5. Autoboxing and unboxing
Essential concept for mastering Java and OOP.
Example:
6. Number formatting
Essential concept for mastering Java and OOP.
Example:
7. Static initialization
Essential concept for mastering Java and OOP.
Example:
8. Constants with static final
Essential concept for mastering Java and OOP.
Example:
💡 Important Points to Remember
things
one! And what about the Color
the ‘+’ operator is overloaded
that if you do put
the
✅ 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 307-348.
Chapter 10: Numbers Matter — Study Notes
This chapter focuses on static variables and methods, which exist independently of any object instance. It also covers essential number formatting, parsing, and manipulation using the Java API.
1. Static Methods
A static method is a method that runs without an instance of the class. It is called on the class itself, not on an object.
Behavior: Static methods cannot access instance variables or non-static methods because they don't "know" about any specific object on the heap.
Usage: Use
staticmethods for utility functions that don't depend on an object's state (e.g.,Math.round(),Integer.parseInt()).Syntax:
ClassName.methodName()(e.g.,Math.random()).Restriction: A static method cannot use the
thiskeyword.
2. Static Variables
A static variable is shared by all instances of a class. There is only one copy of the variable, regardless of how many objects are created.
Initialization: Static variables are initialized when the class is first loaded, before any objects are created.
Shared State: If one object changes a static variable, it changes for all other objects of that class (e.g., a
countvariable to track how manyDuckobjects exist).Constants:
static finalvariables are used as constants.Naming Convention: Constants should be in
ALL_CAPS_WITH_UNDERSCORES(e.g.,public static final double PI = 3.14159;).Final: The
finalkeyword ensures the value cannot change once assigned.
3. The Math Class
The java.lang.Math class is full of static methods for common mathematical operations.
Math.random(): Returns a double between 0.0 and 1.0 (not inclusive of 1.0).Math.abs(): Returns the absolute (positive) value of a number.Math.round(): Rounds a floating-point number to the nearest integer.Math.min()/Math.max(): Returns the smaller or larger of two numbers.
4. Wrappers
Wrapper classes allow you to treat primitive data types as objects. This is often necessary when working with collections like ArrayList, which can only hold objects.
Mapping:
int→Integerdouble→Doubleboolean→Boolean
Autoboxing / Unboxing: Since Java 5.0, the compiler automatically converts between primitives and their wrapper objects.
Autoboxing:
Integer i = 10;(wrapsint10 into anIntegerobject).Unboxing:
int x = i;(extracts theintvalue from theIntegerobject).
Parsing: Wrappers provide static utility methods to convert Strings to primitives (e.g.,
Integer.parseInt("42")).
5. Number Formatting
Java provides strong support for formatting numbers and dates.
String.format(): A static method that works likeprintfin C. It returns a formatted String.String s = String.format("%,d", 1000000);// returns "1,000,000"
Format Specifiers:
%d: Decimal integer%f: Floating point%x: Hexadecimal%c: Character%,d: Insert commas%.2f: Limit to two decimal places
Date Formatting: Using
java.util.Dateandjava.util.Calendar(or newer APIs) to represent and manipulate time.
6. Revision Checklist
Static vs. Non-Static: Do you understand why
Math.random()is static butString.substring()is not? (One performs a global utility; the other operates on specific data).Wrapper utility: Can you use
Integer.parseInt()to turn user text input into a number for math?Constants: Are you declaring your global constants as
public static final?Final variables: Remember that a
finalvariable cannot be changed. Afinalmethod cannot be overridden. Afinalclass cannot be extended.
Last updated