The Cheat Sheet

hello There : Java
Variables

Primitive Data Types

SE

Declare a variable with a primitive data type

int x = 10;
float y = 20.5f;
boolean z = true;

Reference Data Types

SE

Declare a variable with a reference data type

String s = 'Hello, world!';
Conditionals

if

SE

Evaluate a condition and execute code if it's true

if (x > y) {
  /* ... */
}

else if

SE

Evaluate another condition if the previous one is false

else if (x < y) {
  /* ... */
}

else

SE

Execute code if all previous conditions are false

else {
  /*            */
}
Loops

for

SE

Create a loop that iterates a specific number of times

for (int i = 0; i < 10; i++) {
  /* ... */
}

while

SE

Create a loop that continues as long as the condition is true

while (x < 10) {
  /* ... */
  x++;
}

do while

SE

Execute code once, then repeat the loop as long as the condition is true

do {
  /* ... */
  x++;
} while (x < 10);
Functions

Function Declaration

SE

Declare a function with a given name

public static void myFunction() {
  /* ... */
}

Function Overloading

SE

Declare multiple functions with the same name but different parameters

public static void myFunction(int x) {
  /* ... */
}

public static void myFunction(String s) {
  /* ... */
}

Function Recursion

SE

A function that calls itself

public static int factorial(int n) {
  if (n == 1) return 1;
  return n * factorial(n - 1);
}
Arrays

Array Declaration

SE

Declare an array

int[] myArray = new int[5];

Array Initialization

SE

Initialize an array with values

int[] myArray = {1, 2, 3, 4, 5};
Objects

Object Declaration

SE

Declare an object of a custom class

MyClass myObject = new MyClass();

Object Inheritance

SE

Create a subclass that inherits from a parent class

class MySubclass extends MyClass {
  /* ... */
}
Error Handling

try, catch, finally

SE

Handle errors and exceptions using trycatch, and finally blocks

try {
  /* ... */
} catch (Exception e) {
  /* ... */
} finally {
  /* ... */
}

throw

SE

Throw an exception

if (x < 0) {
  throw new IllegalArgumentException('x must be non-negative');
}

Custom Exception

SE

Create a custom exception class

class MyException extends Exception {
  public MyException(String message) {
    super(message);
  }
}
Collections

ArrayList

SE

A dynamic, resizable array

ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(1);
myList.add(2);
myList.remove(0);

HashMap

SE

A collection of key-value pairs

HashMap<String, Integer> myMap = new HashMap<String, Integer>();
myMap.put('one', 1);
myMap.put('two', 2);
myMap.remove('one');
Concurrency

Thread

SE

Create a new thread by extending the Thread class

class MyThread extends Thread {
  public void run() {
    /* ... */
  }
}

MyThread t = new MyThread();
t.start();

Runnable

SE

Create a new thread by implementing the Runnable interface

class MyRunnable implements Runnable {
  public void run() {
    /* ... */
  }
}

Thread t = new Thread(new MyRunnable());
t.start();

synchronized

SE

Synchronize a block of code to prevent concurrent access

synchronized (myObject) {
  /* ... */
}