Java Cheat Sheet
1. Basic Syntax
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Data Types
int - Integer (e.g., 10)
float - Floating point number (e.g., 10.5f)
char - Character (e.g., 'A')
String - Sequence of characters (e.g., "Hello")
boolean - True or False
3. Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
4. Control Statements
if (condition) {
// code block
} else {
// another code block
}
Java Cheat Sheet
switch(expression) {
case value1:
// code
break;
default:
// code
}
5. Loops
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
while (condition) {
// loop body
}
6. Functions
public static int add(int a, int b) {
return a + b;
}
7. Object-Oriented Programming (OOP)
class Animal {
String name;
void makeSound() {
System.out.println("Sound");
}
}
public class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
Java Cheat Sheet
}
}
8. Exception Handling
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
9. File Handling
import java.io.*;
try {
FileWriter writer = new FileWriter("file.txt");
writer.write("Hello, Java");
writer.close();
} catch (IOException e) {
System.out.println("An error occurred.");
}
10. Important Packages & Classes
java.lang - Core Java classes (String, Math, Integer, etc.)
java.util - Data structures & utilities (ArrayList, HashMap, etc.)
java.io - File handling (File, FileReader, FileWriter)
java.net - Networking (Socket, URL, HttpURLConnection)
java.sql - Database connectivity (Connection, Statement, ResultSet)