कोटलिन ऑपरेटर्स: अंकगणित, असाइनमेंट ऑपरेटर और अधिक

कोटलिन के पास अंकगणित, असाइनमेंट, तुलना ऑपरेटरों और अधिक प्रदर्शन करने के लिए ऑपरेटरों का एक सेट है। आप इस लेख में इन ऑपरेटरों का उपयोग करना सीखेंगे।

ऑपरेटर विशेष प्रतीक (अक्षर) होते हैं जो ऑपरेंड (चर और मान) पर संचालन करते हैं। उदाहरण के लिए, +एक ऑपरेटर है जो इसके अतिरिक्त कार्य करता है।

जावा चर लेख में, आपने चर घोषित करने और चर को मान निर्दिष्ट करने के लिए सीखा। अब, आप संचालकों का उपयोग करना सीखेंगे, ताकि वे उन पर विभिन्न कार्य कर सकें।

1. अंकगणित संचालक

यहाँ कोटलिन में अंकगणितीय ऑपरेटरों की एक सूची दी गई है:

कोटलिन अरिथमेटिक ऑपरेटर्स
ऑपरेटर अर्थ
+ इसके अलावा (स्ट्रिंग संयोजन के लिए भी प्रयोग किया जाता है)
- घटाव संचालक
* गुणक संचालक
/ प्रभाग संचालक
% मोडुलस ऑपरेटर

उदाहरण: अंकगणित संचालक

 fun main(args: Array) ( val number1 = 12.5 val number2 = 3.5 var result: Double result = number1 + number2 println("number1 + number2 = $result") result = number1 - number2 println("number1 - number2 = $result") result = number1 * number2 println("number1 * number2 = $result") result = number1 / number2 println("number1 / number2 = $result") result = number1 % number2 println("number1 % number2 = $result") ) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 नंबर 1 + नंबर 2 = 16.0 नंबर 1 - नंबर 2 = 9.0 नंबर 1 * नंबर 2 = 43.75 नंबर 1 / नंबर 2 = 3.5714285714285716 नंबर 1% नंबर 2 = 2.0

+ऑपरेटर भी के संयोजन के लिए प्रयोग किया जाता है Stringमान।

उदाहरण: स्ट्रिंग्स का संघटन

 fun main(args: Array) ( val start = "Talk is cheap. " val middle = "Show me the code. " val end = "- Linus Torvalds" val result = start + middle + end println(result) )

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

बोलना आसान है। मुझे कोड दिखाओ। - लिनुस टोरवाल्ड्स

अंकगणित ऑपरेटर वास्तव में कैसे काम करते हैं?

मान लीजिए, आप +अंक 2 और ए को जोड़ने के लिए अंकगणितीय ऑपरेटर का उपयोग कर रहे हैं ।

हुड के तहत, अभिव्यक्ति सदस्य फ़ंक्शन को a + bकॉल a.plus(b)करता है। plusऑपरेटर के साथ काम करने के लिए ओवरलोड हो गया है Stringमूल्यों और अन्य बुनियादी डेटा प्रकार (चार और बूलियन को छोड़कर)।

 // + ऑपरेटर के लिए बुनियादी प्रकार ऑपरेटर मज़ा प्लस (अन्य: बाइट): इंट ऑपरेटर मज़ा प्लस (अन्य: लघु): इंट ऑपरेटर मज़ा प्लस (अन्य: इंट): इंट ऑपरेटर मज़ा प्लस (अन्य: लंबा): लंबे ऑपरेटर मज़ा प्लस (अन्य: फ्लोट): फ्लोट ऑपरेटर मज़ेदार प्लस (अन्य: डबल): स्ट्रिंग कॉन्फैक्शन ऑपरेटर के लिए डबल // स्ट्रिंग। अधिशेष (अन्य: कोई भी?): स्ट्रिंग 

आप फ़ंक्शन +को ओवरलोडिंग plus()फ़ंक्शन द्वारा उपयोगकर्ता-परिभाषित प्रकार (जैसे ऑब्जेक्ट) के साथ काम करने के लिए भी उपयोग कर सकते हैं ।

अनुशंसित पढ़ना: कोटलिन ऑपरेटर ओवरलोडिंग

यहां अंकगणित ऑपरेटरों और उनके संबंधित कार्यों की एक तालिका है:

अभिव्यक्ति कार्य का नाम में अनुवाद करता है
ए + बी प्लस a। अधिशेष (b)
ए - बी माइनस अ .मिनस (b)
ए * बी समय a .imes (b)
ए / बी div aiv (b)
अ% ब मॉड a.mod (b)

2. असाइनमेंट ऑपरेटर्स

असाइनमेंट ऑपरेटर्स का इस्तेमाल वैरिएबल को वैल्यू असाइन करने के लिए किया जाता है। हमने पहले भी सरल असाइनमेंट ऑपरेटर का उपयोग किया है =

 वैल आयु = 5

यहां, 5 को =ऑपरेटर का उपयोग करके चर आयु को सौंपा गया है ।

यहां सभी असाइनमेंट ऑपरेटरों और उनके संबंधित कार्यों की एक सूची दी गई है:

अभिव्यक्ति के बराबर में अनुवाद करता है
ए + = बी a = a + b a.plusAssign (b)
ए - = बी a = a - b a.minusAssign (b)
ए * = बी a = a * b a .imesAssign (b)
ए / = बी a = a / b a.divAssign (b)
अ% = b a = a% b a.modAssign (b)

उदाहरण: असाइनमेंट ऑपरेटर्स

 fun main(args: Array) ( var number = 12 number *= 5 // number = number*5 println("number = $number") )

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 संख्या = 60

अनुशंसित पढ़ना: कोटलिन में असाइनमेंट ऑपरेटरों को ओवरलोड करना।

3. यूनीरी प्रीफिक्स और इन्क्रीमेंट / डिक्रीमेंट ऑपरेटर्स

Here's a table of unary operators, their meaning, and corresponding functions:

Operator Meaning Expression Translates to
+ Unary plus +a a.unaryPlus()
- Unary minus (inverts sign) -a a.unaryMinus()
! not (inverts value) !a a.not()
++ Increment: increases value by1 ++a a.inc()
-- Decrement: decreases value by 1 --a a.dec()

Example: Unary Operators

 fun main(args: Array) ( val a = 1 val b = true var c = 1 var result: Int var booleanResult: Boolean result = -a println("-a = $result") booleanResult = !b println("!b = $booleanResult") --c println("--c = $c") )

When you run the program, the output will be:

 -a = -1 !b = false --c = 0

Recommended Reading: Overloading Unary Operators

4. Comparison and Equality Operators

Here's a table of equality and comparison operators, their meaning, and corresponding functions:

Operator Meaning Expression Translates to
> greater than a> b a.compareTo(b)> 0
< less than a < b a.compareTo(b) < 0
>= greater than or equals to a>= b a.compareTo(b)>= 0
<= less than or equals to a < = b a.compareTo(b) <= 0
== is equal to a == b a?.equals(b) ?: (b === null)
!= not equal to a != b !(a?.equals(b) ?: (b === null))

Comparison and equality operators are used in control flow such as if expression , when expression , and loops .

Example: Comparison and Equality Operators

 fun main(args: Array) ( val a = -12 val b = 12 // use of greater than operator val max = if (a> b) ( println("a is larger than b.") a ) else ( println("b is larger than a.") b ) println("max = $max") ) 

When you run the program, the output will be:

 b is larger than a. max = 12 

Recommended Reading: Overloading of Comparison and Equality Operators in Kotlin

5. Logical Operators

There are two logical operators in Kotlin: || and &&

Here's a table of logical operators, their meaning, and corresponding functions.

Operator Description Expression Corresponding Function
|| true if either of the Boolean expression is true (a>b)||(a (a>b)or(a
&& true if all Boolean expressions are true (a>b)&&(a (a>b)and(a

Note that, or and and are functions that support infix notation.

Logical operators are used in control flow such as if expression , when expression , and loops .

Example: Logical Operators

 fun main(args: Array) ( val a = 10 val b = 9 val c = -1 val result: Boolean // result is true is a is largest result = (a>b) && (a>c) // result = (a>b) and (a>c) println(result) )

When you run the program, the output will be:

 true

Recommended Reading: Overloading of Logical Operators in Kotlin

6. in Operator

The in operator is used to check whether an object belongs to a collection.

Operator Expression Translates to
in a in b b.contains(a)
!in a !in b !b.contains(a)

Example: in Operator

 fun main(args: Array) ( val numbers = intArrayOf(1, 4, 42, -3) if (4 in numbers) ( println("numbers array contains 4.") ) )

When you run the program, the output will be:

 numbers array contains 4.

Recommended Reading: Kotlin in Operator Overloading

7. Index access Operator

Here are some expressions using index access operator with corresponding functions in Kotlin.

Expression Translated to
a(i) a.get(i)
a(i, n) a.get(i, n)
a(i1, i2,… , in) a.get(i1, i2,… , in)
a(i) = b a.set(i, b)
a(i, n) = b a.set(i, n, b)
a(i1, i2,… , in) = b a.set(i1, i2,… , in, b)

Example: Index access Operator

 fun main(args: Array) ( val a = intArrayOf(1, 2, 3, 4, - 1) println(a(1)) a(1)= 12 println(a(1)) ) 

When you run the program, the output will be:

 2 12

Recommended Reading: Kotlin Index access operator Overloading

8. Invoke Operator

कोटलिन में संबंधित कार्यों के साथ इनवोक ऑपरेटर का उपयोग करते हुए यहां कुछ अभिव्यक्तियाँ हैं।

अभिव्यक्ति में अनुवाद किया
a() a.invoke()
a(i) a.invoke(i)
a(i1, i2,… , in) a.inkove(i1, i2,… , in)
a(i) = b a.set(i, b)

कोटलिन में, कोष्ठक को invokeसदस्य फ़ंक्शन को कॉल करने के लिए अनुवादित किया जाता है ।

अनुशंसित पढ़ना: कोटलिन में ऑपरेटर ओवरलोडिंग को आमंत्रित करें

बिटवाइज ऑपरेशन

जावा के विपरीत, कोटलिन में बिटवाइज़ और बिटशिफ्ट ऑपरेटर नहीं हैं। इन कार्यों को करने के लिए, विभिन्न कार्यों (infix संकेतन का समर्थन) का उपयोग किया जाता है:

  • shl - हस्ताक्षरित शिफ्ट छोड़ दिया
  • shr - सही शिफ्ट हस्ताक्षरित
  • ushr - अनसाइनड शिफ्ट राइट
  • and - बिटवाइज़ और
  • or - बिटवाइज़ या
  • xor - बिटवाइज़ एक्सर
  • inv - बिटवाइज़ उलटा

कोटलिन में बिटवाइज़ ऑपरेशंस के बारे में अधिक जानने के लिए इस पेज पर जाएँ।

इसके अलावा, जावा के विपरीत कोटलिन में कोई टर्नरी ऑपरेटर नहीं है।

दिलचस्प लेख...