? Java Sheet
? Java Sheet
Welcome to the ultimate Java cheat sheet! This guide will take you from
beginner to master level, covering all key concepts with real-world scenarios,
step-by-step solutions, best practices
📌 1. Introduction to Java
Imagine you are building a banking system. You need a language that is
secure, scalable, and platform-independent. Java is perfect because:
java
Copy
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java! 🚀");
}
}
📌 2. Java Basics
Java:
public class Variables {
public static void main(String[] args) {
int age = 25;
double price = 19.99;
boolean isJavaFun = true;
char grade = 'A';
String name = "Chandan";
🔹 Output:
Age: 25
Price: 19.99
Java is fun? true
Grade: A
Name: Chandan
Imagine you run an e-commerce website and need to apply discounts based
on cart value.
🛠️Solution:
java
public class DiscountSystem {
public static void main(String[] args) {
double cartValue = 150;
java
Copy
public class ATM {
public static void main(String[] args) {
int balance = 500;
int withdraw = 100;
🔹 Output:
Copy
Withdrawal successful! Remaining balance: 400
Withdrawal successful! Remaining balance: 300
...
📌 4. Object-Oriented Programming (OOP) 🚀
java
Copy
class Car {
String brand;
int price;
void showCar() {
System.out.println("Brand: " + brand + ", Price: " + price);
}
}
java
Copy
int[] orders = {101, 102, 103};
System.out.println(orders[0]); // Output: 101
java
Copy
ArrayList<String> orders = new ArrayList<>();
orders.add("Order1");
orders.add("Order2");
System.out.println(orders.get(0)); // Output: Order1
java
Copy
HashMap<String, String> users = new HashMap<>();
users.put("Alice", "[email protected]");
System.out.println(users.get("Alice")); // Output: [email protected]
java
Copy
Stack<String> undoStack = new Stack<>();
undoStack.push("Hello");
undoStack.push("Hello, World!");
System.out.println(undoStack.pop()); // Output: Hello, World!
🎯 Exception Handling
java
Copy
public class ExceptionHandling {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero! ❌");
}
}
}
🎯 Multithreading
Java:
class Task extends Thread {
public void run() {
System.out.println("Task is running 🏃♂️");
}
}
🎯 File Handling
java
Copy
import java.io.*;
📌 7. Design Patterns
🎯 Singleton Pattern
java
class Database {
private static Database instance;
private Database() {}
🎯 Factory Pattern
java
Copy
interface Vehicle {
void drive();
}
class VehicleFactory {
public static Vehicle getVehicle(String type) {
if (type.equals("car")) {
return new Car();
} else if (type.equals("bike")) {
return new Bike();
}
return null;
}
}
🎯 Conclusion 🎯
Mastering Java will help you crack coding interviews and build scalable
applications! 🚀 Keep practicing and happy coding! 💡
This cheat sheet will keep evolving, so stay tuned for more updates! 🔥