Back to cheat sheet hub

Java Cheat Sheet

A focused Java quick reference for automation engineers covering the core language concepts we teach before Selenium.

Selenium Module 1 Java fundamentals Interview friendly

Use it for

Revision before coding, TestNG work, Selenium helpers, and interview practice.

Focus

Language basics that show up constantly in framework code and live coding rounds.

Default habit

Prefer readable methods, clear naming, and small reusable objects instead of long procedural test code.

Best pair

Use this with the Selenium, TestNG, and framework cheat sheets.

Variables and Data Types

Java is strongly typed, so declare the type explicitly and keep values predictable.

int score = 95;
double duration = 2.5;
boolean passed = true;
String course = "Selenium";
char grade = 'A';

Control Flow

Use conditionals to branch logic clearly in utility code or validation helpers.

if (score >= 90) {
    System.out.println("Excellent");
} else if (score >= 50) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Loops

Loops are useful for lists, tables, and reusable data-driven checks.

for (int i = 0; i < 3; i++) {
    System.out.println(i);
}

for (String topic : List.of("Java", "Selenium", "TestNG")) {
    System.out.println(topic);
}

Methods

Methods keep repeated test logic and framework utilities maintainable.

public static int addPoints(int current, int extra) {
    return current + extra;
}

public static void logMessage(String message) {
    System.out.println(message);
}

Strings

String operations are common in assertions, locators, logs, and test data shaping.

String name = "Playwright";
System.out.println(name.length());
System.out.println(name.toUpperCase());
System.out.println(name.contains("wright"));
System.out.println(name.equals("Playwright"));

Classes and Objects

Framework design depends on understanding how objects hold state and behavior.

class Student {
    String name;

    Student(String name) {
        this.name = name;
    }

    void greet() {
        System.out.println("Hello " + name);
    }
}

OOP Essentials

Inheritance, abstraction, and interfaces matter when building page classes and base utilities.

interface Reportable {
    void generate();
}

abstract class BasePage {
    abstract void open();
}

Exception Handling

Catch only when you can add context or recover meaningfully.

try {
    int result = 10 / 2;
    System.out.println(result);
} catch (Exception e) {
    System.out.println("Something failed: " + e.getMessage());
} finally {
    System.out.println("Cleanup done");
}

Collections

Lists, sets, and maps appear in test data, configuration, and result aggregation.

List tools = new ArrayList<>();
tools.add("Selenium");
tools.add("TestNG");

Map scores = new HashMap<>();
scores.put("Pramod", 95);