Java
Write once, run anywhere.
Java is a strongly-typed, compiled language built around objects. It powers Android apps, enterprise back-end systems, and Minecraft's plugin ecosystem. More verbose than Python, but extremely structured.
Core concepts
Variables and types
Java is statically typed — you declare the type of every variable. Primitive types (int, double, boolean, char) hold raw values. Reference types (String, arrays, objects) hold a reference to an object on the heap.
// Primitive types
int age = 17;
double height = 1.75;
boolean isActive = true;
char grade = 'A';
// Reference types
String name = "Sibah";
int[] scores = {85, 92, 78};
// Constants (convention: ALL_CAPS)
final double PI = 3.14159;
// Type casting
double d = 9.7;
int i = (int) d; // 9 (truncated, not rounded)
// String operations
String full = "Hello, " + name + "!";
int len = name.length();
String upper = name.toUpperCase();
boolean starts = name.startsWith("Si");Methods
In Java, functions are called methods and must belong to a class. Every method declares its return type (or void). static methods belong to the class itself; instance methods belong to objects created from the class.
public class MathUtils {
// Static method
public static int add(int a, int b) {
return a + b;
}
// Overloaded method (same name, different params)
public static double add(double a, double b) {
return a + b;
}
// Void method (returns nothing)
public static void printResult(int result) {
System.out.println("Result: " + result);
}
public static void main(String[] args) {
int sum = add(3, 4);
printResult(sum); // Result: 7
double dSum = add(1.5, 2.5);
System.out.println(dSum); // 4.0
}
}Classes and objects
A class is a blueprint. An object is an instance of that blueprint. Constructors initialize new objects. Fields hold an object's state. Methods define what the object can do.
public class Player {
// Fields
private String name;
private int level;
private boolean alive;
// Constructor
public Player(String name) {
this.name = name;
this.level = 1;
this.alive = true;
}
// Methods
public String getName() { return name; }
public int getLevel() { return level; }
public void levelUp() {
level++;
System.out.println(name + " is now level " + level);
}
@Override
public String toString() {
return "Player{name=" + name + ", level=" + level + "}";
}
public static void main(String[] args) {
Player p = new Player("Sibah");
p.levelUp();
System.out.println(p); // Player{name=Sibah, level=2}
}
}Conditions and loops
Java's control flow syntax is C-style. if/else if/else for conditions. for, while, do-while for loops. Enhanced for-each loops work with arrays and collections. switch handles multiple cases.
int score = 85;
// if / else if / else
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("C or below");
}
// Traditional for loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// For-each (enhanced for)
int[] arr = {1, 2, 3, 4};
for (int n : arr) {
System.out.println(n);
}
// While
int count = 0;
while (count < 3) {
count++;
}
// Switch
String day = "MON";
switch (day) {
case "MON": System.out.println("Monday"); break;
case "FRI": System.out.println("Friday"); break;
default: System.out.println("Other");
}Collections
Java's Collections framework provides ArrayList (dynamic array), HashMap (key-value), HashSet (unique values), and more. Always prefer them over raw arrays for real programs.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CollectionsExample {
public static void main(String[] args) {
// ArrayList
List<String> skills = new ArrayList<>();
skills.add("JavaScript");
skills.add("Java");
skills.add("Lua");
skills.remove("Lua");
System.out.println(skills.size()); // 2
for (String skill : skills) {
System.out.println(skill);
}
// HashMap
Map<String, Integer> scores = new HashMap<>();
scores.put("Sibah", 95);
scores.put("Bob", 80);
int sibahScore = scores.get("Sibah"); // 95
boolean has = scores.containsKey("Bob"); // true
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}Inheritance and interfaces
extends lets one class inherit from another, getting all its fields and methods. implements lets a class fulfill an interface contract — a list of methods it must provide. Java only allows single inheritance but multiple interface implementation.
// Base class
public abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public abstract String speak();
public void introduce() {
System.out.println("I am " + name + " and I say: " + speak());
}
}
// Subclasses
public class Dog extends Animal {
public Dog(String name) { super(name); }
@Override
public String speak() { return "Woof!"; }
}
public class Cat extends Animal {
public Cat(String name) { super(name); }
@Override
public String speak() { return "Meow!"; }
}
// Interface
public interface Swimmable {
void swim();
}
public class Duck extends Animal implements Swimmable {
public Duck(String name) { super(name); }
@Override
public String speak() { return "Quack!"; }
@Override
public void swim() { System.out.println(name + " is swimming"); }
}