BeginnerJava · Lesson 2 of 8

Variables, Types & Strings

Primitive types, String, var, casting, integer division and formatting output.

Java is statically typed: every variable has a type that never changes. Primitive types hold simple values — int and long (whole numbers), double (decimals), boolean and char. String holds text and is an object. final makes a variable unchangeable.

var lets the compiler infer the type of a local variable from its value — the type is still fixed, you just don't repeat it.

Watch out: dividing two ints gives an int (7 / 2 is 3). Cast one side to double for a decimal result. Convert text to numbers with Integer.parseInt, and format output with String.format or formatted. For money, use BigDecimal rather than double.

Basics.javaJava
import java.math.BigDecimal;

public class Basics {
    public static void main(String[] args) {
        final String school = "Azania Secondary";
        int students = 420;
        students += 15;
        long population = 65_000_000L;
        double average = (78 + 64 + 91) / 3.0;
        boolean isOpen = true;
        char stream = 'A';
        var subject = "Biology";          // inferred as String

        System.out.println(school + " has " + students + " students in stream " + stream);
        System.out.println("Open? " + isOpen + ", population " + population + ", subject " + subject);

        System.out.println(7 / 2);                 // 3   (integer division)
        System.out.println(7 / 2.0);               // 3.5
        System.out.println((double) 7 / 2);        // 3.5 (cast first)
        System.out.println(7 % 2);                 // 1   (remainder)

        int score = Integer.parseInt("88");
        System.out.println(score + 2);             // 90
        System.out.println(String.format("Average: %.1f", average));
        System.out.println("%s scored %d%%".formatted("Amina", score));

        System.out.println(0.1 + 0.2);             // 0.30000000000000004
        System.out.println(new BigDecimal("0.10").add(new BigDecimal("0.20")));   // 0.30

        String name = "  amina juma  ";
        String clean = name.strip().toUpperCase();
        System.out.println(clean + " (" + clean.length() + " characters)");
        System.out.println(clean.equals("AMINA JUMA"));   // compare Strings with equals, not ==
    }
}

Key points

  • Every variable has a fixed type; var just infers it.
  • int / int truncates — use a double or cast for decimals; use BigDecimal for money.
  • Compare Strings with .equals(), never ==.

Exercise

Write a program that stores three scores as Strings (as if typed by a user), converts them with Integer.parseInt, and prints the total and the average to two decimal places using String.format.

Show solution

Try the exercise yourself first — then compare your approach with this one.

Integer.parseInt turns each text score into an int. Dividing by 3.0 (a double) avoids integer division, and String.format("%.2f", ...) shows two decimal places.

Scores.javaJava
public class Scores {
    public static void main(String[] args) {
        String[] typed = {"78", "64", "91"};

        int total = 0;
        for (String text : typed) {
            total += Integer.parseInt(text);
        }
        double average = total / 3.0;

        System.out.println("Total: " + total);                              // Total: 233
        System.out.println(String.format("Average: %.2f", average));        // Average: 77.67
        System.out.println("Integer division would give: " + total / 3);    // 77
    }
}

Check your understanding

  1. What does 7 / 2 evaluate to in Java?

  2. How should you compare two Strings for equal text?

  3. What does var count = 10; mean?

  4. Which type should you use for exact money calculations?

Ask AI