- Introduction to kotlin variables in Hindi
- Syntax of kotlin variables in Hindi
- Printing kotlin variables in Hindi
Introduction to Kotlin Variables
यदि आप data के साथ कोई operation perform करना चाहते है तो सबसे पहले यह आवश्यक है की वह data computer की memory में load किया जाये।
उदाहरण के लिए यदि आप दो numbers को add करना चाहते है तो इसके लिए पहले आपको उन दोनों numbers को computer की memory में store करना होगा। इसके बाद ही आप उन numbers के बीच addition operation perform कर सकते है।
Used to Store Data in Computer Memory
एक Variable का प्रयोग data को store करने के लिए किया जाता है। Variable असल में एक memory location का नाम होता है जँहा पर data store किया जाता है।
जब भी आप कोई variable create करते है तो एक नाम और type define करते है। इससे compiler को पता चलता है की आप एक particular नाम से किसी particular type के data को store करना चाहते है।
Compiler computer की memory में data को store करने के लिए space provide करता है और उसे आपके द्वारा define किया गया नाम दे देता है।
इसके बाद आप उस नाम द्वारा उस memory location पर data store भी कर सकते है और उसे access भी कर सकते है।
Syntax of Kotlin Variables
Kotlin में variables create करना दूसरी programming languages से अलग है।
<val> or <var> <variable-Name> <:> <data-Type> = <value>
|
Kotlin में variable create करने के लिए सबसे पहले आप val या var keyword define करते है। इसके बाद variable का नाम define किया जाता है। Variable का नाम पुरे program में unique होना चाहिए।
Variable के नाम के बाद colon (:) लिखा जाता है। Colon के बाद type को define किया जाता है। Type define करने के बाद आप variable में जो value store करना चाहते है उसे define किया जाता है।
val & var Keywords
Kotlin में आप दो प्रकार के variables create कर सकते है।
- Immutable Variables – जिनकी value को change नहीं किया जा सकता है।
- Mutable Variables – जिनकी value को normal variables की तरह change किया जा सकता है।
Kotlin में immutable variables create करने के लिए val keyword का प्रयोग किया जाता है। यदि आप immutable variables को change करने का प्रयास करते है तो (Val cannot be reassigned) error show होती है।
<val> <variable-Name> <:> <data-Type> = <value>
|
इसी प्रकार var keyword का प्रयोग करके आप mutable variables create कर सकते है।
<var> <variable-Name> <:> <data-Type> = <value>
|
If No Type Defined
Kotlin एक statically typed language है। यानी की compile time पर variable का type compiler को ज्ञात होना चाहिए। यही कारण है की kotlin में variables declare करते समय type भी declare किया जाता है।
लेकिन यदि आप चाहे तो kotlin variable declare करते समय type declaration को skip कर सकते है। ऐसा करने पर compiler automatically assign की गयी value के आधार पर variable का type निर्धारित कर लेता है।
उदाहरण के लिए आप एक integer variable define करते है।
var Age = 18
|
जब compiler ऊपर दिए गए statement को read करेगा तो Age variable की value का type automatically guess कर लेगा और उसे इस प्रकार interpret करेगा।
var Age: Int = 18
|
If Not Initializing
यदि आप kotlin में variable create करते समय उसे initialize नहीं कर रहे है तो ऐसी situation में variable का type declare करना अनिवार्य होता है।
<val> or <var> <variable-Name> <:> <data-Type>
|
Null Safety
Kotlin आपको Null safety provide करती है। Kotlin में यदि आप किसी variable को NULL assign करने का प्रयास करते है तो compiler द्वारा error show की जाती है। Kotlin का उद्देश्य code से NullPointerException को मिटाना है।
लेकिन यदि आप किसी variable को NULL assign करना चाहते है तो ऐसी situation में पहले आप compiler को बताते है की आप उस variable में NULL store करना चाहते है। इसके लिए आप variable declare करते समय type declaration के बाद question mark symbol लगाते है इससे compiler को पता चल जाता है की उस variable में NULL value store की जा सकती है।
var Age: Int? = 18
print(age)
Age = null |
Printing Variables in Kotlin
Kotlin में normal strings और variables की values को print() function द्वारा print किया जाता है।
print(variable-Name)
|
Using Concatenation (+) Operator
Kotlin में आप java की ही तरह concatenation operator का प्रयोग करते हुए string के साथ किसी variable को print करवा सकते है।
var Name: String = “AJ”
print(“My Name is “+Name) |
Previous: Kotlin Basic Types
Next: Kotlin Control Flow