Phase 1: Foundations
Part 1: The Machine & The Memory
1. The Java Trinity: JDK, JRE, and JVM
In layman's terms, think of Java as a Global Restaurant Chain.
• JDK (Java Development Kit): This is the Chef’s Kitchen. It contains the tools (Compiler)
to turn raw ingredients (your code) into a dish (the program).
• JRE (Java Runtime Environment): This is the Dining Room. It contains everything a
customer needs to eat the dish (run the program), like forks, spoons, and a table (Libraries).
• JVM (Java Virtual Machine): This is the Universal Translator. Every computer
(Windows, Mac, Linux) is a different "planet." The JVM sits on that planet and translates the
universal "Java Dish" into the local language.
• The Magic: You write code once (.java file), compile it into Bytecode (.class file),
and the JVM runs it anywhere.
2. Memory Management: Stack vs. Heap
Java manages your computer's brain (RAM) in two specific ways:
• The Stack (The Fast Countertop):
• Stores Primitive Variables (small numbers, booleans).
• Stores Method Calls (which line of code is running right now).
• Behavior: When a method finishes, the memory is cleared instantly. It’s very fast but
small.
• The Heap (The Big Pantry):
• Stores Objects and Strings (large, complex data).
• Behavior: Data stays here as long as your program needs it.
• Garbage Collection: Java has a background "Cleaning Crew." If it sees an object in
the Heap that is no longer being used, it deletes it automatically to save memory.
Part 2: The 8 Primitive Data Types
Java is "Statically Typed." This means every piece of data must have a label. You cannot put a
"Word" into a "Number" box.
A. Whole Numbers (The Integer Family)
1. byte: (8-bit) Range: -128 to 127. Use for small things like age or floor numbers.
2. short: (16-bit) Range: -32,768 to 32,767. Rarely used today, but good for saving memory in
old hardware.
3. int: (32-bit) Range: Up to ~2 billion. The default choice for most whole numbers.
4. long: (64-bit) Range: Massive (Quintillions). Use for timestamps or world population.
• Rule: You must add an L at the end: long debt = 9000000000L;
B. Decimals (The Floating-Point Family)
5. float: (32-bit) Precise up to 7 decimal digits.
• Rule: You must add an f at the end: float price = 10.99f;
6. double: (64-bit) Precise up to 15 decimal digits. The default choice for math/money.
C. The Others
7. boolean: (1-bit) Only true or false.
8. char: (16-bit) Stores a single character/letter using Unicode (e.g., 'A', '%'). Use single
quotes.
Part 3: Non-Primitive - The String
While there are only 8 primitives, the String is a "Special Object."
• String: Used for text (e.g., "Hello").
• Crucial Difference: Primitives live on the Stack. Strings live in the Heap (specifically in a
place called the String Constant Pool to save space).
Step 1: Your Detailed Code Practice
Create a class called FoundationDetail. Copy this and look at the comments:
java
public class FoundationDetail {
public static void main(String[] args) {
// Integer Family
byte myAge = 25;
int salary = 85000;
long globalDebt = 3000000000000L; // L is required
// Decimal Family
float piSmall = 3.14f; // f is required
double piPrecise = 3.141592653589793;
// Logic & Text
boolean isJavaFun = true;
char initial = 'J';
String fullName = "Java Learner 2026";
[Link]("Name: " + fullName);
[Link]("Age: " + myAge);
[Link]("Pi: " + piPrecise);
}
}
Part 4: Type Casting & Operators.
This is where we learn how to "convert" between those storage jars and how to perform math
and logic.
1. Type Casting (Moving data between different Jars)
In Java, you often need to move a value from one data type to another. There are two ways to do
this:
A. Widening Casting (Automatically)
This happens when you move a small type to a larger type. Since the larger jar can easily fit the
smaller value, Java does this for you.
• Example: Putting a byte into an int.
• Real-life: Pouring a cup of water into a 5-gallon bucket. No spills!
•
java
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
// Result: 9.0
Use code with caution.
B. Narrowing Casting (Manually)
This happens when you move a larger type to a smaller type. Since data might be lost (like
decimals being chopped off), Java forces you to do it manually by placing the type in parentheses
().
• Example: Putting a double into an int.
• Real-life: Trying to pour a 5-gallon bucket into a small cup. You will lose some water!
•
java
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
// Result: 9 (The .78 is deleted/lost forever)
Use code with caution.
2. Operators (The "Verbs" of Java)
Operators are symbols used to perform operations on variables.
A. Arithmetic Operators
Standard math: + (Add), - (Subtract), * (Multiply), / (Divide).
• The Modulus %: This gives you the remainder of a division.
• Example: 10 % 3 is 1 (Because 3 goes into 10 three times, with 1 left over).
• Layman Use: Checking if a number is Even or Odd (e.g., num % 2 == 0).
•
B. Comparison Operators
Used to compare two jars. They always return a boolean (true or false).
• == (Equal to) — Note: Double equals is for comparison, single = is for assignment.
• != (Not equal to)
• > (Greater than), < (Less than)
•
C. Logical Operators
Used to combine multiple conditions.
• && (AND): Both must be true (e.g., "I have money AND the store is open").
• || (OR): At least one must be true (e.g., "I have a credit card OR I have cash").
• ! (NOT): Reverses the result (e.g., "NOT true" becomes false).
•
💻 Practice Exercise: The "Exchange"
Try to predict what happens in this code:
java
public class CastingPractice {
public static void main(String[] args) {
// 1. Manual Narrowing (Data Loss)
double fuelPrice = 150.95;
int roundedPrice = (int) fuelPrice;
// 2. Modulus (Remainder)
int items = 11;
int leftovers = items % 2;
// 3. Logic
boolean canBuy = (roundedPrice < 200) && (leftovers > 0);
[Link]("Rounded: " + roundedPrice); // Output: 150
[Link]("Leftovers: " + leftovers); // Output: 1
[Link]("Decision: " + canBuy); // Output: true
}
}
Part 5: Control Flow (Decision Making).
In layman's terms, this is the "Brain" of your program. Without this, your code just runs top-
to-bottom like a list; with this, your code can "think" and choose different paths.
1. The if-else Statement (The Fork in the Road)
This is the most common way to make a decision. It evaluates a boolean (true/false) condition.
• Layman Example: "If I have more than $5, buy a coffee. Otherwise, drink water."
•
java
int money = 10;
if (money > 5) {
[Link]("Buy Coffee");
} else {
[Link]("Drink Water");
}
Use code with caution.
2. The else if (Multiple Choices)
If you have more than two possible outcomes, you chain them together.
java
int score = 85;
if (score >= 90) {
[Link]("Grade: A");
} else if (score >= 80) {
[Link]("Grade: B"); // This will run
} else {
[Link]("Grade: C");
}
Use code with caution.
3. The switch Statement (The Menu)
When you have one variable and many specific values to check against, a switch is much cleaner
than many if-else blocks.
• Crucial Rule: Every "case" needs a break; or the code will "fall through" and execute the
next case too!
•
java
int day = 2;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday"); // This runs
break;
default:
[Link]("Other day");
}
Use code with caution.
4. Modern Java 2026: The Switch Expression
In modern Java, the switch has become much more powerful. You can now use it to return a value
directly using the -> (arrow) syntax. This is the professional way to write it today:
java
String dayType = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
case 6, 7 -> "Weekend";
default -> "Invalid Day";
};
[Link](dayType);
Use code with caution.
5. The Ternary Operator (The One-Liner)
This is a "short-hand" version of if-else. It's great for simple assignments.
• Syntax: variable = (condition) ? value_if_true : value_if_false;
•
java
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
[Link](status); // Output: Adult
Use code with caution.
💻 Step 1 (Practice Task)
Try to write a small program in your head or on your computer:
1. Create an int variable called hour (e.g., 14 for 2:00 PM).
2. If hour is less than 12, print "Good Morning".
3. If hour is between 12 and 17, print "Good Afternoon".
4. Otherwise, print "Good Evening".
Part 6: Loops (The "Repeat" Button).
In 2026, automation is everything. Loops allow you to execute a block of code repeatedly as long as
a specific condition is met.
1. The while Loop (Repeat until... )
Think of this like a Security Guard. He stays at his post while the building is open. He checks the
condition before doing anything.
java
int count = 1;
while (count <= 3) {
[Link]("Guard checking door... " + count);
count++; // Important: If you don't change the value, it loops forever!
}
Use code with caution.
2. The do-while Loop (Do once, then repeat...)
Think of this like a Free Sample. You get to taste it once for sure, and then you decide if you want
to keep eating based on the condition.
• Key Difference: It executes the code at least once, even if the condition is false.
•
java
int hunger = 0;
do {
[Link]("Eating a slice of pizza...");
} while (hunger > 0);
// Result: Prints once, even though hunger is 0.
Use code with caution.
3. The for Loop (The "Exact" Repeat)
This is the most common loop. Use this when you know exactly how many times you want to do
something. It has 3 parts: Start; Stop; Step.
java
// (Start at 1; Stop at 5; Add 1 each time)
for (int i = 1; i <= 5; i++) {
[Link]("Push-up number: " + i);
}
Use code with caution.
4. Loop Control: break and continue
Sometimes you need to change the behavior inside the loop:
• break: Stops the loop immediately (e.g., "I found what I was looking for, stop searching").
• continue: Skips the rest of the current turn and jumps to the next one (e.g., "This item is
broken, skip it and check the next one").
•
java
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips printing '3'
}
[Link]("Number: " + i);
}
Use code with caution.
5. The "Enhanced For-Each" Loop (Modern Java)
This is used specifically for Arrays or Collections (which we will learn next). It is the cleanest way
to look at every item in a list without using counters like i.
java
String[] colors = {"Red", "Green", "Blue"};
for (String color : colors) {
[Link]("Painting with: " + color);
}
Use code with caution.
💻 Practice Exercise: The "Countdown"
Try to write a loop that:
1. Starts at 10.
2. Counts down to 1.
3. When it reaches 5, it prints "Halfway there!"
4. When it finishes, it prints "Blast off!
Part 7: Arrays (The "Locker Room").
1. What is an Array?
In layman's terms, imagine you have 10 football players. Instead of giving each player their own
separate house (Variable), you give them one big Locker Room (Array). Each player has their own
numbered locker (Index).
Crucial Rules of Java Arrays:
1. Fixed Size: Once you build a Locker Room with 10 lockers, you cannot add an 11th locker
later.
2. Same Type: If the Locker Room is for "Strings," you cannot put an "Integer" in any of the
lockers.
3. Zero-Based: Java starts counting at 0, not 1. The first locker is always [0].
3.
2. Creating an Array
There are two ways to build your "Locker Room" in Core Java:
A. The "Fill-It-Later" Way
You know how many items you need, but you don't have the data yet.
java
// Create a locker room for 5 integers
int[] scores = new int[5];
// Assigning values to specific lockers
scores[0] = 95;
scores[1] = 88;
Use code with caution.
B. The "Instant" Way
You have the data ready right now.
java
String[] cars = {"Tesla", "BMW", "Toyota"};
Use code with caution.
3. Accessing and Modifying
You use the index number inside square brackets [] to talk to a specific item.
java
String[] fruits = {"Apple", "Banana", "Cherry"};
[Link](fruits[1]); // Output: Banana
fruits[1] = "Mango"; // Replaces Banana with Mango
[Link](fruits[1]); // Output: Mango
Use code with caution.
4. Multidimensional Arrays (The "Hotel")
If a 1D array is a Locker Room, a 2D Array is a Hotel. It has Rows and Columns.
java
// A table with 2 rows and 3 columns
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
[Link](grid[1][0]); // Output: 4 (Row 1, Column 0)
Use code with caution.
5. Processing Arrays with Loops
This is where the magic happens. We use the loops you just learned to process all lockers at once.
java
int[] ages = {18, 20, 25, 30};
// Using the length property to know when to stop
for (int i = 0; i < [Link]; i++) {
[Link]("Age at locker " + i + " is " + ages[i]);
}
Use code with caution.
6. Important Utility: Pretty Printing
Remember your very first question? In Core Java, to see the whole array at once without a loop, you
use the Arrays tool:
java
import [Link];
int[] numbers = {10, 20, 30};
[Link]([Link](numbers));
// Output: [10, 20, 30]
Use code with caution.
💻 Step 1 (Practice Task)
Try to mentally (or in code) solve this:
1. Create an array of 4 double values representing prices.
2. Use a for loop to calculate the sum of all prices.
3. Print the total
Next : JAVA-2026-PHASE-1-COMPLETE