Table of Contents:
Java Cheat Sheet — Complete Java Syntax, Classes, and Examples
Java is a powerful, object-oriented programming language widely used for developing web, desktop, and mobile applications. This Java Cheat Sheet provides a quick reference for syntax, keywords, data types, and essential programming structures — all in one place.
What is Java?
Java is a platform-independent, high-level language developed by James Gosling at Sun Microsystems (now owned by Oracle). It follows the “Write Once, Run Anywhere (WORA)” principle, meaning compiled Java code can run on any platform that supports Java.
Basic Java Syntax
Every Java program must have a class and a main() method — the entry point of the program.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Explanation:
public class HelloWorld→ Defines a class.public static void main(String[] args)→ Main method, execution starts here.System.out.println()→ Prints output to console.
Java Data Types and Variables
| Type | Category | Example | Description |
|---|---|---|---|
int | Primitive | int age = 25; | Integer numbers |
float | Primitive | float price = 12.5f; | Decimal numbers |
double | Primitive | double rate = 15.99; | Higher precision decimals |
char | Primitive | char grade = 'A'; | Single character |
boolean | Primitive | boolean isTrue = true; | True or false |
String | Non-Primitive | String name = "Java"; | Sequence of characters |
Java Operators
| Operator Type | Example | Description |
|---|---|---|
| Arithmetic | +, -, *, /, % | Mathematical operations |
| Relational | ==, !=, >, <, >=, <= | Compare two values |
| Logical | `&&, | |
| Assignment | =, +=, -=, *=, /= | Assign values |
| Increment/Decrement | ++, -- | Increase or decrease value |
Java Conditional Statements
Example: if, else if, else
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
Java Loops
For Loop
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
While Loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Do-While Loop
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Java Arrays
An array is a container that holds multiple values of the same type.
Example:
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[2]); // Output: 30
Java Methods
Methods allow reusability and organization of code.
Example:
public class Calculator {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(5, 10)); // Output: 15
}
}
Java Object-Oriented Programming (OOPs)
Java is built around OOP principles, which include:
| Concept | Description | Example |
|---|---|---|
| Encapsulation | Binding data & methods together | Classes with private variables |
| Inheritance | Acquiring properties of another class | extends keyword |
| Polymorphism | Performing actions in many forms | Method overloading/overriding |
| Abstraction | Hiding implementation details | Abstract classes & interfaces |
Example of OOP in Java:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Output: Bark
}
}
Java Strings
Strings are objects that represent a sequence of characters.
Example:
String s = "Hello Java";
System.out.println(s.toUpperCase());
System.out.println(s.length());
| Method | Description |
|---|---|
length() | Returns string length |
toLowerCase() | Converts to lowercase |
toUpperCase() | Converts to uppercase |
concat() | Joins strings |
equals() | Compares two strings |
java cheat sheet, java syntax reference, java data types, java loops examples, java methods and classes, java oops concepts, java string handling, java exception handling, java collections framework, java basics for beginners
Java Exception Handling
Handle runtime errors gracefully using try-catch.
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero");
}
Java Collections Framework
Collections store and manipulate groups of objects efficiently.
| Interface | Common Class | Example |
|---|---|---|
| List | ArrayList, LinkedList | Stores ordered elements |
| Set | HashSet, TreeSet | No duplicate elements |
| Map | HashMap, TreeMap | Key-value pairs |
Java Input and Output
Example:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.println("Hello " + name);
}
}
You may Like
FAQ — Java Cheat Sheet
Q1: What is Java used for?
Java is used to build desktop, mobile, and enterprise applications — including Android apps and web servers.
Q2: What is JDK and JRE?
- JDK (Java Development Kit): Used for developing Java applications.
- JRE (Java Runtime Environment): Used for running Java programs.
Q3: What is the difference between == and .equals() in Java?== compares references, while .equals() compares actual string content.
Q4: Can Java run without an internet connection?
Yes, Java programs run locally using the JVM (Java Virtual Machine).
Q5: How do I handle exceptions in Java?
Use try-catch-finally blocks to catch runtime errors and handle them safely.
