Head First Java Chapter 05 summary

Udesh Kavinda
4 min readNov 17, 2021

Developing a Class.

  • Figure out what the class is supposed to do.
  • List the instance variables and methods.
  • Write prepcode for the methods.
  • Write test code for the methods.
  • Implement the class.
  • Test the methods.
  • Debug and reimplement as needed

Pre-increment and Post-increment

Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.

a = ++x;

Post-increment operator: A post-increment operator is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.

a = x++;

For Loops

Regular (non-enhanced) for loops.

for(int i =0; i < 100; i++)

Part One: initialization.

int i =0;

Part two: boolean test.

i < 100;

Part three: iteration expression

i++;

The enhanced for loop

for(String name : nameArray){}

Part One: iteration variable declaration

String name

Part two: The Actual collection

nameArray

Jump Statements — Break and Continue with Labels in Java

The break and continue statements are the jump statements that are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression. These statements can be used inside any loops such as for, while, do-while loop.

The labelled break statement — The labeled break statement terminates the outer most loop (loop1) whereas the normal break statement terminates the innermost loop.

The labelled continue statement — The labelled continue statement skips the current iteration of the outer most loop (loop2) whereas the normal continue skips the current iteration of the innermost loop.

Java Type Casting

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting:

  • Widening Casting (automatically) — converting a smaller type to a larger type size
  • Narrowing Casting (manually) — converting a larger type to a smaller size type

How do I convert a String to an int in Java?

a method in the Integer class that knows how to “parse” a String into the int it represents

Bullet Points

  • Your java program should start with a high level design.
  • Typically you’ll write three things when you create a new class: prepcode | testcode | real(java) code.
  • Prepcode should describe what to do, not how to do it. Implementation comes later.
  • Use the prepcode to help design the test code.
  • Write test code before you implement the methods.
  • Choose for loops over while loops when you know how many times you want to repeat the loop code.
  • Use the pre/post increment operator to add 1 to a variable (x++)
  • Use the pre/post decrement operator to subtract 1 from a variable (x–).
  • Use Integer.parseInt() to get the int value of a string.
  • Integer.parseInt() works only if the String represents a digit (“0″,”1”, etc).
  • Use break to leave a loop early.

--

--