Java Operators

Introduction to java operators

जैसा की आप सब जानते है operators का उपयोग आप variables पर operations perform करने के लिए करते है। हर data type पर अलग अलग operations perform किये जाते है। इसलिए operators भी अलग अलग यूज़ होते है।

Operators unary और binary 2 प्रकार के होते है। Operators के साथ यूज़ होने वाले variables को operand कहते है। Unary operators एक operand के साथ apply होते है और binary operators 2 operands पर apply होते है।

Java में मुख्यतः 6 type के operators होते है।

  1. Arithmetic operators
  2. Logical operators
  3. Bit wise operators
  4. Relational operators
  5. Assignment operators
  6. Conditional operators

इन सभी operators के बारे में निचे दिया जा रहा है।

Arithmetic operators

Arithmetic operators mathematical computation perform करने के लिए यूज़ किये जाते है। Arithmetic operators 5 type के होते है। इनके बारे में निचे दिया जा रहा है।

OperatorExplanationExample
Addition  (+)Addition operator 2 values को add करने के लिए यूज़ किया जाता है।    a+b
Subtraction (-) Subtraction operator एक value में से दूसरी value को subtract करने के लिए यूज़ किया जाता है।    a-c
Multiplication(*) Multiplication operators 2 variables की values को multiply करने के लिए यूज़ किया जाता है।    a*b
Division(/) किसी एक variable की संख्या का भाग दूसरे variable की संख्या में देने के लिए division operator यूज़ किया जाता है।    a/b
Modulus(%)भाग देने के बाद यदि आप शेष बची हुई संख्या प्राप्त करना चाहते है तो modulus operator यूज़ करते है।    a%b

Logical operators

Logical operators boolean values पर operations perform करने के लिए यूज़ किये जाते है। ये operators 3 तरह के होते है। इस तरह के operations में दोनों variables की value true या false हो सकती है। Logical operators के बारे में निचे दिया जा रहा है।

OperatorExplanationExample
Logical AND (&&)  इस तरह के operators के साथ  जब दोनों variables की value true होती है तो ही result true होता है नहीं तो false होता है।   a&&b 
Logical OR (||)    इस तरह के operator के साथ यदि दोनों variables में से किसी एक भी value true होगी तो result true ही होगा।   a||b 
Logical NOT (!)  ये एक unary operator है। जिस भी variable के साथ ये यूज़ किया जाता है उस variable की value को उल्टा कर देता है।     !a 

Bit-wise operators

Bit-wise operators bit by bit operations perform करने के लिए यूज़ किये जाते है। ये operators variables की values को bits में convert करके उन पर operations perform करते है। Bit wise operators 4 तरह के होते है। इनके बारे में निचे दिया जा रहा है।

OperatorExplanationExample
Bit wise AND (&)इस operator को जिन 2 variables के साथ यूज़ किया जाता है ये उनकी common bits को result variable में copy कर देता है।   a&b
Bit wise OR (|) इस operator को जिन 2 variables के साथ यूज़ किया जाता है ये उनकी uncommon bits को result variable में copy कर देता है।   a|b
Left shift (<<) इस operator के द्वारा आप किसी variable की value को bits में convert करने के बाद उसकी bits को left में shift करते है। कितनी bits को शिफ्ट करना है ये वो variable बताता है जो इसके right में होता है।    a<
Right shift (>>) इस variable के द्वारा आप किसी variable की value को bits में convert करने के बाद उसकी values को right में shift करते है। कितनी bits को shift करना है ये वो variable बताता है जो इसके right में होता है।    a>>b 

Relational operators

Relational operators 2 variables के बीच में relation को पता लगाने के लिए यूज़ किये जाते है। इनका प्रयोग ज्यादातर if statement में किया जाता है। इन operators से आपको 2 variables के बीच में relation का पता चलता है जैसे की क्या दोनों variables बराबर है या एक छोटा और एक बड़ा है।

OperatorExplanationExample
Equal to (==)यदि दोनों variables equal होते है तो result true होता है।   a==b
Not equal to(!=)यदि दोनों variables एक दूसरे के equal नहीं होते है तो result true return होता है।   a!=b
Greater than(>)यदि left side का variable right side के variable से बड़ा है तो result true होता है।    a>b
Less than(<)यदि left side का variable right side के variable से छोटा है तो result true होता है।   a
Greater than equal to (>=)यदि left side का variable right side के variable से बड़ा है या उसके बराबर है तो result true होगा।   a>=b
Less than equal to (<=)यदि left side का variable right side के variable से छोटा है या उसके बराबर है तो result true होता है  a<=b

Assignment operators

Assignment process को simple और fast बनाने के लिए java कुछ assignment operators provide करती है। इनके बारे में निचे दिया जा रहा है।

OperatorExplanationExample
Simple assignment (=)ये operator right value की value left variable को assign करता है।   a=b;
Plus assignment (+=)ये operator left और right variables की value को add करके left variable में store करता है।   a+=b;
Minus assignment(-=)ये operator left side के variable की value में से right side के variable की value घटाकर result left side के variable में store करता है।   a-=b
Multiply assignment(*=)ये operator left और right side के variables की values को multiply करके result left side के variable में store करता है।   a*=b
Divide assignment(/=)ये operator left side के variable की value को right side के variable से divide करके result left side के variable में store करता है।   a/=b

Conditional operator (?:)

Conditional operator java का special operator होता है जो if statement की तरह काम करता है। इसे ternary operator भी कहते है। ये variables में conditions के according values store करने के लिए यूज़ किया जाता है। इस operator में ? से पहले एक condition दी जाती है , यदि वो condition true है तो colon से पहले वाली value नहीं तो colon के बाद वाली value variable में store होती है।

Example

a=5, b=3;

c= a>b?a:b; // c में a की value store की जाएगी।