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) में और शब्दों को _ से अलग करके नाम देना चाहिए।

Comments

Popular posts from this blog

Java program to implement file input stream class to read binary data from image file.

Java program to find length of given string and convert it in to upper or lower case.

Java program to check marks out of Bound exception.

Java program to calculate area and perimeter of circle using interfaces.

Java program to merge two text files in to third file.

Java program to read a text file and displays it's contents on screen.

Concatenating and Buffering Files in Java जावा में फ़ाइलों को जोड़ना और बफर करना

Java program to check greatest among three numbers where input numbers are given by user.

Write a Java program to calculate the gross salary of an employee, if basic salary of that employee is greater or equal to 20000 and year's of service is equal or greater than 5 years bonus will be 2000 else1000, TA =10%, DA= 20% and HRA=5% of salary.