Showing posts with label java tutorial for beginners. Show all posts
Showing posts with label java tutorial for beginners. Show all posts

Sunday, November 26, 2023

Variables in Java जावा में वेरिएबल

In Java, a variable is a named storage location used to hold data values during program execution. Variables are essential because they allow you to store, update, and use data dynamically.

जावा में वेरिएबल (चर) एक नामित स्टोरेज स्थान होता है, जिसका उपयोग प्रोग्राम के दौरान डेटा को संग्रहित और उपयोग करने के लिए किया जाता है। वेरिएबल्स प्रोग्रामिंग का एक मूलभूत हिस्सा होते हैं।


🔹 1. Declaration | घोषणा

Before using a variable, it must be declared with its data type and name.

किसी वेरिएबल का उपयोग करने से पहले, उसका डेटा प्रकार और नाम बताकर उसे घोषित (declare) करना होता है।

int age; // Declare an integer variable named 'age'

🔹 2. Initialization | मान देना

After declaring, you can assign a value. This is called initialization.

घोषणा के बाद, वेरिएबल को कोई मान (value) देना Initialization कहलाता है।

age = 50; // Initialize the variable

Declaration and initialization can also be done together:

int age = 50;

🔹 3. Variable Types | वेरिएबल के प्रकार

Java is statically typed, so you must declare the type. Common types are:

int count = 10;         // Integer
double pi = 3.14;       // Decimal number
char grade = 'A';       // Character
boolean isStudent = true; // True or False

जावा में वेरिएबल का डेटा प्रकार पहले से तय करना होता है जैसे int, double, char, boolean आदि।


🔹 4. Naming Rules | नामकरण के नियम

  • Must start with a letter, _ or $

  • Can include letters, digits, _, and $

  • Case-sensitive (Age and age are different)

  • Reserved words like class, int cannot be used

वेरिएबल नाम छोटे अक्षरों से शुरू होते हैं और विशेष शब्दों (keywords) का उपयोग नहीं किया जा सकता।


🔹 5. Scope of Variable | वेरिएबल का स्कोप

Scope means where the variable can be accessed:

public class Example {
    int globalVar = 5; // Class-level (field)

    public void exampleMethod() {
        int localVar = 10; // Local to method
        System.out.println(localVar);
    }
}

वेरिएबल का स्कोप यह तय करता है कि वह किस भाग में मान्य है – जैसे क्लास लेवल, मेथड लेवल या ब्लॉक लेवल।


🔹 6. Final Keyword | अंतिम (Final) वेरिएबल

To make a variable constant, use final:

final double PI = 3.14159;

final का प्रयोग वेरिएबल को स्थायी (unchangeable) बनाने के लिए होता है।


✅ Java Program: Simple Interest | जावा प्रोग्राम: साधारण ब्याज

public class SimpleInterest {
    public static void main(String args[]) {
        // Declare variables
        float p, r, t, si;

        // Initialize variables
        p = 1000;   // Principal
        r = 2;      // Rate
        t = 5;      // Time

        // Calculate SI
        si = (p * r * t) / 100;

        // Print result
        System.out.println("Simple Interest is= " + si);
    }
}

Output:

Simple Interest is= 100.0

📝 Summary (सारांश):

  • Variables store data

  • Must be declared with type

  • Can be initialized separately or together

  • Scope defines accessibility

  • Use final to make constants

वेरिएबल्स डेटा स्टोर करने के लिए आवश्यक हैं, इन्हें पहले घोषित करना होता है, और final से स्थिरांक बनाया जा सकता है।

Constants (Literal) in Java जावा में स्थिरांक

In Java, a constant is a variable whose value cannot be changed once assigned. Constants are used to represent fixed values that remain unchanged during the program’s execution. In Java, constants are declared using the final keyword.

जावा में, स्थिरांक (constant) एक ऐसा वेरिएबल होता है जिसका मान एक बार निर्धारित हो जाने के बाद बदला नहीं जा सकता है। स्थिरांकों का उपयोग ऐसे मानों को दर्शाने के लिए किया जाता है जो प्रोग्राम के निष्पादन के दौरान हमेशा एक जैसे रहते हैं। जावा में, कॉन्स्टेंट्स को final कीवर्ड से घोषित किया जाता है।

📌 Syntax (सिंटैक्स):-

final dataType CONSTANT_NAME = value;

Example (उदाहरण):

final double PI = 3.14159;

🌐 Global Constants in Java | वैश्विक स्थिरांक

In Java, a global constant refers to a value that can be accessed throughout a class or even across multiple classes in the same package. Such constants are generally declared using both the static and final keywords.

जावा में, वैश्विक स्थिरांक (Global Constant) ऐसा मान होता है जिसे किसी एक क्लास या एक ही पैकेज की कई क्लासों में भी उपयोग किया जा सकता है। इसके लिए सामान्यतः static और final दोनों कीवर्ड का प्रयोग किया जाता है।

📌 Syntax (सिंटैक्स):

static final dataType CONSTANT_NAME = value;

Example (उदाहरण):

static final double PI = 3.14159;

✅ Java Program to Demonstrate Constants | जावा प्रोग्राम – स्थिरांक का प्रदर्शन

public class ConstantsExample {
    // Declaration of constants
    static final int MAX_VALUE = 100;
    static final String GREETING = "Hello, World!";
    static final double PI = 3.14159;

    public static void main(String[] args) {
        // Accessing constants within the main method
        System.out.println("Max Value: " + MAX_VALUE);
        System.out.println("Greeting: " + GREETING);
        System.out.println("Value of PI: " + PI);
    }
}

Output (आउटपुट):

Max Value: 100
Greeting: Hello, World!
Value of PI: 3.14159

🔍 Summary (सारांश):

  • Constants help in writing safe and clean code.

  • They make your program more readable and maintainable.

  • Constants should be named in UPPERCASE letters with words separated by underscores (e.g., MAX_SPEED, PI, MIN_BALANCE).

स्थिरांक प्रोग्राम को सुरक्षित, स्पष्ट और समझने योग्य बनाते हैं। इन्हें बड़े अक्षरों (UPPERCASE) में और शब्दों को _ से अलग करके नाम देना चाहिए।

📚 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...