Visit Website

Java Complete Cheat Sheet ☕ OOP, Collections & Methods आसान भाषा में Explained | Beginners Guide

Learn Java basics, OOP, methods, arrays, strings, collections, exceptions and more with this simple beginner-friendly Java cheat sheet.


तो स्वागत है आपका। 👋

अगर आप programming सीखना शुरू कर रहे हैं, तो Java एक ऐसी language है जिसे सीखना काफी useful हो सकता है।

Java का इस्तेमाल software, backend applications, enterprise applications, Android development और कई दूसरे areas में किया जाता है।

लेकिन शुरुआत में Java थोड़ी बड़ी लग सकती है। Variables अलग, methods अलग, classes अलग और फिर आता है OOP, Collections, Exception Handling जैसी चीजें।

इसलिए इस article में हम Java को एक साथ समझेंगे।

यह कोई सिर्फ theory वाला article नहीं है। यहाँ आपको Java syntax, variables, conditions, loops, methods, arrays, strings, OOP, inheritance, polymorphism, collections, exceptions, generics, streams और useful commands के examples भी मिलेंगे।


☕ Java क्या है?

Java एक high-level, object-oriented programming language है।

Java की सबसे खास बात यह है कि इसका code Java Virtual Machine यानी JVM पर run होता है।

एक simple flow ऐसे समझ सकते हैं:

Java Source Code
       ↓
    Compiler
       ↓
   Bytecode
       ↓
      JVM
       ↓
    Program

Java की एक popular line है:

Write Once, Run Anywhere

मतलब Java bytecode को अलग-अलग systems पर JVM की मदद से run किया जा सकता है।


🧰 Java के लिए क्या चाहिए?

Java program लिखने के लिए आमतौर पर आपको JDK (Java Development Kit) चाहिए।

JDK में development के लिए जरूरी tools मिलते हैं।

Basic setup:

JDK
 ↓
Java Compiler
 ↓
Java Code
 ↓
Run Program

आप Java code किसी simple text editor में भी लिख सकते हैं, लेकिन बड़े projects के लिए IntelliJ IDEA, Eclipse या VS Code जैसे IDE useful होते हैं।


📌 पहला Java Program

Java में सबसे basic program:

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Output:

Hello World

यहाँ:

  • class class बनाने के लिए

  • main() program का starting point

  • System.out.println() screen पर output दिखाने के लिए


📦 Java Class क्या है?

Class को आप एक blueprint की तरह समझ सकते हैं।

Example:

class Student {
    String name;
    int age;
}

अब इस class का object बनाया जा सकता है:

Student s1 = new Student();

🧱 Java Variables

Variable ऐसी जगह है जहाँ हम data store करते हैं।

int age = 25;
String name = "Rahul";
double price = 99.50;
boolean active = true;

यहाँ:

TypeExample
int25
double99.50
char'A'
booleantrue
String"Hello"

🔢 Java Data Types

Java में data types को दो main categories में समझ सकते हैं:

Primitive Types

byte
short
int
long
float
double
char
boolean

Example:

int number = 100;
long population = 1000000L;
float height = 5.8f;
double price = 99.99;
char grade = 'A';
boolean passed = true;

Non-Primitive Types

जैसे:

String
Array
Class
Interface
Object

➕ Java Operators

Arithmetic Operators

int a = 10;
int b = 3;

System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);

Operators:

+   Addition
-   Subtraction
*   Multiplication
/   Division
%   Remainder

Comparison Operators

==   Equal
!=   Not Equal
>    Greater
<    Smaller
>=   Greater or Equal
<=   Smaller or Equal

Logical Operators

&&   AND
||   OR
!    NOT

🔀 Java if-else

Condition check करने के लिए if-else use करते हैं।

int age = 20;

if (age >= 18) {
    System.out.println("Adult");
} else {
    System.out.println("Minor");
}

Multiple conditions:

if (marks >= 80) {
    System.out.println("A Grade");
} else if (marks >= 60) {
    System.out.println("B Grade");
} else {
    System.out.println("Needs Improvement");
}

🎯 Java switch

जब कई fixed options में से एक choose करना हो, तो switch useful हो सकता है।

int day = 2;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    default:
        System.out.println("Other Day");
}

Modern Java में switch के नए forms भी available हैं।


🔁 Java Loops

जब किसी काम को बार-बार करना हो, तो loops काम आते हैं।

for Loop

for (int i = 1; i <= 5; i++) {
    System.out.println(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);

Enhanced for Loop

Array या collection के items पढ़ने के लिए:

for (String name : names) {
    System.out.println(name);
}

🛑 break और continue

break

Loop को रोक देता है।

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        break;
    }

    System.out.println(i);
}

continue

Current iteration को skip करता है।

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    System.out.println(i);
}

📦 Java Arrays

एक ही type के कई values store करने के लिए array use किया जा सकता है।

int[] numbers = {10, 20, 30, 40, 50};

Value access:

System.out.println(numbers[0]);

Output:

10

Array index 0 से start होता है।

Array Loop

for (int number : numbers) {
    System.out.println(number);
}

Array Length

System.out.println(numbers.length);

🔤 Java String

Text के लिए String use किया जाता है।

String name = "Java";

कुछ useful methods:

name.length();
name.toUpperCase();
name.toLowerCase();
name.charAt(0);
name.contains("av");
name.startsWith("J");
name.endsWith("a");

Example:

String text = "Hello Java";

System.out.println(text.length());
System.out.println(text.toUpperCase());

🛠️ Java Methods

Method code के किसी काम को एक नाम देने जैसा है।

Example:

static void hello() {
    System.out.println("Hello");
}

Call:

hello();

📥 Method Parameters

Method में values भेज सकते हैं।

static void greet(String name) {
    System.out.println("Hello " + name);
}

Call:

greet("Rahul");

Output:

Hello Rahul

📤 Return Value

Method कोई value वापस भी दे सकता है।

static int add(int a, int b) {
    return a + b;
}

Use:

int result = add(10, 20);
System.out.println(result);

🔄 Method Overloading

एक ही class में same method name के multiple versions हो सकते हैं, अगर उनके parameters अलग हों।

static int add(int a, int b) {
    return a + b;
}

static int add(int a, int b, int c) {
    return a + b + c;
}

इसे Method Overloading कहते हैं।


🧠 OOP क्या है?

OOP का मतलब है:

Object-Oriented Programming

Java में OOP बहुत important concept है।

इसके main concepts:

Class
Object
Encapsulation
Inheritance
Polymorphism
Abstraction

🏗️ Class और Object

Class

class Car {
    String color;
    void drive() {
        System.out.println("Car is moving");
    }
}

Object

Car car = new Car();

car.color = "Red";
car.drive();

यहाँ Car class है और car उसका object है।


🔒 Encapsulation

Data और methods को एक class के अंदर रखना और direct access को control करना Encapsulation कहलाता है।

Example:

class Account {

    private double balance;

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }
}

यहाँ balance को private रखा गया है।


👨‍👦 Inheritance

एक class दूसरी class की properties और methods को inherit कर सकती है।

class Animal {

    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {

    void bark() {
        System.out.println("Barking");
    }
}

अब:

Dog dog = new Dog();

dog.eat();
dog.bark();

Dog को Animal का eat() method मिल गया।


🔄 Polymorphism

Polymorphism का simple मतलब है:

एक नाम, अलग-अलग behavior।

Method overriding इसका common example है।

class Animal {

    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {

    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

अब:

Animal a = new Dog();
a.sound();

Output:

Dog barks

🧩 Abstraction

जब हम जरूरी चीज दिखाते हैं और अंदर की complexity छुपा देते हैं, तो उसे abstraction कहते हैं।

Abstract class:

abstract class Animal {

    abstract void sound();

    void eat() {
        System.out.println("Eating");
    }
}

Subclass:

class Dog extends Animal {

    @Override
    void sound() {
        System.out.println("Bark");
    }
}

📐 Interface

Interface का इस्तेमाल classes के लिए contract define करने में किया जा सकता है।

interface Vehicle {

    void start();
}

Implement:

class Car implements Vehicle {

    public void start() {
        System.out.println("Car Started");
    }
}

🔐 Access Modifiers

Java में access control के लिए ये keywords important हैं:

ModifierBasic Meaning
publicकहीं से access किया जा सकता है
privateउसी class के अंदर
protectedclass/package और inheritance context में access
Defaultउसी package के अंदर

🧱 Constructors

Constructor object बनाते समय call होता है।

class Student {

    String name;

    Student(String name) {
        this.name = name;
    }
}

Object:

Student s = new Student("Rahul");

🪄 this Keyword

this current object को refer करता है।

class Student {

    String name;

    Student(String name) {
        this.name = name;
    }
}

यहाँ दोनों जगह name होने की वजह से this.name current object's variable को दिखाता है।


🧬 static Keyword

static member class से जुड़ा होता है, किसी एक object से नहीं।

Example:

class Counter {

    static int count = 0;
}

Access:

System.out.println(Counter.count);

🔒 final Keyword

final का इस्तेमाल value, method या class को restrict करने के लिए किया जा सकता है।

Variable:

final int MAX = 100;

अब MAX को दोबारा assign नहीं कर सकते।


📚 Java Collections Framework

जब आपको data के कई items manage करने हों, तब Collections बहुत useful हैं।

Common collections:

List
Set
Map
Queue
Deque

📝 ArrayList

ArrayList एक popular List implementation है।

ArrayList<String> names = new ArrayList<>();

names.add("Rahul");
names.add("Aman");
names.add("Ravi");

Value:

System.out.println(names.get(0));

Remove:

names.remove("Aman");

Size:

System.out.println(names.size());

🔗 LinkedList

LinkedList<String> list = new LinkedList<>();

list.add("A");
list.add("B");
list.add("C");

यह List और Deque दोनों तरह के operations के लिए use हो सकती है।


🚫 HashSet

HashSet unique values रखने के लिए useful है।

HashSet<Integer> numbers = new HashSet<>();

numbers.add(10);
numbers.add(20);
numbers.add(10);

Duplicate 10 को set दोबारा store नहीं करेगा।


🗺️ HashMap

Key-value data के लिए HashMap बहुत useful है।

HashMap<Integer, String> students = new HashMap<>();

students.put(1, "Rahul");
students.put(2, "Aman");

Value:

System.out.println(students.get(1));

Check:

students.containsKey(2);

Remove:

students.remove(1);

🚶 Queue

Queue में data आमतौर पर FIFO यानी First In, First Out तरीके से process किया जाता है।

Queue<String> queue = new LinkedList<>();

queue.add("A");
queue.add("B");

System.out.println(queue.poll());

Output:

A

↔️ Stack

Stack का basic concept LIFO यानी Last In, First Out है।

Modern Java code में stack operations के लिए Deque को prefer किया जा सकता है।

Deque<Integer> stack = new ArrayDeque<>();

stack.push(10);
stack.push(20);

System.out.println(stack.pop());

Output:

20

🧬 Generics

Generics से collection में किस type का data रहेगा, यह define कर सकते हैं।

ArrayList<String> names = new ArrayList<>();

यहाँ:

String

बताता है कि list में String values रखी जाएंगी।

एक और example:

ArrayList<Integer> numbers = new ArrayList<>();

⚠️ Exception Handling

Program run करते समय अगर कोई unexpected problem आती है, तो exception हो सकती है।

Java में इसे handle करने के लिए:

try
catch
finally
throw
throws

use किए जाते हैं।

Example:

try {

    int result = 10 / 0;

} catch (ArithmeticException e) {

    System.out.println("Cannot divide by zero");

}

🛡️ finally

finally block cleanup जैसे कामों के लिए useful होता है।

try {

    System.out.println("Try");

} catch (Exception e) {

    System.out.println("Error");

} finally {

    System.out.println("Done");
}

🚨 throw

खुद exception throw करने के लिए:

throw new IllegalArgumentException("Invalid value");

📢 throws

Method declaration में exception को indicate करने के लिए:

void readFile() throws IOException {
    
}

📁 Java Packages

Packages classes को organize करने में मदद करते हैं।

package com.example.myapp;

दूसरे package की class import करने के लिए:

import java.util.ArrayList;

📦 Common Java Packages

PackageUse
java.langBasic Java classes
java.utilCollections और utilities
java.ioInput/Output
java.nioModern file/data APIs
java.timeDate और Time
java.netNetworking
java.mathBig number classes

📅 Java Date & Time

Modern Java में date और time के लिए java.time package useful है।

Example:

LocalDate today = LocalDate.now();

System.out.println(today);

Time:

LocalTime time = LocalTime.now();

Date + Time:

LocalDateTime now = LocalDateTime.now();

🔢 Math Class

कुछ useful methods:

Math.max(10, 20);
Math.min(10, 20);
Math.abs(-10);
Math.sqrt(25);
Math.pow(2, 3);

Random number:

double number = Math.random();

🔄 Streams

Java Streams collection data पर operations करने का आसान तरीका देते हैं।

Example:

List<Integer> numbers = List.of(10, 20, 30, 40);

numbers.stream()
       .filter(n -> n > 20)
       .forEach(System.out::println);

Output:

30
40

🔍 Lambda Expression

Lambda expression से छोटा function-like code लिखा जा सकता है।

Example:

(a, b) -> a + b

Runnable example:

Runnable task = () -> {
    System.out.println("Hello");
};

🧵 Java Threads

एक program में अलग-अलग tasks को independently चलाने के लिए threads का use हो सकता है।

Basic example:

Thread thread = new Thread(() -> {
    System.out.println("Task Running");
});

thread.start();

Modern Java applications में concurrency के लिए कई बेहतर APIs और approaches भी available हैं।


📄 File Handling

File पढ़ने के लिए modern Java में Files और Path useful हैं।

Example:

Path path = Path.of("data.txt");

String text = Files.readString(path);

System.out.println(text);

🧪 Java Testing

Java projects में testing के लिए frameworks जैसे JUnit commonly इस्तेमाल किए जाते हैं।

Basic idea:

Write Code
   ↓
Write Test
   ↓
Run Test
   ↓
Find Problem
   ↓
Fix Code

Testing से bugs जल्दी पकड़ने में मदद मिलती है।


🐞 Java Debugging

अगर Java program में problem आ रही है, तो debugger काफी useful है।

आप:

  • Breakpoint लगा सकते हैं

  • Variables देख सकते हैं

  • Program को step-by-step चला सकते हैं

  • Call stack देख सकते हैं

  • Runtime values check कर सकते हैं

Example:

int a = 10;
int b = 20;

int result = a + b;

System.out.println(result);

result वाली line पर breakpoint लगाकर value check की जा सकती है।


💻 Java Compile और Run Commands

अगर Main.java file है:

javac Main.java

यह Java source code compile करेगा।

फिर:

java Main

Program run होगा।


📋 Java Quick Cheat Sheet

VARIABLE
int age = 20;

STRING
String name = "Java";

IF
if (age >= 18) { }

LOOP
for (int i = 0; i < 10; i++) { }

ARRAY
int[] numbers = {1, 2, 3};

METHOD
static int add(int a, int b) {
    return a + b;
}

CLASS
class Student { }

OBJECT
Student s = new Student();

INHERITANCE
class Dog extends Animal { }

INTERFACE
class Car implements Vehicle { }

LIST
ArrayList<String> list = new ArrayList<>();

SET
HashSet<String> set = new HashSet<>();

MAP
HashMap<Integer, String> map = new HashMap<>();

EXCEPTION
try { }
catch (Exception e) { }

STREAM
list.stream();

LAMBDA
x -> x * 2;

📊 Java में सबसे जरूरी Concepts

Topicक्या सीखना है
VariablesData store करना
Data TypesData का type
OperatorsCalculation और comparison
ConditionsDecision लेना
LoopsRepeated work
ArraysMultiple values
StringsText handling
MethodsReusable code
ClassBlueprint
ObjectClass का instance
EncapsulationData को control करना
InheritanceExisting class से features लेना
Polymorphismअलग behavior
AbstractionComplexity hide करना
CollectionsData manage करना
GenericsType safety
ExceptionsErrors handle करना
StreamsCollection data process करना
LambdaShort function-like code

⚠️ Beginners की Common Java Mistakes

1. = और == को confuse करना

x = 10;

Assignment है।

x == 10

Comparison है।

2. Array index भूल जाना

int[] a = {10, 20, 30};

Index:

0 → 10
1 → 20
2 → 30

3. Null को ignore करना

Object null हो सकता है, इसलिए code लिखते समय null cases का ध्यान रखें।

4. बहुत बड़ी class बनाना

सारा code एक ही class में डालने के बजाय code को छोटे methods और classes में divide करना बेहतर रहता है।

5. Exception को बिना समझे पकड़ लेना

ऐसा code:

catch (Exception e) {
}

लिखकर error को छुपा देना अच्छी practice नहीं है।


💡 Pro Tip

Java सीखते समय सिर्फ syntax याद करने के बजाय छोटे programs बनाइए।

जैसे:

Calculator
   ↓
Student Management
   ↓
Bank Account
   ↓
To-Do List
   ↓
File Manager
   ↓
API Based Project

हर project में नए concepts add करते जाएँ।

इससे Java के concepts ज्यादा अच्छे से समझ आएँगे।


⚠️ Note

Java के अलग-अलग versions में कुछ नए features और syntax जुड़ते रहते हैं।

इसलिए किसी पुराने tutorial में दिखाई गई चीज आपके current JDK या IDE में थोड़ी अलग हो सकती है।

साथ ही, production project में code लिखते समय official Java documentation और project की जरूरत के हिसाब से सही API और approach चुनना जरूरी है।


❓ Java FAQs

Java क्या है?

Java एक popular object-oriented programming language है जिसका इस्तेमाल कई तरह के software और applications बनाने में होता है।

क्या Java beginners के लिए अच्छा है?

हाँ। शुरुआत में concepts थोड़े ज्यादा लग सकते हैं, लेकिन basic syntax समझ आने के बाद Java को step-by-step सीखा जा सकता है।

Java में OOP क्या है?

OOP यानी Object-Oriented Programming। इसमें class, object, inheritance, encapsulation, polymorphism और abstraction जैसे concepts आते हैं।

Java में Array और ArrayList में क्या difference है?

Array का size बनने के बाद fixed रहता है, जबकि ArrayList का size जरूरत के हिसाब से बढ़ या घट सकता है।

HashMap किस काम आता है?

जब data को key-value form में रखना हो, तब HashMap useful होता है।

HashSet क्या करता है?

HashSet unique values का collection रखने के लिए useful है।

Java में method क्या है?

Method code के किसी particular काम को एक नाम देता है, जिसे जरूरत पड़ने पर बार-बार call किया जा सकता है।

Java में constructor क्या है?

Constructor object बनाते समय call होने वाला special member है, जिसका इस्तेमाल object की initial values set करने के लिए किया जा सकता है।

Java में exception क्या है?

Program run करते समय आने वाली ऐसी problem जिसे program handle कर सकता है, उसे exception के रूप में handle किया जाता है।

Java में JVM क्या है?

JVM यानी Java Virtual Machine, Java bytecode को run करने वाला runtime environment है।

Java सीखने के बाद क्या बना सकते हैं?

Java सीखने के बाद आप desktop software, backend applications, enterprise software, APIs और कई दूसरे types के projects पर काम कर सकते हैं।


🚀 Final Revision

अगर आप Java को beginner से अच्छे level तक सीखना चाहते हैं, तो यह order follow करना आसान रहेगा:

Java Basics
     ↓
Variables & Data Types
     ↓
Conditions & Loops
     ↓
Methods
     ↓
Arrays & Strings
     ↓
Classes & Objects
     ↓
OOP
     ↓
Collections
     ↓
Generics
     ↓
Exception Handling
     ↓
Streams & Lambda
     ↓
File Handling
     ↓
Multithreading
     ↓
Testing & Debugging
     ↓
Real Projects

Java में शुरुआत में बहुत सारे concepts दिखाई देते हैं, लेकिन एक साथ सब सीखने की जरूरत नहीं है।

पहले variables, conditions, loops और methods को अच्छे से समझिए। उसके बाद class, object और OOP पर जाइए। फिर Collections, Exceptions और Streams सीखना शुरू करें।

सबसे जरूरी बात है—code खुद लिखिए।

किसी example को सिर्फ पढ़ने के बजाय उसे अपने computer पर type करके run करें। Error आए तो घबराने की जरूरत नहीं है। Error को पढ़ना और उसे fix करना भी programming सीखने का ही हिस्सा है।

Learn → Code → Practice → Debug → Build 🚀


📥 Download

🎬 Need help? Click the button below to watch the complete step-by-step video guide tutorial!

📺 Watch Full Video Guide

एक टिप्पणी भेजें

Have a question or feedback? Share it below! Please avoid spam and stay respectful.
Visit Website
Visit Website