Java Beginner
Write once, run anywhere — a robust, object-oriented, and class-based ecosystem.
Table of Contents
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.
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!"); } }
Main.java for public class Main).
Variables & Data Types
Java relies on strong, static type verification. Variables are separated into low-level primitives and reference objects.
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";
Operators
Java supports standard operations for evaluating mathematics, chaining conditionals, and manipulating variables inline.
// 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
Strings & User Input
Strings are non-primitive objects equipped with built-in utility methods. Capturing human terminal input relies on the Scanner utility.
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
.equals() (e.g., str1.equals(str2)). Using == evaluates memory addresses instead of text literal equity!
Conditionals
Direct the code path using clean branching structures like if-else structures or structural switch evaluations.
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"; };
Loops
Process repeated code segments using deterministic index-based counters or dynamically evaluated logical boundaries.
// 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--; }
Arrays & ArrayList
Native arrays preserve fixed bounds. Dynamic collections that grow dynamically leverage the ArrayList entity class from java.util.
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); }
Methods & Classes Basics
Methods control behavioral parameters inside objects. Structures orchestrate initialization arguments via precise constructor functions.
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 } }