Java Cheat Sheet - This is a concise Java cheat sheet based on core concepts and syntax. It is designed to help you quickly reference Java fundamentals.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class Main
: Defines a class namedMain
.public static void main(String[] args)
: Entry point of the program.System.out.println()
: Prints output to the console.
Data Type | Size | Description |
---|---|---|
byte |
1 byte | Stores whole numbers (-128 to 127) |
short |
2 bytes | Stores whole numbers (-32,768 to 32,767) |
int |
4 bytes | Stores whole numbers (-2^31 to 2^31-1) |
long |
8 bytes | Stores whole numbers (-2^63 to 2^63-1) |
float |
4 bytes | Stores fractional numbers (6-7 decimal digits) |
double |
8 bytes | Stores fractional numbers (15 decimal digits) |
boolean |
1 bit | Stores true or false |
char |
2 bytes | Stores a single character/letter |
int num = 10; // Integer
double pi = 3.14; // Double
char letter = 'A'; // Character
boolean isJavaFun = true; // Boolean
String name = "Java"; // String
Type | Operators |
---|---|
Arithmetic | + , - , * , / , % , ++ , -- |
Assignment | = , += , -= , *= , /= |
Comparison | == , != , > , < , >= , <= |
Logical | && , ` |
if (condition) {
// code
} else if (condition) {
// code
} else {
// code
}
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
for (int i = 0; i < 5; i++) {
// code
}
while (condition) {
// code
}
do {
// code
} while (condition);
int[] numbers = {1, 2, 3, 4, 5}; // Array declaration
System.out.println(numbers[0]); // Access first element
numbers[1] = 10; // Modify second element
int length = numbers.length; // Get array length
public static int add(int a, int b) {
return a + b;
}
// Method call
int result = add(5, 10);
class Car {
String brand; // Field
// Constructor
public Car(String brand) {
this.brand = brand;
}
// Method
public void drive() {
System.out.println("Driving " + brand);
}
}
// Create an object
Car myCar = new Car("Toyota");
myCar.drive();
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle {
void run() {
System.out.println("Bike is running");
}
}
class Person {
private String name; // Private field
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String name) {
this.name = name;
}
}
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
try {
int[] arr = new int[5];
System.out.println(arr[10]); // Throws ArrayIndexOutOfBoundsException
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
System.out.println("This will always execute");
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
File file = new File("example.txt");
FileWriter writer = new FileWriter(file);
writer.write("Hello, Java!");
writer.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
String str = "Hello, Java!";
int length = str.length(); // Length of string
String upper = str.toUpperCase(); // Convert to uppercase
String lower = str.toLowerCase(); // Convert to lowercase
boolean contains = str.contains("Java"); // Check if string contains substring
String substring = str.substring(0, 5); // Extract substring
import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits.get(0)); // Access element
fruits.remove(1); // Remove element
import java.util.HashMap;
HashMap<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);
System.out.println(map.get("Apple")); // Access value
map.remove("Banana"); // Remove key-value pair
interface MathOperation {
int operate(int a, int b);
}
public class Main {
public static void main(String[] args) {
MathOperation add = (a, b) -> a + b;
System.out.println(add.operate(5, 10)); // Output: 15
}
}
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
}
}