FOR TALENT
Blog Explore the trends. Codebrew Stay up to date. Interview Questions Get prepared. Coder Glossary Learn the concepts. Portfolio Showcase your projects. University Preferences Make the right choice. Roadmap Move forward step by step. Cheat Sheets Look up the shortcuts. Free Software Tools Get a hand. Podcast Listen on the go. Salaries Learn what the market pays. AI Tools Explore AI.FOR COMPANIES
Blog HR trends. Codebrew Stay in the loop. HR Webinars Hear from the experts. Case Studies Get inspired. E-book Download the guides. HR Calculation Tools Calculate with ease.
This Java cheat sheet includes symbols, ranges, grouping, assertions, and sample patterns.
// Hello World
// Basic Commands
// Getting Started Resources
// Primitive Data Types
// Type Conversion
int i = (int) 3.14; // 3
double d = 5; // 5.0
// Data Types Resources
// Arithmetic Operators
// Comparison Operators
// Logical Operators
// Operators Resources
// if-else Statement
int x = 10;
if (x > 5) {
System.out.println("Greater");
} else {
System.out.println("Smaller or equal");
}
// switch Statement
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other");
}
// for Loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// Enhanced for Loop
int[] arr = {1,2,3};
for (int n : arr) {
System.out.println(n);
}
// while Loop
int i = 0;
while (i < 3) {
System.out.println(i);
i++;
}
// do-while Loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 3);
// Loop Control Keywords
// Control Flow Resources
// Array Declaration
int[] arr = new int[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
// Array Initialization
int[] arr = {1, 2, 3};
// Iterate Array
for (int n : arr) {
System.out.println(n);
}
// Arrays Resources
// Common Collections
// ArrayList Example
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
System.out.println(list.get(0));
// HashMap Example
import java.util.HashMap;
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
System.out.println(map.get("A"));
// Collections Resources
// Class Definition
public class Car {
private String brand;
public Car(String brand) {
this.brand = brand;
}
public void start() {
System.out.println(brand + " is starting");
}
}
// Object Instantiation
Car car = new Car("Toyota");
car.start();
// Inheritance
public class Animal {
public void speak() {
System.out.println("Animal speaks");
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println("Woof!");
}
}
// Interface
public interface Drawable {
void draw();
}
public class Circle implements Drawable {
public void draw() {
System.out.println("Drawing Circle");
}
}
// Abstract Class
public abstract class Shape {
abstract void draw();
}
public class Square extends Shape {
void draw() {
System.out.println("Drawing Square");
}
}
// OOP Principles
// OOP Resources
// Creational Patterns
// Singleton Pattern
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
// Factory Pattern
public interface Product { }
public class ConcreteProductA implements Product { }
public class ConcreteProductB implements Product { }
public class Factory {
public Product createProduct(String type) {
if (type.equals("A")) return new ConcreteProductA();
else return new ConcreteProductB();
}
}
// Design Patterns Resources
// Lambda Expression
List<String> list = Arrays.asList("a", "b", "c");
list.forEach(s -> System.out.println(s));
// Stream API
List<Integer> nums = Arrays.asList(1,2,3,4,5);
int sum = nums.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();
// Optional
Optional<String> opt = Optional.ofNullable(null);
System.out.println(opt.orElse("default"));
// Method Reference
list.forEach(System.out::println);
// Functional Interfaces
// Functional Programming Resources
// try-catch-finally
try {
int x = 1 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Always runs");
}
// Custom Exception
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
// try-with-resources
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line = br.readLine();
}
// Common Exceptions
// Exception Handling Resources
// Reading a File
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
// Writing to a File
try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))) {
bw.write("Hello!");
}
// NIO Files API
List<String> lines = Files.readAllLines(Paths.get("file.txt"));
// Serialization
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("obj.ser"));
out.writeObject(obj);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("obj.ser"));
MyClass obj2 = (MyClass) in.readObject();
in.close();
// File I/O & Serialization Resources
// Thread Creation
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
new MyThread().start();
Runnable r = () -> System.out.println("Runnable running");
new Thread(r).start();
// ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> System.out.println("Task 1"));
executor.shutdown();
// Synchronization
synchronized(this) {
// critical section
}
// Locks & Atomic
Lock lock = new ReentrantLock();
lock.lock();
try {
// critical section
} finally {
lock.unlock();
}
AtomicInteger ai = new AtomicInteger(0);
ai.incrementAndGet();
// Concurrency Resources
Get inspired, learn, join competitions and grow in your job!
Sign up now