Tuesday, July 29, 2025

Displaying Numeric Values in Applet एप्लेट में संख्यात्मक मान प्रदर्शित करना

In Java applets, numeric values are displayed using the drawString() method from the Graphics class.

जावा एप्लेट्स में संख्यात्मक मानों को ग्राफ़िक्स क्लास की drawString() मेथड का उपयोग करके प्रदर्शित किया जाता है।

To do this, the numeric value must first be converted into a string using the String.valueOf() method.
इस कार्य को करने के लिए, संख्यात्मक मान को पहले String.valueOf() मेथड की सहायता से स्ट्रिंग में बदला जाता है।

🧪 Example: Displaying sum of two numbers  दो संख्याओं का योग दिखाना

// Java File: temp.java  
import java.awt.*;  
import java.applet.*;  

public class temp extends Applet {  
    public void paint(Graphics g) {  
        int x = 10;  
        int y = 25;  
        int add = x + y;  
        String s = "add: " + String.valueOf(add);  
        g.drawString(s, 100, 100);  
    }  
}

🖥️ HTML File to load applet एप्लेट लोड करने हेतु HTML फाइल:

<html>
    <body>
        <applet code="temp.class" width="600" height="400"></applet>
    </body>
</html>

Java Applet Parameter Passing जावा एप्लेट में पैरामीटर पास करना

Java Applets allow you to interact with HTML and pass user inputs directly using the <param> tag.

जावा एप्लेट HTML के साथ इंटरैक्ट कर सकता है और <param> टैग का उपयोग करके यूजर इनपुट को सीधे पास किया जा सकता है।

This is useful when you need to input values from forms or user interactions in a webpage.
यह तब उपयोगी होता है जब आपको वेबपेज में फॉर्म या यूजर इंटरैक्शन से इनपुट प्राप्त करना होता है।


🔷 Passing Parameters in Java Applet जावा एप्लेट में पैरामीटर पास करना

The <param> tag is used within <applet> to pass values to the Java program.
जावा प्रोग्राम को वैल्यू पास करने के लिए <applet> के अंदर <param> टैग का उपयोग किया जाता है।

✨ Attributes of <param>

<param> टैग के ऐट्रिब्यूट्स

  • name: The parameter name.
    name: वह नाम जिसे पैरामीटर के रूप में पास किया जाएगा।

  • value: The value of the parameter.
    value: वह वैल्यू जो name एट्रिब्यूट के अंतर्गत पास की जाएगी।

✅ Syntax: प्रारूप:

<param name="parameterName" value="parameterValue">

🔷 Accessing Parameters using getParameter() 

getParameter() के माध्यम से पैरामीटर एक्सेस करना

This method retrieves the value of the parameter inside the applet.
यह मेथड एप्लेट में पास किए गए पैरामीटर की वैल्यू प्राप्त करता है।

✅ Syntax: प्रारूप:

String value = getParameter("parameterName");

🧪 Example Program - Parameter Passing

✅ HTML File

<html>
<head><title>Applet Parameter Example</title></head>
<body>
<applet code="UserApplet.class" width="300" height="300">
  <param name="userName" value="Ashwani Verma">
</applet>
</body>
</html>

✅ Java File

import java.applet.*;
import java.awt.*;

public class UserApplet extends Applet {
    String username;

    public void start() {
        username = getParameter("userName");
    }

    public void paint(Graphics g) {
        g.drawString("Welcome, " + username, 30, 30);
    }
}

Output: The applet will display – “Welcome, Ashwani Verma”


📌 Final Notes अंतिम टिप्पणियाँ

  • Always use getParameter() carefully to avoid NullPointerException.
    getParameter() का उपयोग सावधानीपूर्वक करें जिससे NullPointerException न हो।

  • Use default values when parameters are not passed.
    जब पैरामीटर पास न हो, तब डिफ़ॉल्ट वैल्यू का प्रयोग करें।

  • Applets are deprecated in modern browsers, so prefer JavaFX or web-based Java in new projects.
    एप्लेट्स अब आधुनिक ब्राउज़र में deprecated हैं, इसलिए नए प्रोजेक्ट्स में JavaFX या web-based Java का उपयोग करें।

☕ जावा एप्लेट क्या है? What is Java Applet?

Java Applet एक छोटा Java प्रोग्राम होता है जो ब्राउज़र पर रन करता है। इसे HTML डॉक्यूमेंट में <applet> टैग द्वारा जोड़ा जाता है।

Java एप्लेट का कोड .class फाइल में सेव होता है और जब यूजर HTML फाइल खोलता है, तो यह कोड क्लाइंट ब्राउज़र पर लोड होता है।


🔁 Java Applet का जीवन चक्र (Applet Life Cycle)

मेथड उद्देश्य (Purpose)

init() प्रारंभिक सेटअप, जैसे बैकग्राउंड कलर

start() एप्लेट का सक्रिय होना

stop() जब यूजर पेज छोड़ता है, एप्लेट रुक जाता है

paint(Graphics g) आउटपुट को स्क्रीन पर प्रदर्शित करना

destroy() ब्राउज़र बंद होने पर एप्लेट को समाप्त करना

🔧 उदाहरण (Examples)

उदाहरण 1: Basic Applet

Java File: photo.java


import java.applet.Applet;

import java.awt.*;


public class photo extends Applet {

    public void init() {

        setBackground(Color.red);

    }

    public void paint(Graphics g) {

        g.drawString("Applet is running", 30, 30);

    }

}

HTML File:


<!DOCTYPE html>

<html>

<head><title>applet example</title></head>

<body>

<applet code="photo.class" width="300" height="300"></applet>

</body>

</html>

⚠️ नोट:

<applet> टैग HTML5 में सपोर्ट नहीं करता। इसके स्थान पर <object> या <embed> का प्रयोग करें।


उदाहरण के लिए:


<object code="photo.class" width="300" height="300">

Your browser does not support applets.

</object>

📢 निष्कर्ष (Conclusion)

HTML और Java Applet का संयोजन उपयोगकर्ता को वेबपेज पर डायनामिक और इंटरैक्टिव कंटेंट प्रस्तुत करने की शक्ति देता है। हालांकि <applet> टैग अब पुराना हो चुका है, लेकिन यह अभी भी जावा सीखने वाले विद्यार्थियों के लिए उपयोगी तकनीक है।


Introduction to HTML, HTML का परिचय | What is HTML ? HTML क्या है?

HTML (HyperText Markup Language) is the standard markup language used to create web pages. It is platform-independent, tag-based, and case-insensitive.

HTML (हाइपरटेक्स्ट मार्कअप लैंग्वेज) एक मानक मार्कअप भाषा है जिसका उपयोग वेब पेज बनाने के लिए किया जाता है। यह प्लेटफॉर्म-इंडिपेंडेंट, टैग-आधारित और केस-इनसेंसिटिव होती है।

HTML allows us to design structured documents using tags and elements. Each tag tells the browser how to display the content.

HTML हमें टैग्स और एलिमेंट्स की सहायता से संरचित डॉक्यूमेंट डिज़ाइन करने की अनुमति देता है। प्रत्येक टैग ब्राउज़र को यह बताता है कि सामग्री को कैसे प्रदर्शित किया जाए।

# 🧱 Basic Structure of an HTML Document | HTML डॉक्यूमेंट की संरचना

```html
<!DOCTYPE html>
<html>
<head>
  <title>Title of Page</title>
</head>
<body>
  <h1>Main Heading</h1>
  <p>This is a paragraph.</p>
</body>
</html>
```

This is a simple HTML page structure that includes a header and body content.
यह एक साधारण HTML पेज की संरचना है जिसमें हेडर और बॉडी कंटेंट होता है।


# 🏷️ Common HTML Tags | सामान्य HTML टैग्स

| Tag        | Purpose (उद्देश्य)                               |
| ---------- | ------------------------------------------------ |
| `<html>`   | HTML डॉक्यूमेंट की शुरुआत और अंत करता है         |
| `<head>`   | मेटा जानकारी, स्क्रिप्ट और टाइटल के लिए          |
| `<title>`  | ब्राउज़र टैब में वेबपेज का शीर्षक दिखाता है      |
| `<body>`   | मुख्य दृश्य सामग्री को परिभाषित करता है          |
| `<applet>` | Java Applet जोड़ने के लिए (HTML5 में deprecated) |


# ✍️ Formatting Tags | फॉर्मेटिंग टैग्स

| Tag        | Description (विवरण)                        |
| ---------- | ------------------------------------------ |
| `<i>`      | Italic text (इटैलिक टेक्स्ट)               |
| `<b>`      | Bold text (बोल्ड टेक्स्ट)                  |
| `<u>`      | Underlined text (रेखांकित टेक्स्ट)         |
| `<mark>`   | Highlighted text (हाइलाइटेड टेक्स्ट)       |
| `<sub>`    | Subscript text (सबस्क्रिप्ट टेक्स्ट)       |
| `<sup>`    | Superscript text (सुपरस्क्रिप्ट टेक्स्ट)   |
| `<strong>` | Emphasized bold text (प्रबल बोल्ड टेक्स्ट) |
| `<small>`  | Smaller text (छोटा टेक्स्ट)                |
| `<ins>`    | Inserted text (नया जोड़ा गया टेक्स्ट)      |

---

# 📝 Form Tags | फॉर्म टैग्स

HTML forms are used to collect user input.
HTML फॉर्म का उपयोग यूज़र से इनपुट प्राप्त करने के लिए किया जाता है।

| Tag          | Purpose (उद्देश्य)                               |
| ------------ | ------------------------------------------------ |
| `<form>`     | HTML फॉर्म तैयार करता है                         |
| `<input>`    | टेक्स्ट बॉक्स, चेकबॉक्स, रेडियो बटन, पासवर्ड आदि |
| `<textarea>` | मल्टीलाइन इनपुट हेतु                             |
| `<select>`   | ड्रॉपडाउन लिस्ट के लिए                           |
| `<option>`   | ड्रॉपडाउन में विकल्प जोड़ने हेतु                 |
| `<button>`   | बटन बनाने के लिए                                 |
| `<label>`    | किसी इनपुट के लिए टेक्स्ट लेबल दर्शाता है        |

### 📌 Sample Form Code (फॉर्म कोड का उदाहरण):

```html
<form action="thankyou.html" method="post">
  Name: <input type="text" name="name"><br>
  Password: <input type="password" name="pass"><br>
  Gender:
  <input type="radio" name="gender" value="male"> Male
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="submit" value="Submit">
</form>
```

---

# 📦 Frame Tags | फ्रेम टैग्स

Used to divide web pages into multiple sections or windows.
वेब पेज को कई हिस्सों में विभाजित करने के लिए फ्रेम टैग्स का प्रयोग किया जाता है।

| Tag          | Description (विवरण)                                        |
| ------------ | ---------------------------------------------------------- |
| `<frame>`    | एक फ्रेम परिभाषित करता है                                  |
| `<frameset>` | फ्रेम के सेट को परिभाषित करता है                           |
| `<noframes>` | फ्रेम न सपोर्ट करने वाले ब्राउज़रों के लिए वैकल्पिक कंटेंट |
| `<iframe>`   | इनलाइन फ्रेम तैयार करता है                                 |

---

# 🖼️ Image Tags | इमेज टैग्स

| Tag      | Purpose (उद्देश्य)                        |
| -------- | ----------------------------------------- |
| `<img>`  | वेबपेज पर इमेज शामिल करता है              |
| `<map>`  | इमेज मैप को परिभाषित करता है              |
| `<area>` | इमेज मैप के क्षेत्रों को परिभाषित करता है |

```html
<img src="winter.jpg" alt="Winter Image" width="300">
```

---

# 🔗 Link Tags | लिंक टैग्स

Used to create hyperlinks.
वेब पेज में एक से दूसरे पेज पर जाने के लिए लिंक टैग्स का प्रयोग होता है।

| Tag      | Description (विवरण)                  |
| -------- | ------------------------------------ |
| `<a>`    | हाइपरलिंक बनाने के लिए               |
| `<link>` | CSS जैसी बाहरी फाइल्स को जोड़ने हेतु |

```html
<a href="about.html">About Us</a>
```

---

# 📋 List Tags | लिस्ट टैग्स

| Tag    | Purpose (उद्देश्य)               |
| ------ | -------------------------------- |
| `<ul>` | अनऑर्डर्ड लिस्ट (unordered list) |
| `<ol>` | ऑर्डर्ड लिस्ट (ordered list)     |
| `<li>` | लिस्ट आइटम                       |
| `<dl>` | डेफिनिशन लिस्ट                   |
| `<dt>` | डेफिनिशन टर्म (शब्द)             |
| `<dd>` | डेफिनिशन विवरण                   |

---

# 📊 Table Tags | टेबल टैग्स

| Tag         | Purpose (उद्देश्य)                |
| ----------- | --------------------------------- |
| `<table>`   | टेबल बनाता है                     |
| `<tr>`      | टेबल की एक रो बनाता है            |
| `<th>`      | टेबल हैडर सेल तैयार करता है       |
| `<td>`      | टेबल डाटा सेल तैयार करता है       |
| `<caption>` | टेबल का शीर्षक (कैप्शन) जोड़ता है |

```html
<table border="1">
  <caption>Student Data</caption>
  <tr><th>Name</th><th>Class</th></tr>
  <tr><td>Ravi</td><td>10th</td></tr>
</table>
```

---

# 🎨 Style Tags | स्टाइल टैग्स

CSS can be applied using the `<style>` tag within HTML documents.
HTML डॉक्यूमेंट में स्टाइल लागू करने के लिए `<style>` टैग का प्रयोग होता है।

```html
<style>
p { color: red; font-size: 20px; }
</style>
```

---

# 📚 Section Tags | सेक्शन टैग्स

| Tag         | Purpose (उद्देश्य)                      |
| ----------- | --------------------------------------- |
| `<div>`     | ब्लॉक लेवल सेक्शन बनाता है              |
| `<span>`    | इनलाइन लेवल कंटेनर बनाता है             |
| `<section>` | डॉक्यूमेंट के विभागों को दर्शाने के लिए |

---

# 📜 Scripting Tags | स्क्रिप्टिंग टैग्स

Used for embedding scripts like JavaScript.
HTML में JavaScript जैसी स्क्रिप्ट्स जोड़ने हेतु टैग्स का प्रयोग होता है।

```html
<script>
  alert("Welcome to HTML World!");
</script>
<noscript>
  Your browser does not support JavaScript.
</noscript>
```

---

# 🚀 Conclusion | निष्कर्ष

HTML is the foundational language for web development. With HTML5, we now have richer media support, semantic tags, and better integration with JavaScript and CSS.
HTML वेब विकास की आधारशिला है। HTML5 के साथ अब हमें बेहतर मीडिया सपोर्ट, सेमान्टिक टैग्स और JavaScript व CSS के साथ बेहतर समन्वय मिलता है।

HTML अकेले नहीं, बल्कि CSS और JavaScript के साथ मिलकर एक पूर्ण वेबसाइट तैयार करता है।

Exception Handling in Java एक्सेप्शन हैंडलिंग

Exception Handling is an effective mechanism to handle runtime errors that may occur while executing a Java program.

एक्सेप्शन हैंडलिंग एक प्रभावी तकनीक है जो जावा प्रोग्राम को रन करते समय उत्पन्न होने वाली रनटाइम एरर को नियंत्रित करती है।

It ensures that the program flow remains normal even after unexpected errors.
यह सुनिश्चित करता है कि अप्रत्याशित त्रुटियों के बावजूद प्रोग्राम का सामान्य प्रवाह बना रहे।

It also helps provide user-friendly messages instead of abrupt program crashes.
यह उपयोगकर्ता को अचानक क्रैश की बजाय फ्रेंडली मैसेज देता है।

✅ Why Exception Handling is Important? एक्सेप्शन हैंडलिंग क्यों ज़रूरी है?

  • Prevents abrupt termination of programs प्रोग्राम को अचानक बंद होने से रोकता है

  • Helps identify bugs at runtime रनटाइम पर बग की पहचान में मदद करता है

  • Allows use of custom exception messages कस्टम एक्सेप्शन मैसेज का उपयोग संभव बनाता है

  • Enables graceful error recovery त्रुटियों से सरल पुनर्प्राप्ति को संभव बनाता है


📌 Basic Syntax बेसिक सिंटैक्स

try {
// Code that may throw exception ऐसा कोड जो एक्सेप्शन उत्पन्न कर सकता है
} catch (ExceptionType e) {
// Exception handler एक्सेप्शन को हैंडल करने वाला कोड
} finally {
// Cleanup code (optional) फाइनल क्लीनअप कोड (वैकल्पिक)
}


⚙️ Java Exception Class Hierarchy जावा एक्सेप्शन क्लास हाइरार्की

  • All exceptions in Java are derived from Throwable class. जावा में सभी एक्सेप्शन Throwable क्लास से प्राप्त होते हैं।

Throwable
/ \
Exception Error
/ \ \
Checked Unchecked JVM/System Errors


Checked Exceptions: Must be handled during compilation (e.g. IOException, SQLException)
चेक्ड एक्सेप्शन: इन्हें कंपाइल करते समय संभालना ज़रूरी होता है (जैसे IOException, SQLException)

Unchecked Exceptions: Occur at runtime (e.g. ArithmeticException, NullPointerException)
अनचेक्ड एक्सेप्शन: ये रनटाइम पर उत्पन्न होते हैं (जैसे ArithmeticException, NullPointerException)

Errors: Cannot be handled programmatically (e.g. OutOfMemoryError, StackOverflowError)
एरर: इन्हें प्रोग्राम के द्वारा नियंत्रित नहीं किया जा सकता (जैसे OutOfMemoryError, StackOverflowError)


🎯 Keywords Used in Exception Handling एक्सेप्शन हैंडलिंग में प्रयुक्त कीवर्ड

Keyword Meaning (अर्थ)
try Code block that may throw exceptionएक्सेप्शन उत्पन्न होने वाला कोड
catch Handles the exceptionएक्सेप्शन को संभालता है
finally Executes always, used for cleanupहमेशा चलेगा, क्लीनअप के लिए
throw Throws an exceptionएक्सेप्शन फेंकने के लिए
throws Declares exception in method signatureमेथड में संभावित एक्सेप्शन बताने के लिए

💡 Example 1: ArrayIndexOutOfBoundsException

import java.util.*;
class ExcepArrOutDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int arr[] = new int[10];
int n;
System.out.println("Enter the length of array");
try {
n = s.nextInt();
for(int i = 0; i < n; i++)
arr[i] = s.nextInt();
} catch (ArrayIndexOutOfBoundsException aioobe) {
System.out.println("ARRAY IS OVERFLOW");
aioobe.printStackTrace();
}
}
}

यह प्रोग्राम बताएगा कि यदि हम ऐरे से बाहर वैल्यू डालते हैं तो क्या होगा।


💡 Example 2: ArithmeticException

import java.util.*;
class ExcepDivDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b, c;
System.out.println("Enter numerator");
a = s.nextInt();
System.out.println("Enter denominator");
b = s.nextInt();
try {
c = a / b;
System.out.println("Result = " + c);
} catch (ArithmeticException ae) {
System.out.println("Denominator must not be zero");
ae.printStackTrace();
}
}
}

यह उदाहरण ज़ीरो से भाग देने पर एरर हैंडलिंग को दर्शाता है।


💡 Example 3: NullPointerException

class NullPointerDemo {
public void NullP() {
System.out.println("exception");
}
public static void main(String[] args) {
try {
NullPointerDemo np = null;
if(np == null)
throw new NullPointerException();
np.NullP();
} catch (NullPointerException npe) {
System.out.println("Null reference found");
npe.printStackTrace();
}
}
}

यह कोड दिखाता है कि अगर हम null ऑब्जेक्ट पर कोई मेथड कॉल करते हैं तो क्या होगा।


🔧 Create Your Own Exception

class MyException extends Exception {
MyException(String message) {
super(message);
}
}
public class TestCustomException {
static void validateAge(int age) throws MyException {
if (age < 18)
throw new MyException("You are underage!");
else
System.out.println("Eligible to vote");
}
public static void main(String args[]) {
try {
validateAge(15);
} catch (MyException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}

उपरोक्त कोड में हमने एक कस्टम एक्सेप्शन बनाया है।


🔚 Conclusion (निष्कर्ष)

Exception Handling is essential for writing robust and user-friendly Java applications.
एक्सेप्शन हैंडलिंग मज़बूत और यूज़र-फ्रेंडली एप्लिकेशन बनाने के लिए ज़रूरी है।

It improves error management, makes debugging easier, and maintains control over program execution.
यह एरर मैनेजमेंट को बेहतर बनाता है, डिबगिंग आसान करता है और प्रोग्राम को नियंत्रित रखता है।

Types of Errors in Java (जावा में एरर के प्रकार)

Errors in Java are issues or mistakes that occur during the compilation or execution of a program. Understanding these errors is essential for debugging and writing efficient programs.

जावा में एरर वे समस्याएँ होती हैं जो प्रोग्राम को कम्पाइल या रन करते समय उत्पन्न होती हैं। इन त्रुटियों को समझना प्रोग्रामिंग में सुधार और डिबगिंग के लिए आवश्यक है।


🧩 a) Syntax Errors (सिंटेक्स एरर)

Syntax errors occur when the programmer does not follow the correct structure or rules of the Java language. These errors are detected during compilation time, hence also called compile-time errors.

सिंटेक्स एरर तब उत्पन्न होती हैं जब प्रोग्रामर जावा की नियमबद्ध संरचना का सही पालन नहीं करता है। ये त्रुटियाँ कम्पाइल के समय पता चलती हैं, इसलिए इन्हें कम्पाइल-टाइम एरर भी कहते हैं।

✅ Example:

// Missing semicolon
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!") // ← semicolon missing
}
}

❌ Output:

HelloWorld.java:3: error: ';' expected
System.out.println("Hello, World!")
^

🧠 b) Logical Errors (लॉजिकल एरर)

Logical errors occur when the program compiles and runs without crashing, but gives incorrect output due to faulty logic. These are the most difficult to detect because there is no error message.

लॉजिकल एरर तब होती हैं जब प्रोग्राम बिना किसी एरर के रन तो करता है, लेकिन आउटपुट गलत आता है। ये एरर सबसे मुश्किल होती हैं क्योंकि कोई एरर मैसेज नहीं मिलता।

✅ Example:

// Infinite loop due to faulty logic
public class InfiniteLoopExample {
public static void main(String[] args) {
for(int i = 1; true; i++) {
System.out.println("Value of i: " + i);
}
}
}

❌ Problem:

The loop will never terminate, creating an infinite loop.


⚠️ c) Runtime Errors (रन टाइम एरर) or Exceptions (एक्सेप्शन)

These errors occur while the program is running, not during compilation. They are often caused by illegal operations such as dividing by zero, accessing null objects, or file not found issues.

रन टाइम एरर वो त्रुटियाँ हैं जो प्रोग्राम के रन करते समय उत्पन्न होती हैं। ये एक्सेप्शन कहलाती हैं और आमतौर पर अवैध ऑपरेशन के कारण होती हैं जैसे - ज़ीरो से डिवाइड करना या null को एक्सेस करना।

Technically, errors and exceptions are different in Java:

  • Errors like OutOfMemoryError are serious issues and can't be handled easily.

  • Exceptions like ArithmeticException or NullPointerException can be caught using try-catch blocks.

तकनीकी रूप से Error और Exception अलग-अलग होते हैं:

  • Error (जैसे OutOfMemoryError) गंभीर होती हैं और इन्हें हैंडल करना कठिन होता है।

  • Exception (जैसे ArithmeticException) को try-catch से संभाला जा सकता है।

✅ Example:

// Division by zero
public class RuntimeErrorExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a / b); // Runtime Error: ArithmeticException
}
}

❌ Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero

🛡 Handling Runtime Exceptions (रन टाइम एक्सेप्शन को हैंडल करना)

You can handle runtime exceptions using try-catch blocks to avoid program crashes.

आप try-catch ब्लॉक का उपयोग करके रन टाइम एक्सेप्शन को संभाल सकते हैं और प्रोग्राम को क्रैश होने से बचा सकते हैं।

✅ Example with try-catch:

public class HandleException {
public static void main(String[] args) {
try {
int a = 10, b = 0;
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.println("Can't divide by zero!");
}
}
}

✅ Output:

Can't divide by zero!

🔚 Conclusion (निष्कर्ष)

Understanding the types of errors in Java helps developers write better, bug-free programs. Always check for syntax, plan logic carefully, and handle exceptions gracefully.

जावा में एरर के प्रकारों की समझ से प्रोग्रामिंग में गुणवत्ता आती है। सिंटेक्स की जांच करें, लॉजिक सही बनाएँ और एक्सेप्शन को सावधानीपूर्वक हैंडल करें।

Java Synchronization जावा में सिंक्रोनाइजेशन

Synchronization in Java is a technique used to control the access of multiple threads to a shared resource. When multiple threads try to access the same method or resource simultaneously, it may lead to inconsistency or unexpected behavior. Synchronization ensures that only one thread can access the resource at a time.

जावा में सिंक्रोनाइजेशन एक ऐसी तकनीक है जिसका उपयोग तब किया जाता है जब एक से अधिक थ्रेड किसी साझा संसाधन (resource) को एक ही समय पर एक्सेस करने की कोशिश करते हैं। यह स्थिति डेटा इनकंसिस्टेंसी या प्रोग्राम एरर का कारण बन सकती है। सिंक्रोनाइजेशन के द्वारा हम सुनिश्चित करते हैं कि एक समय में केवल एक ही थ्रेड उस संसाधन को एक्सेस करे।


🧵 Why Synchronization is Needed? (सिंक्रोनाइजेशन क्यों आवश्यक है?)

English:

  • Prevents thread interference थ्रेड हस्तक्षेप को रोकने के लिए

  • Ensures data consistency डेटा की स्थिरता बनाए रखने के लिए

  • Useful in banking apps, inventory systems, etc. बैंकिंग या इन्वेंट्री जैसे एप्लिकेशन में आवश्यक


🛠️ Ways to Synchronize in Java (जावा में सिंक्रोनाइजेशन के तरीके)

1️⃣ Synchronized Method (सिंक्रोनाइज्ड मेथड)

You can synchronize an entire method by adding the synchronized keyword to its declaration. This locks the resource for that thread.

आप किसी मेथड को पूरी तरह से synchronized बना सकते हैं। इसके लिए synchronized कीवर्ड को मेथड डिक्लेरेशन में जोड़ा जाता है। इससे वह मेथड उस थ्रेड के लिए लॉक हो जाता है।

Syntax:

synchronized return_type methodName() {
// critical section
}

2️⃣ Synchronized Block (सिंक्रोनाइज्ड ब्लॉक)

If only part of the method needs to be synchronized, you can use a synchronized block to lock only that section.

यदि पूरे मेथड को synchronized बनाना आवश्यक न हो तो केवल उस भाग को लॉक किया जा सकता है जहाँ साझा संसाधन उपयोग हो रहा हो।

Syntax:

synchronized(object_reference) {
// critical code
}

🧪 Example: Synchronized Method

Below is a Java program that uses a synchronized method to print a multiplication table safely when accessed by two threads:

English:
This example shows that even if two threads call the same method on the same object, the output remains consistent.

Hindi:
यह उदाहरण दर्शाता है कि दो थ्रेड्स यदि एक ही ऑब्जेक्ट की मेथड को कॉल करें, तब भी आउटपुट बिना टकराव के आता है।

// Java Program to demonstrate synchronized method
class Table {
synchronized void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
public class TestSynchronization {
public static void main(String args[]) {
final Table obj = new Table(); // shared object
Thread t1 = new Thread() {
public void run() {
obj.printTable(5);
}
};
Thread t2 = new Thread() {
public void run() {
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}

Output (आउटपुट):

5
10
15
...
50
100
200
300
...
1000

📝 Output always comes in a sequence – either full 5-table or full 100-table first, never mixed — thanks to synchronization.


🔐 Benefits of Synchronization (सिंक्रोनाइजेशन के लाभ)

✅ Thread-safe resource access थ्रेड-सेफ संसाधन एक्सेस
✅ Avoids race conditions रेस कंडीशन से सुरक्षा
✅ Suitable for multi-user systems मल्टी-यूज़र सिस्टम में उपयोगी
✅ Reduces inconsistent state errors डेटा असंगतता से बचाव



⚠️ Caution While Using Synchronization (सावधानी)

English:
Overusing synchronization may cause: सिंक्रोनाइजेशन का अत्यधिक उपयोग कर सकते हैं:

  • Performance degradation परफॉर्मेंस में कमी

  • Deadlocks if not handled carefully डेडलॉक की संभावना


🔚 Conclusion (निष्कर्ष)

Synchronization is a vital part of multithreaded programming in Java. It helps maintain consistency and correctness in concurrent applications by allowing only one thread to access a resource at a time.

सिंक्रोनाइजेशन जावा की मल्टीथ्रेडिंग का एक अनिवार्य हिस्सा है। यह एक समय में केवल एक थ्रेड को साझा संसाधन तक पहुंच प्रदान कर प्रोग्राम को सुरक्षित और सटीक बनाता है।

Java Multithreading जावा में मल्टीथ्रेडिंग

Multithreading in Java is a powerful feature that allows concurrent execution of two or more parts of a program to make the best use of CPU. Java enables programs to perform multiple tasks simultaneously using threads.

जब हम जावा प्रोग्राम चलाते हैं, तो CPU को कई बार इंतजार करना पड़ता है जैसे – इनपुट लेने में, नेटवर्क रिस्पॉन्स का इंतजार, या कोई अन्य प्रोसेस के कारण। इस बेकार समय का उपयोग हम दूसरे कार्यों के लिए कर सकते हैं, और यही प्रक्रिया Multithreading कहलाती है।


🧵 What is a Thread? (थ्रेड क्या होता है?)

A Thread is a lightweight subprocess, the smallest unit of processing. A thread executes within a program and shares process resources with other threads.

एक थ्रेड वह यूनिट है जो "execution में चल रहा प्रोग्राम" कहलाता है। जावा प्रोग्राम में default thread main() होता है। लेकिन हम और भी थ्रेड्स बना सकते हैं ताकि काम समानांतर (parallel) हो सके।


🔄 Thread Life Cycle (थ्रेड का जीवन चक्र)

  1. New – Thread is created. थ्रेड ऑब्जेक्ट बनते ही इस स्थिति में होता है।

  2. Runnable – Thread is ready to run. start() मेथड कॉल होने पर Runnable स्थिति में आता है।

  3. Running – Thread is executing.  run() मेथड कॉल होने पर यह एक्टिव होता है।

  4. Waiting – Thread is waiting for resources.  किसी कारण थ्रेड रुका हुआ है।

  5. Terminated – Thread has completed execution. कार्य समाप्त होते ही थ्रेड Destroy हो जाता है।


🔧 Creating Thread in Java (जावा में थ्रेड बनाना)

1️⃣ Using Thread Class (थ्रेड क्लास का उपयोग)

English:
You can create a thread by extending the Thread class and overriding the run() method.

Hindi:
आप Thread क्लास को extend करके और run() मेथड को override करके थ्रेड बना सकते हैं।

🧪 Example Program:

// Example using Thread class
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running using Thread class.");
}
public static void main(String args[]) {
MyThread t1 = new MyThread();
t1.start(); // Start the thread
}
}

Output:

Thread is running using Thread class.

2️⃣ Using Runnable Interface (रनेबल इंटरफेस का उपयोग)

English:
You can implement the Runnable interface and pass it to the Thread constructor.

Hindi:
आप Runnable इंटरफेस को implement करके उसे Thread के कंस्ट्रक्टर में पास कर सकते हैं।

🧪 Example Program:

// Example using Runnable interface
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running using Runnable interface.");
}
public static void main(String args[]) {
MyRunnable myRunnable = new MyRunnable();
Thread t1 = new Thread(myRunnable);
t1.start(); // Start the thread
}
}

Output:

Thread is running using Runnable interface.

🛠️ Important Thread Methods (महत्वपूर्ण थ्रेड मेथड्स)

Method Description (English) विवरण (Hindi)
start() Starts the thread थ्रेड को चालू करता है
run() Contains the code to be executed थ्रेड द्वारा किया जाने वाला कार्य
sleep(ms) Puts thread to sleep for milliseconds थ्रेड को कुछ समय तक सुलाता है
join() Waits for another thread to die दूसरे थ्रेड के समाप्त होने का इंतजार
getName() Gets thread name थ्रेड का नाम प्राप्त करता है
getPriority() Gets thread priority थ्रेड की प्राथमिकता दर्शाता है
isAlive() Checks if thread is alive थ्रेड ज़िंदा है या नहीं

🧠 Why Use Multithreading in Java? (जावा में मल्टीथ्रेडिंग क्यों?)

✅ Better CPU Utilization (बेहतर CPU उपयोग)
✅ Faster Execution (तेजी से एक्सीक्यूशन)
✅ Efficient Resource Handling (प्रभावी संसाधन उपयोग)
✅ Useful for Games, Servers, GUI Apps (खासकर गेम, सर्वर और GUI के लिए)


📝 निष्कर्ष (Conclusion)

Multithreading makes Java powerful and efficient. By creating multiple threads, a program can utilize system resources better and provide faster execution.

मल्टीथ्रेडिंग से जावा और भी ज्यादा प्रभावी और शक्तिशाली बनता है। यह सीपीयू के समय का बेहतर उपयोग करता है और आपकी एप्लिकेशन को responsive बनाता है।

Sunday, July 27, 2025

Java Packages- Built-in and User Defined जावा पैकेज- इन-बिल्ट और यूजर डिफाइंड

Java पैकेज प्रोग्रामिंग के लिए एक शानदार तरीका है जिससे हम classes और interfaces को categories में बांट सकते हैं। इससे आपके कोड को manage करना, reuse करना और पढ़ना बहुत आसान हो जाता है। Java में मुख्यतः दो प्रकार के packages होते हैं:-

1. In-built Packages (इन-बिल्ट पैकेज)

Java API में पहले से ही कई useful packages शामिल होते हैं, जो अलग-अलग काम के लिए जरूरी classes और interfaces प्रोवाइड करते हैं:

  • java.awt:
    इसका फुल फॉर्म “Abstract Window Toolkit” है। इस पैकेज की मदद से आप Graphical User Interface (GUI) बना सकते हैं, जैसे windows, buttons, menus आदि।

  • java.net:
    Networking से जुड़े सभी operations (जैसे socket programming, HTTP connections) के लिए इस पैकेज का यूज़ किया जाता है।

इसके अलावा भी कई महत्वपूर्ण पैकेज हैं जैसे java.io, java.util, javax.swing, आदि।


2. User Defined Packages (यूजर डिफाइंड पैकेज)

अगर आप अपनी जरूरत के हिसाब से कोई खास functionality अलग से रखना चाहते हैं, तो आप खुद का package बना सकते हैं। आइए एक उदाहरण के साथ समझते हैं:

1. Package बनाना:

// File: MyClass.java
package MyPack;

public class MyClass {
    public void getsetname(String s) {
        System.out.println(s);
    }
}

2. Package को Import करके यूज़ करना:

// File: AccessName.java
import MyPack.MyClass;

public class AccessName {
    public static void main(String args[]) {
        String name = "Ajay";
        MyClass obj = new MyClass();
        obj.getsetname(name);
    }
}

User Defined Package Compile करने की कमांड:

javac -d . MyClass.java

यह कमांड compiled files को सही package structure में सेव करती है (e.g., MyPack/MyClass.class).


निष्कर्ष:

Java में packages का इस्तेमाल करके आप अपने कोड को modular और maintainable बना सकते हैं। In-built packages डेवलपमेंट को आसान बनाते हैं और user-defined packages आपको flexibility देते हैं।

Wednesday, July 23, 2025

Interfaces in Java जावा में इंटरफेस

An interface in Java is a kind of blueprint or structure for a class. Just like a class, it can have methods and variables, but the methods declared in an interface are abstract by default. In other words, an interface defines a contract that other classes must follow. Interfaces are used to achieve abstraction and multiple inheritance.

इंटरफेस, जावा में एक तरह की क्लास का ढांचा या ब्लूप्रिंट होता है। इसमें क्लास की तरह ही मेथड और वेरिएबल होते हैं, लेकिन इनमें घोषित मेथड डिफ़ॉल्ट रूप से एब्सट्रेक्ट होती हैं। इंटरफेस का उपयोग एब्सट्रेक्शन और मल्टीपल इनहेरिटेंस प्राप्त करने के लिए किया जाता है।


📌 Interface Features (इंटरफेस की विशेषताएं)

In newer versions of Java, interfaces can also contain private, default, and static methods. Interfaces cannot be instantiated directly, but a class that implements an interface must provide implementations for all its abstract methods.

जावा के नए संस्करणों में, इंटरफेस में private, default, और static मेथड्स भी हो सकती हैं। इंटरफेस का इंस्टेंस नहीं बनाया जा सकता, लेकिन जो क्लास इसे इम्प्लीमेंट करती है उसे इसकी सभी एब्सट्रेक्ट मेथड्स को डिफाइन करना जरूरी होता है।


🔧 Implementing Interfaces (इंटरफेस को इम्प्लीमेंट करना)

To implement an interface in Java, the implements keyword is used.

interface Animal {
   public void test();
}

class Cat implements Animal {
   public void test() {
      System.out.println("Test method of Animal interface is Implemented");
   }

   public static void main(String args[]) {
      Animal a = new Cat();
      a.test();
   }
}

आउटपुट:

Test method of Animal interface is Implemented

इंटरफेस को इम्प्लीमेंट करने के लिए implements कीवर्ड का उपयोग किया जाता है। ऊपर दिए गए उदाहरण में Cat क्लास ने Animal इंटरफेस की test() मेथड को इम्प्लीमेंट किया है।


♻️ Multiple Inheritance Using Interfaces (इंटरफेस से मल्टीपल इनहेरिटेंस)

Java does not support multiple inheritance with classes, but we can achieve it using interfaces.

interface Eatable {
   void eat();
}

interface Drinkable {
   void drink();
}

class Food implements Eatable, Drinkable {
   public void eat() {
      System.out.println("this is eatable");
   }
   public void drink() {
      System.out.println("this is drinkable");
   }

   public static void main(String args[]) {
      Food obj = new Food();
      obj.eat();
      obj.drink();
   }
}

आउटपुट:

this is eatable
this is drinkable

जावा में एक क्लास एक से अधिक इंटरफेस को इम्प्लीमेंट करके मल्टीपल इनहेरिटेंस को प्राप्त कर सकती है।


🔗 Extending Interfaces (इंटरफेस को एक्सटेंड करना)

An interface can extend another interface using the extends keyword.

interface NewsPaper {
   void news();
}

interface Magazine extends NewsPaper {
   void colorful();
}

एक इंटरफेस दूसरे इंटरफेस को extends कीवर्ड से एक्सटेंड कर सकता है, जैसा कि ऊपर उदाहरण में दर्शाया गया है।


✅ Benefits of Interface (इंटरफेस के लाभ)

  1. Achieves full abstraction. सम्पूर्ण एब्सट्रेक्शन प्राप्त करने के लिए।

  2. Supports multiple inheritance. मल्टीपल इनहेरिटेंस के लिए।

  3. Helps in loose coupling between components. लूज़ कपलिंग के लिए सहायक।


⚔️ Interface vs Abstract Class (इंटरफेस बनाम एब्सट्रेक्ट क्लास)

Feature Interface Abstract Class
Methods Only abstract (Java 8+ allows default/static) Abstract + non-abstract
Keyword interface abstract
Inheritance Can extend only interfaces Can extend class and implement interfaces
Multiple Inheritance Supported Not supported
Variables Public, static, final only Can be static, final, or instance
Implementation Cannot provide implementation Can provide implementation

इंटरफेस और एब्सट्रेक्ट क्लास में यह प्रमुख अंतर होते हैं, जो उनके उपयोग को परिभाषित करते हैं।


🧪 Program Example (प्रोग्राम उदाहरण)

class Example {
   void view() {
      System.out.println("Example class extends in Using class");
   }
}

interface ex {
   final double max = 100;
}

interface ex1 extends ex {
   final double pi = 3.14;
   void show();
}

interface ex2 {
   final double PI = (22 / 7);
   void display();
}

class Using extends Example implements ex1, ex2 {
   double exa = max;
   double ex1a = pi;
   double ex2a = PI;

   public void show() {
      System.out.println("max=" + exa);
      System.out.println("pi=" + ex1a);
   }

   public void display() {
      System.out.println("PI=" + ex2a);
   }
}

public class TestInterface {
   public static void main(String args[]) {
      Using u = new Using();
      ex1 e1 = u;
      e1.show();

      ex2 e2 = u;
      e2.display();

      u.view();
   }
}

इस प्रोग्राम में एक क्लास, एक क्लास को एक्सटेंड कर रही है और दो इंटरफेस को इम्प्लीमेंट कर रही है, जिससे इंटरफेस की शक्ति का प्रदर्शन होता है।

Java Wrapper Classes – Boxing, Unboxing, and Need जावा व्रेपर क्लासेस – बॉक्सिंग, अनबॉक्सिंग और आवश्यकता

What is a Wrapper Class? व्रेपर क्लास क्या है?

A wrapper class in Java is a special type of class whose object contains a primitive data type. In simple terms, it wraps primitive values into an object form.

व्रेपर क्लास जावा में एक विशेष प्रकार की क्लास होती है जिसका ऑब्जेक्ट प्रिमिटिव डेटा टाइप को अपने अंदर संग्रहीत करता है। सरल शब्दों में, यह एक प्रिमिटिव मान को ऑब्जेक्ट में लपेटता है।


🔄 a) Boxing and Autoboxing बॉक्सिंग और ऑटोबॉक्सिंग

Java allows converting a primitive type into its corresponding wrapper object — this is called Boxing. When this happens automatically, it's called Autoboxing.

जावा में प्रिमिटिव डेटा टाइप को उनके संबंधित व्रेपर ऑब्जेक्ट में बदलने की प्रक्रिया को बॉक्सिंग कहा जाता है। जब यह रूपांतरण स्वचालित रूप से होता है तो उसे ऑटोबॉक्सिंग कहते हैं।


Examples:

  • intInteger

  • doubleDouble

  • longLong


🔁 b) Unboxing and Auto-unboxing अनबॉक्सिंग और ऑटोअनबॉक्सिंग

The reverse of boxing — converting a wrapper object back into a primitive type — is called Unboxing. If done automatically, it's called Auto-unboxing.

बॉक्सिंग की विपरीत प्रक्रिया, जिसमें व्रेपर क्लास ऑब्जेक्ट को फिर से प्रिमिटिव टाइप में बदला जाता है, अनबॉक्सिंग कहलाती है। जब यह प्रक्रिया स्वयं होती है, तो इसे ऑटोअनबॉक्सिंग कहते हैं।


📦 Java Wrapper Class Table जावा व्रेपर क्लास तालिका

Primitive Type Wrapper Class
boolean                     Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

💻 Example 1 – Primitive to Wrapper (Boxing)

public class WrapperExample1 {
public static void main(String args[]) {
int a = 5;
Integer i = Integer.valueOf(a); // boxing
Integer j = a; // autoboxing
System.out.println(a + " " + i + " " + j);
}
}

💻 Example 2 – Wrapper to Primitive (Unboxing)

public class WrapperExample2 {
public static void main(String args[]) {
Integer a = new Integer(3);
int i = a.intValue(); // unboxing
int j = a; // auto-unboxing
System.out.println(a + " " + i + " " + j);
}
}

Why Wrapper Classes are Needed? व्रेपर क्लास की आवश्यकता क्यों होती है?

  1. They convert primitive types into objects. यह प्रिमिटिव डेटा टाइप को ऑब्जेक्ट में बदलते हैं।

  2. Classes in java.util work only with objects. java.util पैकेज की क्लासेस केवल ऑब्जेक्ट के साथ काम करती हैं।

  3. Data structures like ArrayList and Vector store only objects. जैसे डेटा स्ट्रक्चर – ArrayList, Vector केवल ऑब्जेक्ट स्टोर करते हैं।

  4. In multithreading, synchronization requires objects. मल्टीथ्रेडिंग में सिंक्रोनाइज़ेशन के लिए ऑब्जेक्ट की आवश्यकता होती है।

Java Vector Class – Explained with Methods and Example जावा वेक्टर क्लास – विधियों और उदाहरण सहित समझाया गया

Introduction to Vector Class वेक्टर क्लास का परिचय

The Vector class in Java is like a dynamic array that grows as needed. Unlike arrays, you don't need to specify its size in advance. It is a part of the java.util package and implements the List interface. Objects in a vector can be added, removed, accessed, or searched using their index positions. 

वेक्टर क्लास, जावा में एक डायनामिक ऐरे की तरह होता है, जो आवश्यकता अनुसार आकार बढ़ा सकता है। पारंपरिक ऐरे के विपरीत, इसमें साइज पहले से तय करने की जरूरत नहीं होती। यह java.util पैकेज का हिस्सा है और List इंटरफ़ेस को इम्प्लीमेंट करता है। इसमें ऑब्जेक्ट्स को इंडेक्स द्वारा जोड़ा, हटाया, खोजा और एक्सेस किया जा सकता है।


🧾 Syntax / सिंटैक्स

public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Here <E> stands for Element type.
यहाँ <E> का अर्थ है एलिमेंट (Element) का प्रकार।


🔧 Commonly Used Vector Methods | सामान्यत: प्रयोग होने वाले वेक्टर मेथड्स

Method Description (English) विवरण (हिंदी)
add() Adds an element to the vector वेक्टर में एलिमेंट जोड़ता है
addAll() Adds multiple elements at once एक साथ कई एलिमेंट जोड़ता है
firstElement() Returns the first element पहले एलिमेंट को लौटाता है
clear() Empties the vector वेक्टर को खाली करता है
get() Gets element at specific index किसी विशेष इंडेक्स का एलिमेंट प्राप्त करता है
insertElementAt() Inserts element at specific index विशेष इंडेक्स पर एलिमेंट जोड़ता है
indexOf() Finds index of given element दिए गए एलिमेंट का इंडेक्स देता है
remove() Removes element at a specific index विशेष इंडेक्स से एलिमेंट हटाता है
removeAllElements() Removes all elements सभी एलिमेंट हटाता है
clone() Creates a clone (copy) of vector वेक्टर की क्लोन कॉपी बनाता है
capacity() Returns current capacity of vector वेक्टर की वर्तमान क्षमता बताता है
contains() Checks if vector contains an element जांचता है कि एलिमेंट मौजूद है या नहीं
isEmpty() Checks if the vector is empty वेक्टर खाली है या नहीं
lastElement() Returns the last element in the vector वेक्टर का अंतिम एलिमेंट लौटाता है

💻 Example Program | उदाहरण प्रोग्राम

import java.util.*;

public class useOfVector {
    public static void main(String args[]) {
        Vector<String> v = new Vector<>();
        v.addElement("ram");
        v.addElement("syam");
        v.addElement("raja");
        v.addElement("rohan");
        v.addElement("sohan");

        System.out.println("This name vector contains: " + v.firstElement());
        System.out.println("These name vector contains: " + v.lastElement());
        System.out.println("Capacity of this vector: " + v.capacity());
        System.out.println("Size of this vector: " + v.size());

        for(int i = 0; i < v.size(); i++) {
            System.out.println(" " + v.get(i));
        }
    }
}

🖨 Output | आउटपुट

This name vector contains: ram  
These name vector contains: sohan  
Capacity of this vector: 10  
Size of this vector: 5  
ram  
syam  
raja  
rohan  
sohan  

📌 Key Points

  • Vector increases its capacity automatically.

  • Can hold different types of objects if not type-parameterized.

  • Thread-safe, hence slightly slower than ArrayList.

📌 मुख्य बातें

  • वेक्टर की क्षमता अपने आप बढ़ जाती है।

  • यदि जनरिक्स का उपयोग नहीं किया गया हो, तो यह विभिन्न प्रकार के ऑब्जेक्ट्स रख सकता है।

  • थ्रेड-सेफ होता है, इसलिए ArrayList की तुलना में थोड़ा धीमा होता है।

Java Strings – Working with Text in Java जावा स्ट्रिंग्स

In Java, a string is a sequence of characters. For example, "Java Book" is a string. Strings in Java are defined as objects so that various operations can be performed on them like concatenation, comparison, and extraction. The String class allows us to create string variables or objects and perform operations on them.

जावा में, एक स्ट्रिंग अक्षरों (characters) का एक क्रम होती है। जैसे कि "जावा की किताब" एक स्ट्रिंग है। जावा में स्ट्रिंग को ऑब्जेक्ट के रूप में परिभाषित किया गया है ताकि हम उस पर विभिन्न ऑपरेशन्स जैसे जोड़ना, तुलना करना आदि कर सकें। String क्लास की मदद से हम सामान्य स्ट्रिंग वेरिएबल या ऑब्जेक्ट बना सकते हैं और उन पर ऑपरेशन कर सकते हैं।

String myString = "this is a string"; // variable
String myString = new String("This is a string"); // object

✨ Common Methods of Java String Class जावा स्ट्रिंग क्लास की सामान्य मेथड्स:

1. equals() – String Comparison

Used to compare two strings. If they are equal, it returns true.

दो स्ट्रिंग्स की तुलना के लिए equals() मेथड का प्रयोग किया जाता है। यदि दोनों समान हों तो यह true लौटाता है।

String s1 = "hello world";
String s2 = "hello world";
if(s1.equals(s2)) {
System.out.println("Both Strings are Equal.");
}

2. toUpperCase() and toLowerCase() – Change Case

These methods convert the string to uppercase or lowercase.

ये मेथड्स किसी स्ट्रिंग को कैपिटल या स्मॉल अक्षरों में बदलने के लिए उपयोग होते हैं।

String str1 = "gooDMoRniNg";
System.out.println(str1.toUpperCase()); // GOODMORNING
System.out.println(str1.toLowerCase()); // goodmorning

3. startsWith() and endsWith() – Check Prefix/Suffix

Used to test whether the string starts or ends with a particular word.

इनका उपयोग यह जांचने के लिए किया जाता है कि स्ट्रिंग किसी विशेष शब्द से शुरू या समाप्त होती है या नहीं।

String s1 = "hello world";
if(s1.startsWith("hello")) System.out.println("Starts with hello");
if(s1.endsWith("world")) System.out.println("Ends with world");

4. length() – Get String Length

Returns the number of characters in the string.

स्ट्रिंग की लम्बाई यानी करैक्टर की संख्या को जानने के लिए length() मेथड प्रयोग की जाती है।

String myString = "Hello World";
System.out.println(myString.length()); // 11

5. concat() – String Concatenation

Used to join two strings either with + or concat() method.

दो स्ट्रिंग्स को जोड़ने के लिए + या concat() मेथड का प्रयोग किया जाता है।

String s1 = "Hello ";
String s2 = "World";
String result = s1.concat(s2); // Hello World
System.out.println(result);

6. charAt() – Access Specific Character

Returns the character at the specified index.

यह मेथड किसी दिए गए इंडेक्स पर मौजूद करैक्टर को लौटाता है।

String s = "hello";
System.out.println(s.charAt(1)); // e

7. substring() – Extract Part of String

Extracts a part of the string from a given start index to end index.

इसका उपयोग स्ट्रिंग का कोई भाग प्राप्त करने के लिए किया जाता है।

String s = "Sitaram";
System.out.println(s.substring(2, 5)); // tar

8. indexOf() – Find Position of Substring

Returns the starting index of a substring within a string.

यह सबस्ट्रिंग के पहले अक्षर की पोजीशन (index) लौटाता है।

String s = "hello my name is khan";
System.out.println(s.indexOf("my")); // 6

Array in Java – Types, Syntax & Examples जावा में ऐरे

In Java, an array is a collection of similar data types stored in contiguous memory locations. Each element in an array is accessed using an index, which starts from 0 and goes up to arraySize - 1. The array name acts as the base address for the array. Arrays are also known as subscripted variables in programming. Java arrays are of two main types:

जावा में, ऐरे (Array) एक ही प्रकार के डेटा एलेमेंट्स का समूह होता है जिसे कंप्यूटर मेमोरी में लगातार (contiguous) बाइट्स पर स्टोर किया जाता है। प्रत्येक एलिमेंट को इंडेक्स के माध्यम से एक्सेस किया जाता है जो 0 से arraySize - 1 तक होता है। ऐरे का नाम ही उसका बेस एड्रेस होता है। इन्हें सबस्क्रिप्टेड वेरिएबल्स भी कहा जाता है। जावा ऐरे के दो मुख्य प्रकार होते हैं:


🔸 a) One-Dimensional Array (एकविमीय ऐरे – 1D)

A 1D array stores elements in a single linear list format. It uses one subscript ([]), hence the name 1D array. All elements are of the same type and stored in consecutive memory locations.

1D ऐरे का उपयोग डेटा को एक लिस्ट के रूप में स्टोर करने के लिए किया जाता है। इसमें केवल एक सबस्क्रिप्ट ([]) होता है। यह एक ही प्रकार के डेटा को संग्रहित करता है और सभी एलिमेंट्स लगातार क्रम में स्टोर होते हैं।

✅ Syntax (प्रारूप):

data_type[] array_name = new data_type[size]; // Declaration
data_type[] array_name = {value0, value1, ..., valuen}; // Initialization

📘 Example:

int[] empID = new int[5];
int[] empID = new int[] {2, 4, 6, 8, 10};
// Accessing array elements
System.out.println(empID[0]); // Output: 2
System.out.println(empID[1]); // Output: 4

यहाँ empID ऐरे 5 इन्टिजर वैल्यूज़ स्टोर करता है जिन्हें इंडेक्स द्वारा एक्सेस किया जाता है।


🔹 b) Multi-Dimensional Array (बहुविमीय ऐरे – 2D, 3D...)

Java allows arrays with two or more dimensions using multiple subscripts like [][], [][][], etc. The most commonly used is the 2D array, which is used to represent tables, matrices, or tabular records with rows and columns.

जावा में आप एक से अधिक सबस्क्रिप्ट [] का उपयोग करके 2D, 3D, 4D... nD ऐरे बना सकते हैं। इन सभी को मिलाकर मल्टीडायमेंशनल ऐरे कहा जाता है। सामान्यतः 2D ऐरे का प्रयोग सारणीबद्ध डेटा जैसे तालिकाएं, रिकॉर्ड्स या मैट्रिक्स को स्टोर करने के लिए किया जाता है।

✅ Syntax (प्रारूप):

data_type[][] array_name = new data_type[rows][columns];

📘 Example:

int[][] marks = new int[3][5]; // 3 rows and 5 columns

इस उदाहरण में marks एक 2D ऐरे है जिसमें 3 पंक्तियाँ (rows) और 5 स्तम्भ (columns) हैं।

🔺 Note:

कभी भी ऐरे को डिक्लेयर करते समय उसकी साइज न दें जैसे - int[] arr = new int[]; — यह कंपाइल टाइम एरर देगा।


📏 Array Length vs String Length

In Java, you can use the .length variable to find the size of an array. But for strings, you use the .length() method.

जावा में ऐरे की लंबाई पता करने के लिए .length वेरिएबल का उपयोग किया जाता है, जबकि स्ट्रिंग की लंबाई जानने के लिए .length() मेथड का उपयोग किया जाता है।


🧪 Program 1: Find Length of an Array

public class ArrayTest {
public static void main(String[] args) {
int[] rollNo = new int[] {10, 20, 30};
System.out.println("Size of Array rollNo is = " + rollNo.length);
}
}

📤 Output:

Size of Array rollNo is = 3

🧪 Program 2: Print Elements Using Loop

public class ArrayTest {
public static void main(String[] args) {
int[] empId = new int[] {10, 20, 30};
for (int i = 0; i < empId.length; i++) {
System.out.println(empId[i]);
}
}
}

📤 Output:

10
20
30

✅ सारांश तालिका (Summary Table)

ऐरे का प्रकार उपयोग एक्सेस
1D ऐरे                      लिस्ट डेटा                                    एक इंडेक्स से
2D ऐरे तालिका/मैट्रिक्स दो इंडेक्स से (पंक्ति और स्तम्भ)
length ऐरे की साइज जानने के लिए वेरिएबल
length() स्ट्रिंग की लंबाई जानने के लिए मेथड

📚 Other Stream Classes in Java जावा में अन्य स्ट्रीम क्लासेस

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operat...