DEV Community

Md Jamilur Rahman
Md Jamilur Rahman

Posted on

Modern Java Part 3: Language Basics — Variables, Types & Control Flow

Every Java program is built from the same bricks: variables to store data, types to describe data, and control flow to make decisions. Let's lay the foundation.

Variables — Storing Information

A variable is a labeled box where you keep a value.

String name = "Jamil";    // A box labeled "name" containing "Jamil"
int age = 30;             // A box labeled "age" containing 30
double salary = 85000.50; // A box labeled "salary" containing 85000.50
boolean isEmployed = true;// A box labeled "isEmployed" containing true
Enter fullscreen mode Exit fullscreen mode

The var keyword (Java 10+):

Instead of writing the type, let Java figure it out:

var name = "Jamil";       // Java knows this is a String
var age = 30;             // Java knows this is an int
var salary = 85000.50;    // Java knows this is a double
Enter fullscreen mode Exit fullscreen mode

Analogy: var is like labeling a box "stuff" — you know what's inside, and so does Java. Use it when the type is obvious from the right side.

Data Types — The 8 Primitives + Objects

Java has 8 primitive types (the building blocks):

Type What It Stores Example Size
byte Whole numbers (-128 to 127) byte b = 100; 1 byte
short Whole numbers (-32k to 32k) short s = 30000; 2 bytes
int Whole numbers (most common) int x = 42; 4 bytes
long Huge whole numbers long big = 9999999999L; 8 bytes
float Decimal numbers (less precise) float f = 3.14f; 4 bytes
double Decimal numbers (more precise) double d = 3.14159; 8 bytes
char Single character char c = 'A'; 2 bytes
boolean true or false boolean flag = true; 1 bit

Rule of thumb: Use int for whole numbers, double for decimals, String for text, boolean for yes/no.

Everything else is an object:

String name = "Hello";     // String is an object, not primitive
int[] numbers = {1, 2, 3}; // Arrays are objects
Enter fullscreen mode Exit fullscreen mode

Operators — Doing Things with Data

Arithmetic:

int sum = 10 + 3;       // 13
int diff = 10 - 3;      // 7
int product = 10 * 3;   // 30
int quotient = 10 / 3;  // 3 (integer division — drops decimal)
double precise = 10.0 / 3; // 3.3333... (use double for decimals)
int remainder = 10 % 3; // 1 (modulo — remainder after division)
Enter fullscreen mode Exit fullscreen mode

Comparison:

int a = 10, b = 20;
a == b    // false (equal to)
a != b    // true  (not equal to)
a > b     // false (greater than)
a < b     // true  (less than)
a >= 10   // true  (greater or equal)
a <= 5    // false (less or equal)
Enter fullscreen mode Exit fullscreen mode

Logical:

boolean x = true, y = false;
x && y    // false (AND — both must be true)
x || y    // true  (OR — at least one true)
!x        // false (NOT — flips the value)
Enter fullscreen mode Exit fullscreen mode

Real-world analogy: && is "I need milk AND eggs" (both required). || is "I'll drink coffee OR tea" (either works). ! is "I'm NOT hungry."

Control Flow — Making Decisions

if/else — The Traffic Light:

int speed = 75;

if (speed > 80) {
    System.out.println("Slow down!");
} else if (speed > 60) {
    System.out.println("Going a bit fast");
} else {
    System.out.println("Good speed");
}
// Output: Going a bit fast
Enter fullscreen mode Exit fullscreen mode

switch — The Menu:

String day = "Monday";

String type = switch (day) {
    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Weekday";
    case "Saturday", "Sunday" -> "Weekend";
    default -> "Unknown";
};
System.out.println(type); // Weekday
Enter fullscreen mode Exit fullscreen mode

Notice the -> syntax — this is modern switch (Java 14+). No more break; statements. No more fall-through bugs.

for loop — Counting:

for (int i = 0; i < 5; i++) {
    System.out.println("Count: " + i);
}
// Count: 0, 1, 2, 3, 4
Enter fullscreen mode Exit fullscreen mode

for-each — Iterating Collections:

String[] fruits = {"Apple", "Banana", "Cherry"};

for (String fruit : fruits) {
    System.out.println(fruit);
}
Enter fullscreen mode Exit fullscreen mode

while — Repeat Until Condition:

int count = 0;
while (count < 3) {
    System.out.println("Hello");
    count++;
}
// Prints "Hello" 3 times
Enter fullscreen mode Exit fullscreen mode

Method Calls — Reusable Actions

// Define a method
String greet(String name) {
    return "Hello, " + name + "!";
}

// Use it
void main() {
    System.out.println(greet("Jamil"));  // Hello, Jamil!
    System.out.println(greet("World"));  // Hello, World!
}
Enter fullscreen mode Exit fullscreen mode

Methods are like recipes — write once, use many times.

String Operations — Working with Text

String name = "Jamil";

name.length()           // 5 (character count)
name.toUpperCase()      // "JAMIL"
name.toLowerCase()      // "jamil"
name.charAt(0)          // 'J' (first character)
name.contains("mil")    // true
name.startsWith("Ja")   // true
name.replace("J", "K")  // "Kamil"
name.isEmpty()          // false
Enter fullscreen mode Exit fullscreen mode

String concatenation:

String first = "Jamil";
String last = "Rahman";
String full = first + " " + last;  // "Jamil Rahman"

// Modern way (Java 15+):
String formatted = STR."My name is \{first} \{last}";
Enter fullscreen mode Exit fullscreen mode

Important: Strings are immutable — every operation creates a new String:

String s = "hello";
s.toUpperCase();  // Returns "HELLO" but s is still "hello"
s = s.toUpperCase(); // NOW s is "HELLO"
Enter fullscreen mode Exit fullscreen mode

Arrays — Lists of Same-Type Items

// Create
int[] numbers = {10, 20, 30, 40, 50};
String[] names = new String[3]; // Empty array of 3 slots

// Access (index starts at 0)
numbers[0]    // 10
numbers[2]    // 30
numbers.length // 5

// Modify
names[0] = "Jamil";
names[1] = "Rahman";
Enter fullscreen mode Exit fullscreen mode

Real-world analogy: An array is like a parking lot with numbered spots. Each spot holds one car (same type). Spot #0 is the first one.

Key Takeaways

  1. var lets Java infer types — use when obvious
  2. 8 primitives: byte, short, int, long, float, double, char, boolean
  3. Modern switch uses -> — no more break;
  4. Strings are immutable — operations return new strings
  5. Arrays start at index 0, have fixed size

What's Next?

Part 4 covers Classes & Objects — how Java organizes code into reusable blueprints. This is where object-oriented programming begins.


This series follows dev.java/learn — the official Java learning path. Each article covers one topic, explained simply.

Top comments (0)