Java Programming Notes (हिंदी + English)

Complete study material for degree & diploma students – with examples in both languages

Topics of Java Programming

1. Introduction to Java / Java का परिचय

English

What is Java? Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. It follows the principle "Write Once, Run Anywhere" (WORA), meaning compiled Java code can run on any platform that supports Java without recompilation.

Key Features:

  • Simple, secure, and robust
  • Platform independent (bytecode + JVM)
  • Object-oriented (supports encapsulation, inheritance, polymorphism)
  • Multithreaded
  • Automatic memory management (garbage collection)

Basic Structure of a Java Program:

  • Package declaration (optional)
  • Import statements
  • Class definition (public class name must match filename)
  • main() method – entry point

हिंदी

Java क्या है? Java एक हाई-लेवल, ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग भाषा है जिसे 1995 में Sun Microsystems (अब Oracle) ने बनाया था। यह "Write Once, Run Anywhere" (एक बार लिखो, कहीं भी चलाओ) के सिद्धांत पर काम करती है – मतलब Java का बना हुआ कोड बिना बदलाव के किसी भी प्लेटफॉर्म पर चल सकता है।

मुख्य विशेषताएँ:

  • सीखने और इस्तेमाल में आसान, सुरक्षित और मजबूत
  • प्लेटफॉर्म इंडिपेंडेंट (बाइटकोड और JVM की मदद से)
  • ऑब्जेक्ट-ओरिएंटेड (एनकैप्सुलेशन, इनहेरिटेंस, पॉलीमोर्फिज्म सपोर्ट करता है)
  • मल्टीथ्रेडिंग की सुविधा
  • ऑटोमैटिक मेमोरी मैनेजमेंट (गार्बेज कलेक्शन)

Java प्रोग्राम की बेसिक संरचना:

  • पैकेज डिक्लेरेशन (ऑप्शनल)
  • इम्पोर्ट स्टेटमेंट
  • क्लास डेफिनिशन (public class का नाम फ़ाइल नाम से मेल खाना चाहिए)
  • main() मेथड – यहीं से प्रोग्राम शुरू होता है
// My first Java program
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Explanation: public class HelloWorld defines a class. public static void main(String[] args) is the entry point. System.out.println() prints output.

व्याख्या: public class HelloWorld एक क्लास को डिफाइन करता है। public static void main(String[] args) प्रोग्राम का एंट्री पॉइंट है। System.out.println() आउटपुट प्रिंट करता है।

↑ Back to Top

2. Basic Syntax / मूल वाक्यविन्यास

English

Basic Syntax Rules:

  • Every statement ends with a semicolon (;).
  • Comments: // single line or /* multi line */.
  • Blocks are enclosed in curly braces { }.
  • Java is case‑sensitive: myVar and myvar are different.
  • Class names should start with an uppercase letter (convention).
  • Method and variable names start with a lowercase letter (camelCase).

Identifiers: Names given to classes, methods, variables. Must begin with a letter, underscore, or dollar sign; cannot use keywords.

हिंदी

बेसिक सिंटैक्स के नियम:

  • हर स्टेटमेंट के बाद सेमीकोलन (;) लगाना जरूरी है।
  • कमेंट्स: // एक लाइन वाला या /* कई लाइनों वाला */
  • ब्लॉक घुंघराले ब्रेसेस { } के अंदर होते हैं।
  • Java केस‑सेंसिटिव है – myVar और myvar अलग-अलग हैं।
  • क्लास का नाम बड़े अक्षर (uppercase) से शुरू करना चाहिए (आम तौर पर)।
  • मेथड और वेरिएबल का नाम छोटे अक्षर (lowercase) से शुरू होता है (camelCase)।

आइडेंटिफायर्स: क्लास, मेथड, वेरिएबल को दिए गए नाम। नाम अक्षर, अंडरस्कोर या डॉलर चिह्न से शुरू हो सकता है; कीवर्ड नहीं हो सकते।

// Syntax example
public class SyntaxDemo {
    public static void main(String[] args) {
        int number = 10; // variable declaration
        System.out.println("Number is: " + number);
    }
}

Explanation: The program declares an integer variable and prints it. Notice the semicolons and braces.

व्याख्या: इस प्रोग्राम में एक इंटिजर वेरिएबल डिक्लेयर किया गया है और उसकी वैल्यू प्रिंट की गई है। सेमीकोलन और ब्रेसेस पर ध्यान दें।

↑ Back to Top

3. Data Types / डेटा प्रकार

English

Java has two categories of data types:

1. Primitive Data Types (8 types):

  • byte (1 byte) – -128 to 127
  • short (2 bytes)
  • int (4 bytes) – most common integer type
  • long (8 bytes) – suffix L or l
  • float (4 bytes) – suffix F or f
  • double (8 bytes) – default for decimals
  • char (2 bytes) – single Unicode character
  • boolean (1 bit) – true/false

2. Non-Primitive (Reference) Types: classes, interfaces, arrays, enums.

हिंदी

Java में डेटा टाइप दो तरह के होते हैं:

1. प्रिमिटिव डेटा टाइप (8 प्रकार):

  • byte (1 बाइट) – -128 से 127 तक
  • short (2 बाइट्स)
  • int (4 बाइट्स) – सबसे ज्यादा इस्तेमाल होने वाला इंटिजर टाइप
  • long (8 बाइट्स) – अंत में L या l लगाते हैं
  • float (4 बाइट्स) – अंत में F या f लगाते हैं
  • double (8 बाइट्स) – दशमलव के लिए डिफॉल्ट टाइप
  • char (2 बाइट्स) – एक अक्षर (Unicode)
  • boolean (1 बिट) – true या false

2. नॉन-प्रिमिटिव (रेफरेंस) टाइप: क्लास, इंटरफ़ेस, ऐरे, एनम।

// Data types example
public class DataTypesDemo {
    public static void main(String[] args) {
        byte b = 100;
        short s = 5000;
        int i = 100000;
        long l = 100000L;
        float f = 5.75f;
        double d = 19.99;
        char c = 'A';
        boolean bool = true;

        System.out.println("byte: " + b);
        System.out.println("short: " + s);
        System.out.println("int: " + i);
        System.out.println("long: " + l);
        System.out.println("float: " + f);
        System.out.println("double: " + d);
        System.out.println("char: " + c);
        System.out.println("boolean: " + bool);
    }
}

Explanation: Each variable is declared with its data type. Note the suffixes for long and float.

व्याख्या: हर वेरिएबल को उसके डेटा टाइप के साथ डिक्लेयर किया गया है। long और float के अंत में L/l और F/f लगाना जरूरी है।

↑ Back to Top

4. Variables & Constants / चर और स्थिरांक

English

Variables: A variable is a container that holds a value. It must be declared with a type and a name. Variables can be local (inside a method), instance (inside a class but outside methods), or static (class variables).

Constants: In Java, constants are declared using the final keyword. A final variable cannot be reassigned. Convention: constant names are in uppercase with underscores.

final int DAYS_IN_WEEK = 7;

हिंदी

वेरिएबल (Variables): वेरिएबल एक कंटेनर होता है जिसमें वैल्यू स्टोर होती है। इसे एक टाइप और नाम के साथ डिक्लेयर करना पड़ता है। वेरिएबल तीन तरह के हो सकते हैं: लोकल (मेथड के अंदर), इंस्टेंस (क्लास के अंदर लेकिन मेथड के बाहर), या स्टेटिक (क्लास वेरिएबल)।

कॉन्स्टैंट्स (Constants): Java में कॉन्स्टैंट बनाने के लिए final कीवर्ड का इस्तेमाल करते हैं। final वेरिएबल की वैल्यू एक बार सेट करने के बाद बदली नहीं जा सकती। नामकरण की परंपरा: सभी अक्षर बड़े (uppercase) और शब्दों के बीच अंडरस्कोर।

final int DAYS_IN_WEEK = 7;

// Variables and constants
public class VarConstDemo {
    static int staticVar = 10;      // static variable
    int instanceVar = 20;           // instance variable
    final double PI = 3.14159;      // constant

    public static void main(String[] args) {
        int localVar = 30;           // local variable
        System.out.println("Local: " + localVar);
        System.out.println("Static: " + staticVar);

        VarConstDemo obj = new VarConstDemo();
        System.out.println("Instance: " + obj.instanceVar);
        System.out.println("Constant PI: " + obj.PI);
    }
}

Explanation: The program demonstrates local, instance, and static variables, as well as a final constant.

व्याख्या: इस प्रोग्राम में लोकल, इंस्टेंस और स्टेटिक वेरिएबल दिखाए गए हैं, साथ ही एक final कॉन्स्टैंट भी।

↑ Back to Top

5. Input & Output / इनपुट और आउटपुट

English

Output: System.out.println() prints a line; System.out.print() prints without newline.

Input: Java provides the Scanner class (in java.util) for reading input. Other classes: BufferedReader, Console.

हिंदी

आउटपुट: System.out.println() से एक लाइन प्रिंट होती है; System.out.print() से बिना नई लाइन के प्रिंट होता है।

इनपुट: Java में इनपुट लेने के लिए Scanner क्लास (java.util पैकेज में) सबसे आसान तरीका है। अन्य क्लास: BufferedReader, Console

// Input and output using Scanner
import java.util.Scanner;

public class IODemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Hello " + name + ", you are " + age + " years old.");
        sc.close();
    }
}

Explanation: Scanner is used to read input. nextLine() reads a string, nextInt() reads an integer. Always close the scanner.

व्याख्या: Scanner से इनपुट लिया जाता है। nextLine() स्ट्रिंग पढ़ता है, nextInt() इंटिजर पढ़ता है। स्कैनर को बंद करना (close) अच्छी आदत है।

↑ Back to Top

6. Operators / ऑपरेटर

English

Java provides rich set of operators:

  • Arithmetic: + - * / %
  • Relational: == != < > <= >=
  • Logical: && || !
  • Assignment: = += -= *= /= %=
  • Increment/Decrement: ++ --
  • Bitwise: & | ^ ~ << >>
  • Ternary: ? :
  • instanceof – type comparison

हिंदी

Java में कई तरह के ऑपरेटर होते हैं:

  • अरिथमेटिक: + - * / %
  • रिलेशनल: == != < > <= >=
  • लॉजिकल: && || !
  • असाइनमेंट: = += -= *= /= %=
  • इन्क्रीमेंट/डिक्रीमेंट: ++ --
  • बिटवाइज: & | ^ ~ << >>
  • टर्नरी (शर्त ऑपरेटर): ? :
  • instanceof – यह चेक करता है कि कोई ऑब्जेक्ट किसी खास टाइप का है या नहीं
// Operators example
public class OperatorsDemo {
    public static void main(String[] args) {
        int a = 10, b = 3;
        System.out.println("a + b = " + (a + b));
        System.out.println("a > b = " + (a > b));
        System.out.println("(a > 5) && (b < 5) = " + ((a > 5) && (b < 5)));
        a += 5;  // a = a + 5
        System.out.println("a after += 5: " + a);
        int max = (a > b) ? a : b;
        System.out.println("Max using ternary: " + max);
    }
}
↑ Back to Top

7. Control Structures / नियंत्रण संरचनाएँ

English

Java supports if, else if, else, and switch statements.

हिंदी

Java में if, else if, else, और switch स्टेटमेंट होते हैं, जिनसे कंडीशन के आधार पर अलग-अलग कोड चलाया जा सकता है।

// Control structures
public class ControlDemo {
    public static void main(String[] args) {
        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");
        }

        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("Other day");
        }
    }
}
↑ Back to Top

8. Loops / लूप्स

English

Java provides while, do-while, for, and enhanced for-each loops.

हिंदी

Java में while, do-while, for, और एन्हांस्ड for-each लूप होते हैं। लूप का मतलब होता है किसी कोड को बार-बार चलाना जब तक कंडीशन सही रहे।

// Loops example
public class LoopsDemo {
    public static void main(String[] args) {
        // for loop
        for (int i = 1; i <= 5; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        // while loop
        int j = 1;
        while (j <= 5) {
            System.out.print(j + " ");
            j++;
        }
        System.out.println();

        // for-each loop on array
        int[] arr = {10, 20, 30, 40};
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}
↑ Back to Top

9. Arrays / ऐरे

English

An array is a container that holds a fixed number of values of a single type. Index starts from 0.

Declaration: int[] arr = new int[5]; or int[] arr = {1,2,3};

हिंदी

ऐरे एक कंटेनर है जिसमें एक ही टाइप के कई वैल्यू (फिक्स्ड नंबर) स्टोर किए जा सकते हैं। इंडेक्स 0 से शुरू होता है।

डिक्लेरेशन: int[] arr = new int[5]; या int[] arr = {1,2,3};

// Arrays example
public class ArraysDemo {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};

        // access and print
        System.out.println("First element: " + numbers[0]);

        // loop through
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.println();

        // 2D array
        int[][] matrix = { {1,2}, {3,4} };
        System.out.println("Element at [1][0]: " + matrix[1][0]);
    }
}
↑ Back to Top

10. Strings / स्ट्रिंग्स

English

Strings are objects in Java. They are immutable. Common methods: length(), charAt(), substring(), equals(), compareTo(), toUpperCase(), etc.

हिंदी

Java में Strings ऑब्जेक्ट होते हैं। ये अपरिवर्तनीय (immutable) होते हैं – एक बार बन गए तो बदले नहीं जा सकते। कुछ आम मेथड: length() (लंबाई), charAt() (किसी पोजीशन का कैरेक्टर), substring() (सबस्ट्रिंग निकालना), equals() (तुलना), toUpperCase() (बड़े अक्षर में बदलना) आदि।

// String example
public class StringDemo {
    public static void main(String[] args) {
        String str = "Hello Java";

        System.out.println("Length: " + str.length());
        System.out.println("Char at 0: " + str.charAt(0));
        System.out.println("Substring (6): " + str.substring(6));
        System.out.println("Uppercase: " + str.toUpperCase());

        String str2 = "hello java";
        System.out.println("Equals (ignore case): " + str.equalsIgnoreCase(str2));
    }
}
↑ Back to Top

11. Methods / विधियाँ

English

Methods are blocks of code that perform a specific task. They can return a value or be void. Methods can be overloaded (same name, different parameters).

हिंदी

मेथड कोड का एक ब्लॉक होता है जो कोई खास काम करता है। वह कोई वैल्यू लौटा सकता है या void (कुछ नहीं लौटाता) हो सकता है। मेथड को ओवरलोड किया जा सकता है – मतलब एक ही नाम से अलग-अलग पैरामीटर वाले मेथड बना सकते हैं।

// Methods example
public class MethodsDemo {
    static int add(int a, int b) {
        return a + b;
    }

    static double add(double a, double b) { // overloaded
        return a + b;
    }

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

    public static void main(String[] args) {
        int sum = add(5, 3);
        System.out.println("Sum (int): " + sum);
        System.out.println("Sum (double): " + add(5.5, 2.3));
        greet("Alice");
    }
}
↑ Back to Top

12. Object-Oriented Programming (Classes & Objects) / ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (क्लास और ऑब्जेक्ट)

English

A class is a blueprint for creating objects. An object is an instance of a class. It has state (fields) and behavior (methods).

Concepts: encapsulation, inheritance, polymorphism, abstraction.

हिंदी

क्लास एक ब्लूप्रिंट की तरह होती है, जिससे ऑब्जेक्ट बनाए जाते हैं। ऑब्जेक्ट क्लास का एक उदाहरण (instance) होता है। उसमें स्टेट (फील्ड) और बिहेवियर (मेथड) होते हैं।

OOP के मुख्य कॉन्सेप्ट: एनकैप्सुलेशन (डेटा छुपाना), इनहेरिटेंस (विरासत), पॉलीमोर्फिज्म (बहुरूपता), एब्स्ट्रैक्शन (सिर्फ जरूरी चीजें दिखाना)।

// Class and object example
class Student {
    String name;
    int age;

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class OOPDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Rahul";
        s1.age = 20;
        s1.display();
    }
}
↑ Back to Top

13. Inheritance & Polymorphism / इनहेरिटेंस और पॉलीमोर्फिज्म

English

Inheritance: One class acquires properties of another using extends. Supports single inheritance.

Polymorphism: Same method name behaves differently based on object (method overriding).

हिंदी

इनहेरिटेंस (विरासत): एक क्लास दूसरी क्लास के गुणों को extends कीवर्ड से प्राप्त कर सकती है। Java में सिर्फ एक ही क्लास से इनहेरिट कर सकते हैं (single inheritance)।

पॉलीमोर्फिज्म (बहुरूपता): एक ही मेथड का नाम, लेकिन अलग-अलग ऑब्जेक्ट के साथ अलग व्यवहार। यह मेथड ओवरराइडिंग से होता है।

// Inheritance and polymorphism
class Animal {
    void sound() {
        System.out.println("Animal makes sound");
    }
}

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

public class InheritanceDemo {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.sound();  // Dog barks (polymorphism)
    }
}
↑ Back to Top

14. Exception Handling / अपवाद प्रबंधन

English

Exceptions are runtime errors. Java provides try-catch-finally blocks to handle them. Use throw to explicitly throw an exception, and throws to declare them.

हिंदी

एक्सेप्शन वो एरर हैं जो प्रोग्राम चलते समय आती हैं (runtime errors)। Java में इन्हें हैंडल करने के लिए try-catch-finally ब्लॉक होते हैं। throw से खुद एक्सेप्शन फेंक सकते हैं, और throws से मेथड के बारे में बता सकते हैं कि वह एक्सेप्शन फेंक सकता है।

// Exception handling
public class ExceptionDemo {
    public static void main(String[] args) {
        try {
            int[] arr = new int[5];
            arr[10] = 100;  // ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Caught: " + e);
        } finally {
            System.out.println("Finally block always executes");
        }
    }
}
↑ Back to Top

15. File Handling / फ़ाइल हैंडलिंग

English

Java provides the java.io and java.nio packages for file operations. Common classes: File, FileReader, FileWriter, BufferedReader, BufferedWriter.

हिंदी

Java में फ़ाइल ऑपरेशन के लिए java.io और java.nio पैकेज हैं। आम इस्तेमाल होने वाली क्लास: File, FileReader, FileWriter, BufferedReader, BufferedWriter

// File writing and reading
import java.io.*;

public class FileDemo {
    public static void main(String[] args) {
        // Write to file
        try (FileWriter fw = new FileWriter("test.txt")) {
            fw.write("Hello, Java File!\n");
            fw.write("This is a test.");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Read from file
        try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
↑ Back to Top