Java Programming: Complete Beginner to OOP
Chapter 2 / 8· 20 min read· 0 cards

Variables, Data Types and Operators

Java's strongly-typed variables for numbers, characters, booleans, and text.

Strongly typed variables

Java is strongly and statically typed — you must declare the type of every variable, and the compiler strictly enforces it. This means more typing up front, but it catches a whole class of bugs before your program ever runs, which is part of why Java is trusted for large systems:

int age = 19;            // whole number
double price = 49.99;    // decimal number
char grade = 'A';        // single character (single quotes)
boolean passed = true;   // true or false
String name = "Mehul";   // text (capital S — String is a class)

Notice String has a capital S — it's a class, not a basic type, which is why it's capitalised while int, double, char, and boolean are lowercase "primitive" types. This distinction matters in Java and we'll return to it.


The primitive types

Java's basic building blocks are the primitive types — simple values stored directly. The ones you'll use most:

  • int — whole numbers, 4 bytes (about ±2 billion)
  • long — bigger whole numbers, 8 bytes (for very large values)
  • double — decimal numbers, 8 bytes (the everyday choice for decimals)
  • char — a single character, in single quotes
  • boolean — true or false

Because each has a fixed size, an int can overflow if you exceed its range — a good reason to use long for things like large counts or IDs. Java is precise about types, and that precision is a feature.


Arithmetic and the integer-division trap

Arithmetic works as expected, but Java shares C's integer-division behaviour — the classic beginner surprise:

int a = 17, b = 5;
System.out.println(a + b);   // 22
System.out.println(a * b);   // 85
System.out.println(a / b);   // 3   -- integer division! decimal dropped
System.out.println(a % b);   // 2   -- remainder (modulo)

System.out.println((double) a / b);  // 3.4 -- cast for real division

When both numbers are integers, Java discards the decimal: 17 / 5 is 3. To get 3.4 you must make at least one side a decimal — the cast (double) a does that. This single rule causes a lot of "why is my answer wrong?" confusion; remember it and you'll avoid the trap.


Comparison, logical, and shortcut operators

Comparisons produce a boolean, and you combine conditions with && (and), || (or), ! (not):

int age = 25;
boolean hasLicense = true;

System.out.println(age >= 18);    // true
System.out.println(age == 25);    // true  -- double equals compares numbers

if (age >= 18 && hasLicense) {
    System.out.println("Can drive");
}

Mind the = versus == trap — single assigns, double compares. Java also has the shortcuts +=, -=, ++ (add one), and -- (subtract one), which you'll use heavily in loops.


A crucial gotcha: comparing Strings

Here's a Java-specific trap that catches every beginner, and it follows from String being a class, not a primitive. To compare the contents of two strings, you must use .equals(), not ==:

String a = "hello";
String b = "hello";

// ❌ WRONG — == compares references (memory locations), not content
if (a == b) { ... }            // unreliable, don't do this for Strings

// ✅ CORRECT — .equals() compares the actual text
if (a.equals(b)) {
    System.out.println("They match!");
}

For primitives like int, == correctly compares values. But for objects like String, == checks whether they're the same object in memory, which isn't what you usually want. Always use .equals() to compare String content. This is one of the most common Java beginner bugs — burn it into memory now and save yourself hours later.


String methods

The String class comes loaded with useful methods, since text handling is so common:

String text = "Hello World";
System.out.println(text.length());        // 11
System.out.println(text.toUpperCase());   // HELLO WORLD
System.out.println(text.charAt(0));       // H
System.out.println(text.substring(0, 5)); // Hello
System.out.println(text.contains("World"));// true
System.out.println(text.replace("World", "Java")); // Hello Java

Like in many languages, these methods return new strings rather than changing the original (strings are immutable in Java). With variables and types understood — and that crucial String-comparison rule learned — we're ready for control flow.

Reading mode · scroll to read at your own pace

Finished "Variables, Data Types and Operators"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.