Labs ICT
Pro Login

Examples

So you have learned the basics of Java and now you want to see what it can actually do. I get it. Theory is fine but nothing beats looking at real code that does real things.

Let me walk you through three practical examples. These are the kind of programs you might actually write as a beginner — a calculator, a number checker, and a grade system. Nothing fancy, but they will show you how Java works in the real world.

A Simple Calculator

Here is the thing about calculators — everybody builds one when they are learning a new language. It is the perfect first project because it uses variables, arithmetic, and output. Trust me, once you write this, things start clicking.

public class Main {
  public static void main(String[] args) {
    int num1 = 15;
    int num2 = 4;

    System.out.println("Addition: " + (num1 + num2));
    System.out.println("Subtraction: " + (num1 - num2));
    System.out.println("Multiplication: " + (num1 * num2));
    System.out.println("Division: " + (num1 / num2));
    System.out.println("Remainder: " + (num1 % num2));
  }
}

See how straightforward that is? You declare two numbers, then use +, -, *, /, and % to perform operations. The % operator gives you the remainder of a division — super useful for checking if a number is even or odd.

Try it Yourself →

The Number Checker

So you have variables and operators down. Now let us throw in some logic. This program takes a number and tells you if it is even or odd, prime or not, and positive or negative. It uses conditionals and loops — two things you will use in every single Java program you ever write.

public class Main {
  public static void main(String[] args) {
    int number = 17;

    if (number % 2 == 0) {
      System.out.println(number + " is even");
    } else {
      System.out.println(number + " is odd");
    }

    boolean isPrime = true;
    for (int i = 2; i <= number / 2; i++) {
      if (number % i == 0) {
        isPrime = false;
        break;
      }
    }

    if (isPrime && number > 1) {
      System.out.println(number + " is prime");
    } else {
      System.out.println(number + " is not prime");
    }

    if (number > 0) {
      System.out.println(number + " is positive");
    } else if (number < 0) {
      System.out.println(number + " is negative");
    } else {
      System.out.println(number + " is zero");
    }
  }
}

A few things to notice here. The if-else blocks let your program make decisions. The for loop repeats code a set number of times — in this case checking divisors. And the boolean variable isPrime acts as a flag to track the result.

Try it Yourself →

A Grade System

By now you are probably getting comfortable with conditionals. Let us take it one step further with an else-if ladder. This is one of those patterns you will see everywhere — it checks multiple conditions in order and picks the first one that matches.

public class Main {
  public static void main(String[] args) {
    int score = 85;
    char grade;

    if (score >= 70) {
      grade = 'A';
    } else if (score >= 60) {
      grade = 'B';
    } else if (score >= 50) {
      grade = 'C';
    } else if (score >= 40) {
      grade = 'D';
    } else {
      grade = 'F';
    }

    System.out.println("Score: " + score);
    System.out.println("Grade: " + grade);
  }
}

The program checks the score from top to bottom. If the score is 85, it hits the first condition (score >= 70) and assigns grade = 'A'. The rest of the conditions are skipped entirely. That is the key thing about else if — only one block ever runs.

Change the score variable yourself and see how the grade changes. Try 55 or 42 and watch what happens.

Try it Yourself →

What is Next?

These examples cover variables, arithmetic, conditionals, loops, and booleans. That is already a solid foundation. The best thing you can do now is mess with the code — change numbers, add features, break things on purpose. That is how you actually learn.

Head over to the exercises when you are ready to test yourself.