Hello World Program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Comments:
// Single-line comment
/* Multi-line comment */
/** Documentation comment */Primitive Data Types:
byte b = 10; // 8-bit signed integer
short s = 20; // 16-bit signed integer
int i = 30; // 32-bit signed integer
long l = 40L; // 64-bit signed integer
float f = 50.0f; // 32-bit floating point
double d = 60.0; // 64-bit floating point
char c = 'A'; // 16-bit Unicode character
boolean bool = true; // true or falseNon-Primitive Data Types:
String str = "Hello";
int[] arr = {1, 2, 3};Declaration and Initialization:
int num; // Declaration
num = 5; // Initialization
int num2 = 10; // Declaration + InitializationConstants:
final int CONSTANT = 100;Arithmetic Operators:
int sum = 10 + 20; // Addition
int diff = 20 - 10; // Subtraction
int prod = 10 * 20; // Multiplication
int quot = 20 / 10; // Division
int rem = 20 % 10; // ModulusRelational Operators:
int a = 10, b = 20;
boolean result;
result = (a == b); // Equal to
result = (a != b); // Not equal to
result = (a > b); // Greater than
result = (a < b); // Less than
result = (a >= b); // Greater than or equal to
result = (a <= b); // Less than or equal toLogical Operators:
boolean x = true, y = false;
boolean res;
res = (x && y); // Logical AND
res = (x || y); // Logical OR
res = (!x); // Logical NOTAssignment Operators:
int num = 10;
num += 5; // num = num + 5
num -= 5; // num = num - 5
num *= 5; // num = num * 5
num /= 5; // num = num / 5
num %= 5; // num = num % 5Unary Operators:
int num = 10;
num++; // Post-increment
num--; // Post-decrement
++num; // Pre-increment
--num; // Pre-decrementIf-Else:
int num = 10;
if (num > 0) {
System.out.println("Positive");
} else if (num < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}Switch:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}Loops:
For Loop:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}While Loop:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}Do-While Loop:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);Single-Dimensional Array:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}Multi-Dimensional Array:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}Method Declaration and Invocation:
public class MyClass {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.sayHello();
int result = obj.add(5, 10);
System.out.println(result);
}
void sayHello() {
System.out.println("Hello");
}
int add(int a, int b) {
return a + b;
}
}Method Overloading:
public class MyClass {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}Classes and Objects:
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.println(obj.x);
}
}Constructors:
public class MyClass {
int x;
MyClass(int y) {
x = y;
}
public static void main(String[] args) {
MyClass obj = new MyClass(10);
System.out.println(obj.x);
}
}Inheritance:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}Polymorphism:
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();
}
}Abstraction:
abstract class Animal {
abstract void 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();
}
}Encapsulation:
public class MyClass {
private int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}Interfaces:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}Try-Catch:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
}Finally:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
} finally {
System.out.println("This will always execute");
}
- **Throw:**
```java
public class MyClass {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
} else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(15);
}
}public class MyClass {
static void checkAge(int age) throws ArithmeticException {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
} else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
try {
checkAge(15);
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}List:
import java.util.ArrayList;
import java.util.List;
public class MyClass {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
for (String fruit : list) {
System.out.println(fruit);
}
}
}Set:
import java.util.HashSet;
import java.util.Set;
public class MyClass {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
for (String fruit : set) {
System.out.println(fruit);
}
}
}Map:
import java.util.HashMap;
import java.util.Map;
public class MyClass {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Cherry");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}Reading Input:
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
}
}Writing Output:
import java.io.FileWriter;
import java.io.IOException;
public class MyClass {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, World!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}Creating Threads:
public class MyClass extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args) {
MyClass thread = new MyClass();
thread.start();
}
}Runnable Interface:
public class MyClass implements Runnable {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args) {
MyClass obj = new MyClass();
Thread thread = new Thread(obj);
thread.start();
}
}interface MyFunctionalInterface {
void myMethod();
}
public class MyClass {
public static void main(String[] args) {
MyFunctionalInterface msg = () -> {
System.out.println("Hello, Lambda");
};
msg.myMethod();
}
}import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MyClass {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<String> result = names.stream()
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
result.forEach(System.out::println);
}
}Default Methods in Interfaces:
interface MyInterface {
default void defaultMethod() {
System.out.println("Default Method");
}
}
public class MyClass implements MyInterface {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.defaultMethod();
}
}Static Methods in Interfaces:
interface MyInterface {
static void staticMethod() {
System.out.println("Static Method");
}
}
public class MyClass implements MyInterface {
public static void main(String[] args) {
MyInterface.staticMethod();
}
}This cheat sheet covers the most critical aspects of Java programming. It's a quick reference guide to help you with syntax and basic concepts while working on Java projects.