Java Variables, Data Types & Control Flow — The Real Starting Point
Java Variables, Data Types & Control Flow — The Real Starting Point
🎯 By the End of This Lesson
After reading through this lesson, you will be able to confidently do the following 3 things.
- ▸✅ Explain the difference between the 4 primitives used in real projects (int/double/boolean/char) out of the 8 primitive types
- ▸✅ Know when to use for-each, for, while, and switch
- ▸✅ Explain the integer division pitfall + type casting and the truncate behavior of
(int) double
Treat the learning goals as a checklist — close this lesson only when you can answer all of them.
8 Primitive Types — No Need to Memorize Them All
Java Has 2 Categories
- ▸Primitive types — types that hold the actual value. 8 kinds.
- ▸Reference types — types that hold the address of an object.
String, arrays, classes, etc.
Just memorize 4 primitives: int, double, boolean, char.
The 8 Primitives — One Table
Conclusion: At first, you only need to know int, double, boolean, and char.
Declaration and Assignment
= means "assign", not "equal" — it places the right-hand value into the left-hand variable.
Output — println vs printf
System.out.print("On one line "); // No newline
System.out.printf("Age: %d years old, Height: %.1fcm%n", age, height); // C-style formatting
``
%d = integer, %f = decimal, %s = string, %n = newline. The option %.1f` means 1 decimal place.
Type Casting — Converting Between int and double
Implicit Conversion (Automatic)
Small type → large type is converted automatically.
Explicit Conversion (Manual)
Large type → small type requires explicit casting. This is intentional — it signals potential data loss.
Specify the target type in parentheses. It truncates, not rounds — 3.99 becomes 3.
Common Pitfall — Integer Division
The most common bug. Dividing two integers drops the decimal portion.
if · switch · Loops — One-Page Reference
if / else
The condition must be a boolean. You cannot use 0/1 like in C (e.g., if (x) is not valid).
switch — When Comparing Many Values
Java 14+ introduced arrow syntax, which is much cleaner.
The old syntax (Java 13 and below) had a pitfall: forgetting break causes fall-through to the next case — use arrow syntax in new code.
for — When the Number of Iterations is Known
The order is init → condition check → body → increment → condition check .... This is called the lifecycle.
while / do-while — When You Only Have a Condition
do-while always executes once before checking the condition. Rarely used in practice.
Example — Sum of 1 to 100
Arrays — Grouping Multiple Values of the Same Type
Array Declaration and Initialization
int[] is the "int array" type. The C-style int arr[] also works but is not recommended.
Access — Indexes Start at 0
Accessing out-of-bounds throws ArrayIndexOutOfBoundsException — the most common runtime error.
Length — .length
.length is a field (no parentheses) — it is not length(). Do not confuse it with String's length().
for-each — More Concise Iteration
When you don't need the index, this is far more readable. However, since the index is unavailable, use a regular for loop when you need to modify elements.
2D Arrays
Commonly used for game boards and matrices. In production code ArrayList is used most of the time, but arrays are faster for algorithm problems.
What's Next
Variables, control flow, arrays — this is the fundamental building material of all Java code. In the next chapter on OOP, you will explore "why we need to group things into classes."
☕ Try It Live — Variables, Types, Operators & Control Flow
🤖 Try Prompting the AI Like This
Once you know the concepts in this lesson, you can give the AI specific instructions. Instead of vague requests like "fix this," you can make vocabulary-driven requests — that is where token savings begin.
- ▸"Correct the use of int vs. long and float vs. double in this Java code where appropriate."
- ▸"Convert this for loop into an enhanced for-each loop."
- ▸"Refactor this if-else chain into a switch expression (Java 14+)."
Why This Saves Tokens
Without the concepts, you have to ask "what does that mean?" after every AI response. Those follow-up questions burn tokens. Learn the concepts once and the conversation ends in one round.