Head First Java: Chapter-10 summary

Numbers Matter

Tharaka Dissanayake
4 min readJul 18, 2022

If a class does not need an instance variable, the methods of that class can declare as static. Methods in the Math class don’t use any instance variable values. And because the methods are ‘static’, you don’t need to have an instance of Math.

. By making a class static, it can run without any instance of the class. Static means behaviour(methods) does not depend on an instance variable. They are not part of the class, not the object. So static methods can be accessed directly. No need to object. Because static methods do not depend on their instance variables.

Static methods.

Static methods run without knowing any particular instance of the static method’s class. A static method can’t refer to any instance variables of the class. The static method doesn’t know which instance’s variable value to use. Static methods can’t use non-static methods, either! Also, all instances of the same class share a single copy of the static variables. All static variables in a class are initialized before any class object can be created if we do not assign values to static variables. When the class is loaded, static variables are assigned default values.

Final variables

A variable marked final means that once initialized. It can never change.

Final static variables must be initialized. Otherwise, the compiler catches it in the compiling time. The “final” keyword can be used for static variables, instance variables, local variables, method parameters, classes, and methods. A final method means you can’t override the method. A final class means you can’t extend the class. A static method can’t access a non-static variable. But can a non-static method access a static variable

Valuable methods in Math class.

Wrapping a primitive

If we want to make primitive variables as objects wrapping classes can be used. There’s a wrapper class for every primitive type since the wrapper classes are in Java. Lang package, you don’t need to import them. You can recognize wrapper classes because each one is named after the primitive type it wraps, but with the first letter capitalized to follow the class naming convention.

Method arguments with the wrapper classes.

If a method takes a wrapper type, you can pass a reference to a wrapper or a primitive of the matching type. And, of course, the reverse is true — if a method takes a primitive, you can pass in either a compatible primitive or a reference to a wrapper of that primitive type.

Method arguments with the return values.

If a method declares a primitive return type, you can return either a compatible primitive or a reference to the wrapper of that primitive type. And if a method declares a wrapper return type, you can return either a reference to the wrapper type or a primitive of the matching type.

Method arguments with the boolean expressions.

Any place a boolean value is expected, you can use either an expression that evaluates to a boolean (4 > 2), a primitive boolean, or a reference to a Boolean wrapper.

Number formatting

With Java 5.0 version, Formatter class comes. It can be found in Java. util package.

--

--