MarzleyTech Learn

Home / Learn / Java / Java introduction: what Java is, where it's used, the JDK and JVM, your first program, syntax rules and input/output

Java introduction: what Java is, where it's used, the JDK and JVM, your first program, syntax rules and input/output

Java is one of the world's most widely used programming languages. It powers large banking and insurance systems, government and telecom back ends, enterprise web applications, Android apps (alongside Kotlin), big data tools, and countless university courses, including many computer science programmes in Kenya. Java's motto, "write once, run anywhere", comes from the Java Virtual Machine (JVM), which lets the same compiled program run on Windows, Linux and macOS. Learning Java teaches strong programming fundamentals (types, classes, structure) that transfer to Kotlin, C#, and other languages.

Why Java?

StrengthWhat it means
PortableCompiled to bytecode that runs on any device with a JVM
Strongly typedCatches many errors at compile time
Object-orientedOrganises big programs into classes and objects
Huge ecosystemLibraries and frameworks for almost everything (Spring, Hibernate, Apache tools)
Mature and stableUsed for decades in critical systems; long-term support releases
JobsConstant demand in banks, telcos, enterprise software and Android

Where Java is used

AreaExamples
Enterprise back endsBanking cores, insurance, payroll, ERP systems (Spring Boot APIs)
AndroidMany existing apps are written in Java; Kotlin is now Google's preferred language, and they interoperate
Web applicationsSpring Boot, Jakarta EE
Big dataHadoop, Kafka, Spark (JVM ecosystem)
Desktop and toolsIntelliJ IDEA, Eclipse, Minecraft (Java Edition)
EducationUniversity programming courses, AP/IGCSE-style curricula

How Java runs: JDK, JRE, JVM

Main.java  --javac (compiler)-->  Main.class (bytecode)  --JVM-->  runs on Windows/Linux/macOS
TermMeaning
JDK (Java Development Kit)Everything to write and compile Java: compiler (javac), tools, runtime
JRE (Java Runtime Environment)What's needed to run Java programs
JVM (Java Virtual Machine)Executes bytecode, manages memory (garbage collection)

Installing and tools

  1. Install a JDK (an LTS version such as 21) from Adoptium (Eclipse Temurin), Oracle or Microsoft.
  2. Check: java -version and javac -version.
  3. Choose an editor:
  4. IntelliJ IDEA Community (free, excellent), Eclipse, NetBeans, or VS Code with the Java extension pack.
  5. Android Studio for Android development.
  6. Compile and run from the terminal:
Terminal
javac Main.java     # creates Main.class
java Main           # runs it
java Main.java      # newer JDKs can compile and run a single file in one step

In this hub, the Run button compiles and runs Java for you online.

Your first program

Java · runs live in the interactive lesson
class Main {
    public static void main(String[] args) {
        System.out.println("Habari, Kenya!");
        System.out.println("Learning Java on the Marzley learning hub.");
    }
}

Line by line:

  • class Main { ... }: every Java program lives inside a class. In a file, the public class name must match the file name (Main.java).
  • public static void main(String[] args): the main method, where the program starts.
  • public: can be called from outside; static: belongs to the class, no object needed; void: returns nothing; String[] args: command-line arguments.
  • System.out.println(...): prints text followed by a new line.
  • Statements end with ;; code blocks are wrapped in { }.

Syntax rules

  • Java is case-sensitive: Main ≠ main, String ≠ string.
  • Every statement ends with a semicolon.
  • Braces {} group code; indentation is for humans (but always indent neatly).
  • Text (Strings) uses double quotes; single characters use single quotes: 'A'.

Variables and data types

Java variables must be declared with a type:

Java · runs live in the interactive lesson
class Main {
    public static void main(String[] args) {
        int students = 45;               // whole numbers
        long population = 53_000_000L;   // big whole numbers (underscores for readability)
        double fee = 12500.50;           // decimals
        float discount = 0.1f;           // smaller decimals (f suffix)
        char grade = 'A';                // one character
        boolean paid = true;             // true/false
        String school = "Kenya High";    // text (a class, not a primitive)

        System.out.println(school + ": " + students + " students, fee KSh " + fee);
        System.out.println("Grade " + grade + ", paid: " + paid + ", population " + population);

        final double VAT_RATE = 0.16;    // final = constant
        var town = "Kisumu";             // var infers the type (String)
        System.out.println(town + " VAT rate " + VAT_RATE + ", discount " + discount);
    }
}
Primitive typeSizeExample
byte8-bit-128 to 127
short16-bit±32,767
int32-bitabout ±2.1 billion
long64-bitvery large numbers
float32-bit decimal3.14f
double64-bit decimal3.14159 (default for decimals)
char16-bit character'K'
booleantrue/falsetrue

String isn't primitive; it's a class with useful methods (next lessons).

Operators and casting

Java · runs live in the interactive lesson
class Main {
    public static void main(String[] args) {
        int a = 17, b = 5;
        System.out.println(a + b);          // 22
        System.out.println(a / b);          // 3: int / int drops the decimal!
        System.out.println(a % b);          // 2: remainder
        System.out.println((double) a / b); // 3.4: cast to double first

        int count = 10;
        count++;                            // 11
        count += 5;                         // 16
        System.out.println(count);

        double price = 1999.99;
        int whole = (int) price;            // narrowing cast: 1999 (decimal dropped)
        System.out.println(whole);

        int bigSum = Integer.MAX_VALUE;
        System.out.println(bigSum + 1);     // overflow wraps to a negative number!
        System.out.println(Math.round(12.567 * 100) / 100.0);   // 12.57
    }
}

Watch for integer division and overflow (use long for large values such as totals in cents).

Output formatting

Java · runs live in the interactive lesson
class Main {
    public static void main(String[] args) {
        String name = "Amina";
        double balance = 15250.5;
        int items = 3;
        System.out.printf("Hello %s, you have %d items.%n", name, items);
        System.out.printf("Balance: KSh %,.2f%n", balance);
        System.out.println(String.format("%-10s|%6d|", "Chapati", 30));
    }
}
FormatMeaning
%sString
%dInteger
%.2fDecimal with 2 places
%,.2fWith thousands separators
%nNew line

Input with Scanner

Reading keyboard input (run this on your own computer, as the online runner has no keyboard input):

Java
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.println("Hello " + name + ", next year you'll be " + (age + 1));
        sc.close();
    }
}

nextInt() leaves the Enter key in the buffer; if you read a line after it, call sc.nextLine() once first.

Comments and naming conventions

Java
// single-line comment
/* multi-line
   comment */
/** Javadoc comment: documents classes and methods */
ItemConventionExample
ClassesPascalCaseStudentRecord
Methods and variablescamelCasecalculateFee, totalAmount
ConstantsUPPER_SNAKE_CASEVAT_RATE
Packageslowercaseke.co.marzley.fees

Common errors

ErrorCause
error: ';' expectedMissing semicolon
cannot find symbolTypo in a variable/method name, or missing import
incompatible types: possible lossy conversion from double to intAssigning a decimal to an int without casting
class X is public, should be declared in a file named X.javaFile name doesn't match the public class
Exception in thread "main" java.lang.ArithmeticException: / by zeroDividing an integer by zero at runtime
Think about it: A student writes int average = total / count; where total = 245 and count = 4, expecting 61.25, but gets 61. Why, and how do they fix it?Show answer

Both are ints, so Java performs integer division and the result is stored in an int. Use a double and cast: double average = (double) total / count; which gives 61.25.

Summary

  • Java is a portable, strongly typed, object-oriented language used in enterprise systems, Android, web back ends, big data and education.
  • javac compiles .java to bytecode; the JVM runs it anywhere; install a JDK (LTS) and use IntelliJ, VS Code or Android Studio.
  • Programs start in public static void main(String[] args) inside a class; statements end with ;; Java is case-sensitive.
  • Declare variables with types (int, long, double, char, boolean, String); watch integer division, casting and overflow.
  • Print with println/printf, read input with Scanner, follow naming conventions and learn common error messages.

Check yourself

  1. What does JVM stand for?

    Show answer

    Java Virtual Machine

  2. Which command compiles a Java file?

    Show answer

    javac

  3. What is the name of the method where a Java program starts?

    Show answer

    main

  4. What does 17 / 5 give in Java when both are ints?

    Show answer

    3

  5. Which keyword makes a variable constant?

    Show answer

    final

  6. Which class reads keyboard input?

    Show answer

    Scanner

Lesson 1 of 9 in Java · Printable course notes