Skip to content
PickNTech

Variables, Types and Control Flow

Store data in typed variables and make decisions with if, switch and loops.

Beginner18 minUpdated 20 Sept 2026

What you’ll learn

  • Primitive types vs reference types
  • Declaring variables and using var
  • if/else, switch expressions and loops
  • Common beginner mistakes with == and integer division

Variables are typed

Java is statically typed: every variable has a type that is known at compile time, and the compiler stops you from putting the wrong kind of value into it.

int age = 28;
double price = 19.99;
boolean active = true;
char grade = 'A';
String name = "Kousik";

Primitive types

Java has eight primitive types. You will use four of them 95% of the time:

TypeHoldsExample
intWhole numbers (±2.1 billion)int count = 42;
longBigger whole numberslong views = 9_000_000_000L;
doubleDecimal numbersdouble rate = 3.75;
booleantrue / falseboolean done = false;

The others are byte, short, float and char.

Reference types

Everything else — String, arrays, and any class you write — is a reference type. The variable holds a reference (a pointer) to an object on the heap, and it can be null.

Common mistake

Never compare strings with ==. It checks whether two references point to the same object, not whether the text is equal. Use a.equals(b).

Type inference with var

Since Java 10, local variables can use var when the type is obvious from the right-hand side:

var total = 0;               // int
var names = new ArrayList<String>();  // ArrayList<String>

The variable is still strongly typed — var just saves you typing it twice.

Making decisions

int score = 72;
 
if (score >= 90) {
    System.out.println("Excellent");
} else if (score >= 60) {
    System.out.println("Pass");
} else {
    System.out.println("Try again");
}

Switch expressions

Modern Java has a much cleaner switch that returns a value and needs no break:

String day = "SAT";
String type = switch (day) {
    case "SAT", "SUN" -> "Weekend";
    case "MON", "TUE", "WED", "THU", "FRI" -> "Weekday";
    default -> "Unknown";
};

Loops

// Classic for loop — when you need the index
for (int i = 0; i < 3; i++) {
    System.out.println("Attempt " + i);
}
 
// Enhanced for — when you just need each element
int[] prices = {10, 20, 30};
for (int p : prices) {
    System.out.println(p);
}
 
// while — when you don't know how many iterations upfront
int balance = 100;
while (balance > 0) {
    balance -= 30;
}

Integer division

7 / 2 is 3, not 3.5, because both operands are int. Write 7 / 2.0 or cast: (double) 7 / 2.

⚡ Try it yourself

Write a program that prints the numbers 1 to 20, but prints Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both.

Show a solution
for (int i = 1; i <= 20; i++) {
    if (i % 15 == 0) System.out.println("FizzBuzz");
    else if (i % 3 == 0) System.out.println("Fizz");
    else if (i % 5 == 0) System.out.println("Buzz");
    else System.out.println(i);
}

Interview questions

QWhat is the difference between == and equals()?+

== compares primitive values, or for objects, whether two references point to the same object. equals() compares logical equality as defined by the class — for String it compares the characters.

QIs Java pass-by-value or pass-by-reference?+

Always pass-by-value. For objects, the value that gets copied is the reference, so a method can mutate the object it receives but cannot make the caller's variable point to a different object.

Key takeaways

  • Primitives hold values; reference types hold pointers to objects.
  • Compare strings with equals(), never ==.
  • Prefer switch expressions (->) in modern Java.

Finished this lesson?

Mark it complete to move forward on your roadmap.