0% found this document useful (0 votes)
10 views

? Java Sheet

This Java Master Cheat Sheet provides a comprehensive guide for beginners to advanced users, covering key concepts such as installation, variables, control flow, object-oriented programming, data structures, advanced concepts, design patterns, and best practices. It includes real-world scenarios and code examples for practical understanding. The document emphasizes the importance of mastering Java for coding interviews and application development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

? Java Sheet

This Java Master Cheat Sheet provides a comprehensive guide for beginners to advanced users, covering key concepts such as installation, variables, control flow, object-oriented programming, data structures, advanced concepts, design patterns, and best practices. It includes real-world scenarios and code examples for practical understanding. The document emphasizes the importance of mastering Java for coding interviews and application development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

🚀 Java Master Cheat Sheet (Expanded) 🚀

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

🌍 Real-World Scenario: Why Java?

Imagine you are building a banking system. You need a language that is
secure, scalable, and platform-independent. Java is perfect because:

✅ Object-oriented: Helps structure code efficiently.

✅ Platform-independent: Runs on any device using the Java Virtual


Machine (JVM).

✅ Secure: Essential for handling transactions.

🛠️Java Installation & Setup

1️⃣ Download JDK from Oracle or use OpenJDK.


2️⃣ Set up Java Environment Variables (JAVA_HOME).
3️⃣ Install an IDE: IntelliJ IDEA, Eclipse, or VS Code.
4️⃣ Run your first Java program! 🎉

java
Copy
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java! 🚀");
}
}

🔹 Output: Hello, Java! 🚀

📌 2. Java Basics

🎯 Variables & Data Types

Java has different types of variables:

 int (integer) ➝ int age = 25; 🎂


 double (decimal) ➝ double price = 19.99; 💲
 boolean (true/false) ➝ boolean isJavaFun = true; ✅
 char (single character) ➝ char grade = 'A'; 🎖️
 String (text) ➝ String name = "Chandan"; 💬

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";

System.out.println("Age: " + age);


System.out.println("Price: " + price);
System.out.println("Java is fun? " + isJavaFun);
System.out.println("Grade: " + grade);
System.out.println("Name: " + name);
}
}

🔹 Output:

Age: 25
Price: 19.99
Java is fun? true
Grade: A
Name: Chandan

📌 3. Control Flow (if-else, loops)

🌍 Scenario: E-commerce Discount System 🛒

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;

if (cartValue > 100) {


System.out.println("You get a 10% discount! 🎉");
} else {
System.out.println("No discount. Add more items! 🛍️");
}
}
}

🔹 Output: You get a 10% discount! 🎉

🔁 Loops (for, while, do-while)

🌍 Scenario: ATM Withdrawal 💵

java
Copy
public class ATM {
public static void main(String[] args) {
int balance = 500;
int withdraw = 100;

while (balance >= withdraw) {


balance -= withdraw;
System.out.println("Withdrawal successful! Remaining balance: " + balance);
}
}
}

🔹 Output:

Copy
Withdrawal successful! Remaining balance: 400
Withdrawal successful! Remaining balance: 300
...
📌 4. Object-Oriented Programming (OOP) 🚀

🌍 Scenario: Car Dealership 🚗

java
Copy
class Car {
String brand;
int price;

Car(String brand, int price) {


this.brand = brand;
this.price = price;
}

void showCar() {
System.out.println("Brand: " + brand + ", Price: " + price);
}
}

public class CarDealership {


public static void main(String[] args) {
Car car1 = new Car("Tesla", 50000);
car1.showCar();
}
}

🔹 Output: Brand: Tesla, Price: 50000

📌 5. Data Structures & Algorithms (DSA) 📊

🌍 Scenario: Managing Customer Orders 📦

📌 Arrays (Fixed-size collections)

java
Copy
int[] orders = {101, 102, 103};
System.out.println(orders[0]); // Output: 101

📌 ArrayList (Dynamic collections)

java
Copy
ArrayList<String> orders = new ArrayList<>();
orders.add("Order1");
orders.add("Order2");
System.out.println(orders.get(0)); // Output: Order1

📌 HashMap (Key-Value Pair - Storing User Data)

java
Copy
HashMap<String, String> users = new HashMap<>();
users.put("Alice", "[email protected]");
System.out.println(users.get("Alice")); // Output: [email protected]

📌 Stack (Undo Feature in Editor)

java
Copy
Stack<String> undoStack = new Stack<>();
undoStack.push("Hello");
undoStack.push("Hello, World!");
System.out.println(undoStack.pop()); // Output: Hello, World!

📌 6. Advanced Java Concepts

🎯 Exception Handling

🌍 Scenario: Handling Invalid User Input

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! ❌");
}
}
}

🔹 Output: Cannot divide by zero! ❌

🎯 Multithreading

🌍 Scenario: Running Multiple Tasks Simultaneously

Java:
class Task extends Thread {
public void run() {
System.out.println("Task is running 🏃‍♂️");
}
}

public class Multithreading {


public static void main(String[] args) {
Task task1 = new Task();
task1.start();
}
}

🔹 Output: Task is running 🏃‍♂️

🎯 File Handling

🌍 Scenario: Reading and Writing Files

java
Copy
import java.io.*;

public class FileHandling {


public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, Java! 🚀");
writer.close();

FileReader reader = new FileReader("example.txt");


int data;
while ((data = reader.read()) != -1) {
System.out.print((char) data);
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred! ❌");
}
}
}

🔹 Output: Hello, Java! 🚀

📌 7. Design Patterns

🎯 Singleton Pattern

🌍 Scenario: Managing a Single Database Connection

java
class Database {
private static Database instance;

private Database() {}

public static Database getInstance() {


if (instance == null) {
instance = new Database();
}
return instance;
}

public void connect() {


System.out.println("Connected to the database! 🗄️");
}
}

public class SingletonPattern {


public static void main(String[] args) {
Database db = Database.getInstance();
db.connect();
}
}

🔹 Output: Connected to the database! 🗄️

🎯 Factory Pattern

🌍 Scenario: Creating Different Types of Vehicles

java
Copy
interface Vehicle {
void drive();
}

class Car implements Vehicle {


public void drive() {
System.out.println("Driving a car! 🚗");
}
}

class Bike implements Vehicle {


public void drive() {
System.out.println("Riding a bike! 🚴");
}
}

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;
}
}

public class FactoryPattern {


public static void main(String[] args) {
Vehicle vehicle = VehicleFactory.getVehicle("car");
vehicle.drive();
}
}

🔹 Output: Driving a car! 🚗

📌 8. Java Best Practices ✅

 Use meaningful variable names (customerName instead of c).


 Follow DRY Principle (Don't Repeat Yourself - Use functions!).
 Use ArrayList instead of Arrays (unless size is fixed).
 Optimize loops (Use for-each instead of traditional loops where
possible).
 Always handle exceptions (Avoid crashes in production!).

🎯 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! 🔥

You might also like