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);
}
}
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
Post a Comment