1. Basic Syntax and Structure
Data Types
- Primitive Types:
int,double,char,boolean,long,float,short,byte - Non-Primitive Types:
String,Array,Classes
Variables
int a = 10; // Integer String name = "SDET"; // String boolean isActive = true; // Boolean
Control Statements
- If-Else:
if (condition) {
// Code block
} else {
// Code block
}- Switch:
switch (value) {
case 1: System.out.println("One"); break;
default: System.out.println("Default");
}- Loops:
for (int i = 0; i < 10; i++) {}
while (condition) {}
do {} while (condition);2. OOP Basics
Class and Object
class Car {
String color;
void drive() {
System.out.println("Driving");
}
}
Car myCar = new Car();
myCar.drive();Inheritance
class Vehicle {
void start() { System.out.println("Vehicle started"); }
}
class Car extends Vehicle {}
Car car = new Car();
car.start();Polymorphism
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
Animal obj = new Dog();
obj.sound(); // Outputs "Bark"Encapsulation
class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}Abstraction
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing Circle"); }
}3. Java Collections Framework
List
- ArrayList: Dynamic array
List<String> list = new ArrayList<>();
list.add("Test");Set
- HashSet: No duplicates
Set<Integer> set = new HashSet<>(); set.add(1);
Map
- HashMap: Key-value pairs
Map<Integer, String> map = new HashMap<>(); map.put(1, "One");
4. Java Streams and Lambda
List<String> names = Arrays.asList("Alice", "Bob");
names.stream().filter(name -> name.startsWith("A"))
.forEach(System.out::println);5. File Handling
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}6. Multithreading
class MyThread extends Thread {
public void run() {
System.out.println("Thread Running");
}
}
MyThread t = new MyThread();
t.start();7. Selenium with Java
WebDriver Setup
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");Locators
WebElement element = driver.findElement(By.id("elementId"));Actions
element.click();
element.sendKeys("text");Waits
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id")));8. Exception Handling
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Division by zero");
} finally {
System.out.println("Always executed");
}9. API Testing with Java (Rest Assured)
Response response = RestAssured.get("https://api.example.com/data");
System.out.println(response.getStatusCode());10. Key Design Patterns
- Singleton:
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) instance = new Singleton();
return instance;
}
}- Factory:
class ShapeFactory {
Shape getShape(String shapeType) {
if (shapeType.equals("CIRCLE")) return new Circle();
return null;
}
}11. Maven Basics
- Add dependencies in
pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.1</version>
</dependency>- Run commands:
mvn clean install
12. TestNG Basics
- Annotations:
@Test @BeforeClass @AfterMethod
- Execute test cases via XML:
<suite name="Suite">
<test name="Test">
<classes>
<class name="TestClass"/>
</classes>
</test>
</suite>13. Advanced Concepts
Generics
public class Box<T> {
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}Reflection API
Class<?> clazz = Class.forName("java.util.ArrayList");
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}Streams Advanced
List<Integer> numbers = Arrays.asList(1, 2, 3, 4); int sum = numbers.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum(); System.out.println(sum);
Functional Interfaces
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(5, 10));CompletableFuture
CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(result -> result + " World")
.thenAccept(System.out::println);Advanced Multithreading
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> System.out.println("Task 1"));
executor.submit(() -> System.out.println("Task 2"));
executor.shutdown();Annotations
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
String value();
}14. Best Practices
- Follow DRY (Don’t Repeat Yourself).
- Use Page Object Model (POM).
- Implement logging with Log4j.
- Parameterize tests with DataProvider.
- Optimize code with streams and lambdas.
This cheatsheet provides essential Java concepts and code snippets for SDETs, covering everything from basics to practical use in test automation
🔥 Level Up Your SDET Skills 🔥
Monthly Drop : Real-world automation • Advanced interview strategies • Members-only resources
