Java logo

Java Beginner

Write once, run anywhere — a robust, object-oriented, and class-based ecosystem.

1 Structure & Output

Java is a strictly object-oriented language. Every sequence of executable logic must reside inside a class, and execution matches the signature of the main method.

java · Main.java
public class Main {
    // This is a single-line comment

    /* This is a 
       multi-line comment */

    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
The file name must exact-match the name of the public class inside it (e.g., Main.java for public class Main).

2 Variables & Data Types

Java relies on strong, static type verification. Variables are separated into low-level primitives and reference objects.

int 32-bit Integer 10, -500, 0
double 64-bit Float 3.14, -0.05
boolean Logical state true, false
char 16-bit Unicode 'A', '7', '$'
String Object literal "Text block"
var Local inference Java 10+ only
java · Variables.java
int age = 25;
double price = 19.99;
boolean isPremium = true;
char letter = 'V';

// Immutable variables use 'final'
final double TAX_RATE = 0.22;

// Local Variable Type Inference (Java 10+)
var message = "Type inferred as String";

3 Operators

Java supports standard operations for evaluating mathematics, chaining conditionals, and manipulating variables inline.

java · Operators.java
// Arithmetic Operators
int result = 10 / 3;       // 3 (Integer division discards remainder)
double precise = 10.0 / 3; // 3.3333333333333335

// Comparison & Logic
boolean canAccess = (age >= 18) && isPremium;

// Shorthand increments
int score = 10;
score++;                   // 11
score += 5;                // 16

4 Strings & User Input

Strings are non-primitive objects equipped with built-in utility methods. Capturing human terminal input relies on the Scanner utility.

java · InputOutput.java
import java.util.Scanner;

String txt = "Developer";
System.out.println(txt.length());      // 9
System.out.println(txt.toUpperCase()); // DEVELOPER

// Initializing terminal Scanner stream
Scanner input = new Scanner(System.in);
System.out.print("Enter nickname: ");
String nickname = input.nextLine();     // reads user text block

input.close(); // close stream resource
Always compare String objects using .equals() (e.g., str1.equals(str2)). Using == evaluates memory addresses instead of text literal equity!

5 Conditionals

Direct the code path using clean branching structures like if-else structures or structural switch evaluations.

java · Branching.java
int speed = 75;

if (speed > 90) {
    System.out.println("Fast");
} else if (speed > 60) {
    System.out.println("Moderate"); // ← executes
} else {
    System.out.println("Slow");
}

// Modern Switch Expression (Java 14+)
String direction = "R";
String action = switch (direction) {
    case "L" -> "Turn Left";
    case "R" -> "Turn Right"; // ← evaluates
    default  -> "Stay Straight";
};

6 Loops

Process repeated code segments using deterministic index-based counters or dynamically evaluated logical boundaries.

java · Loops.java
// Incremental for loop
for (int i = 1; i <= 3; i++) {
    System.out.print(i + " "); // 1 2 3
}

// Conditional while loop
int energy = 3;
while (energy > 0) {
    System.out.println("Running...");
    energy--;
}

7 Arrays & ArrayList

Native arrays preserve fixed bounds. Dynamic collections that grow dynamically leverage the ArrayList entity class from java.util.

java · Collections.java
import java.util.ArrayList;

// Fixed Native Array
int[] primeNumbers = {2, 3, 5, 7};

// Dynamic ArrayList (Requires Object wrappers like Integer, not int)
ArrayList<Integer> scores = new ArrayList<>();
scores.add(90);
scores.add(85);
scores.remove(0); // removes element at position 0

// Enhanced For-Each Loop
for (int s : scores) {
    System.out.println(s);
}

8 Methods & Classes Basics

Methods control behavioral parameters inside objects. Structures orchestrate initialization arguments via precise constructor functions.

java · UserTemplate.java
class Player {
    String username;
    int rank;

    // Constructor function
    Player(String username, int rank) {
        this.username = username;
        this.rank = rank;
    }

    // Object Instance Method
    void displaySpecs() {
        System.out.println(username + " level: " + rank);
    }
}

public class MainApp {
    public static void main(String[] args) {
        Player p1 = new Player("Vidux", 99);
        p1.displaySpecs(); // Vidux level: 99
    }
}
Now that you have mastered procedural components, deep dive into robust corporate architectures studying Inheritance, Interfaces, and Polymorphism.