☼
⌃
☰

java

Table of Contents

Statically-typed, object-oriented language running on the JVM (Java Virtual Machine). Write-once-run-anywhere via bytecode compilation. Dominant in enterprise backends, Android development, and — notably for your security work — a common target language: several technique notes assume Java-based applications (SSTI's Freemarker/ Thymeleaf payloads, general enterprise app pentesting).

Related: Type System Object Oriented Programming Server-Side Template Injection Metasploit

Core Syntax

JAVA
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, world");
    }
}

Every file's public class name must match the filename (Main.java for class Main). Compilation and execution are separate steps:

SHELL
javac Main.java   # compiles to Main.class (bytecode)
java Main          # runs it on the JVM

Type System

Statically typed — every variable's type is fixed at declaration and checked at compile time.

JAVA
int x = 5;                    // primitive
String name = "Greg";         // reference type (object)
double price = 19.99;
boolean isActive = true;

// Primitives have wrapper/object equivalents
Integer boxedX = 5;           // autoboxing
int unboxedX = boxedX;        // auto-unboxing
Primitive Wrapper
int Integer
double Double
boolean Boolean
char Character
long Long

Primitives are faster and use less memory; wrappers are needed when you need an object (e.g. in collections, since generics don't support primitives directly).

Classes and Objects

JAVA
public class User {
    private String name;
    private int age;

    // Constructor
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter/setter — Java convention, no native properties
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

// Usage
User u = new User("Greg", 30);
System.out.println(u.getName());

Inheritance and Interfaces

JAVA
interface Greetable {
    String greet();  // interface methods are implicitly abstract
}

class Person implements Greetable {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String greet() {
        return "Hello, " + name;
    }
}

// Single inheritance via extends, multiple interfaces via implements
class Employee extends Person {
    public Employee(String name) {
        super(name);  // call parent constructor
    }
}

Java allows single class inheritance but multiple interface implementation — the mechanism for what other languages solve with multiple inheritance or traits.

Collections

JAVA
import java.util.*;

List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
int aliceAge = ages.get("Alice");

Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
uniqueNames.add("Alice");  // no-op, already present

Generics (<String>, <String, Integer>) enforce type safety at compile time — a List<String> can't accept an Integer.

Exception Handling

JAVA
try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: " + e.getMessage());
} finally {
    System.out.println("Always runs");
}

// Checked exceptions must be declared or caught
public void readFile() throws IOException {
    // ...
}

Checked exceptions (must be declared with throws or caught) vs unchecked/runtime exceptions (don't require declaration) is a Java- specific distinction most other languages don't enforce.

Build Tools

Two dominant build systems — you'll encounter both in the wild:

Tool Config file Convention
Maven pom.xml XML, convention-over-configuration
Gradle build.gradle Groovy/Kotlin DSL, more flexible
SHELL
mvn compile          # Maven build
mvn test              # Maven run tests
gradle build           # Gradle build

Security-Relevant Notes

Given your pentest context specifically:

  • Deserialization: Java's native object deserialization (ObjectInputStream.readObject()) is the classic gadget-chain RCE vector — see Insecure Deserialization for the ysoserial workflow
  • Template engines: Freemarker and Thymeleaf (both Java-based) are common SSTI targets — the engine detection payload ${7*7} confirms Freemarker; *{7*7} confirms Thymeleaf
  • Reflection: Java's reflection API (Class.forName(), getMethod()) is frequently the mechanism behind gadget chains — it lets code call methods by name at runtime, bypassing normal type-safety assumptions
  • Enterprise stack fingerprints: Spring Framework, Apache Tomcat, and WebLogic are common Java enterprise targets, each with their own CVE history worth checking during recon

Tips

  • Java's verbosity (getters/setters, explicit types) is a genuine trade-off for compile-time safety — don't fight the convention, IDEs auto-generate boilerplate
  • equals() vs ===: use .equals() for object content comparison, =====only compares references (memory addresses) for non-primitives — a very common bug source for anyone coming from Python where =====compares values
  • null handling is unenforced by the type system (unlike Rust's Option or Python's explicit None checks) — NullPointerException is the single most common runtime error in Java code
  • When reading a Java target during a pentest, check pom.xml or build.gradle (if source-accessible) for dependency versions before searching for known CVEs — version-specific gadget chains depend on exact library versions being present