जावा ऑपरेटर: अंकगणित, संबंधपरक, तार्किक और अधिक

इस ट्यूटोरियल में, आप जावा में विभिन्न प्रकार के ऑपरेटरों, उनके सिंटैक्स और उदाहरणों की मदद से उनका उपयोग करने के तरीके के बारे में जानेंगे।

ऑपरेटर प्रतीक हैं जो चर और मूल्यों पर संचालन करते हैं। उदाहरण के लिए, +एक ऑपरेटर इसके अलावा के लिए उपयोग किया जाता है, जबकि *एक ऑपरेटर गुणा के लिए भी उपयोग किया जाता है।

जावा में ऑपरेटरों को 5 प्रकारों में वर्गीकृत किया जा सकता है:

  1. अंकगणितीय आपरेटर
  2. असाइनमेंट ऑपरेटर्स
  3. संबंधपरक संकारक
  4. लॉजिकल ऑपरेटर्स
  5. गैर संचालक
  6. बिटवाइज ऑपरेटर्स

1. जावा अंकगणितीय ऑपरेटर

अंकगणित ऑपरेटरों का उपयोग चर और डेटा पर अंकगणितीय संचालन करने के लिए किया जाता है। उदाहरण के लिए,

 a + b;

यहां, +ऑपरेटर का उपयोग दो चर a और b को जोड़ने के लिए किया जाता है। इसी तरह, जावा में विभिन्न अन्य अंकगणितीय ऑपरेटर हैं।

ऑपरेटर ऑपरेशन
+ जोड़
- घटाव
* गुणन
/ विभाजन
% मोडुलो ऑपरेशन (विभाजन के बाद शेष)

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

 class Main ( public static void main(String() args) ( // declare variables int a = 12, b = 5; // addition operator System.out.println("a + b = " + (a + b)); // subtraction operator System.out.println("a - b = " + (a - b)); // multiplication operator System.out.println("a * b = " + (a * b)); // division operator System.out.println("a / b = " + (a / b)); // modulo operator System.out.println("a % b = " + (a % b)); ) )

आउटपुट

 a + b = 17 a - b = 7 a * b = 60 a / b = 2 a% b = 2 

उपरोक्त उदाहरण में, हम का इस्तेमाल किया है +, -और *गणना करने के लिए ऑपरेटरों इसके अलावा, घटाव, गुणा और संचालन।

/ प्रभाग संचालक

a / bहमारे कार्यक्रम में ऑपरेशन पर ध्यान दें । /ऑपरेटर विभाजन ऑपरेटर है।

यदि हम दो पूर्णांकों के साथ डिवीजन ऑपरेटर का उपयोग करते हैं, तो परिणामी भागफल भी पूर्णांक होगा। और, यदि कोई एक ऑपरेंड एक फ्लोटिंग-पॉइंट नंबर है, तो हमें रिजल्ट भी फ्लोटिंग-पॉइंट में मिलेगा।

 In Java, (9 / 2) is 4 (9.0 / 2) is 4.5 (9 / 2.0) is 4.5 (9.0 / 2.0) is 4.5

% मोडुलो ऑपरेटर

मॉडुलो ऑपरेटर %शेष की गणना करता है। जब a = 7विभाजित किया जाता है b = 4, तो शेष 3 होता है

नोट : %ऑपरेटर मुख्य रूप से पूर्णांक के साथ प्रयोग किया जाता है।

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

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

 int age; age = 5;

यहाँ, =असाइनमेंट ऑपरेटर है। यह वैरिएबल के दाईं ओर अपने बाईं ओर वैल्यू असाइन करता है। अर्थात्, 5 को चर आयु को सौंपा गया है।

आइए देखें जावा में कुछ और असाइनमेंट ऑपरेटर उपलब्ध हैं।

ऑपरेटर उदाहरण के बराबर
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

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

 class Main ( public static void main(String() args) ( // create variables int a = 4; int var; // assign value using = var = a; System.out.println("var using =: " + var); // assign value using =+ var += a; System.out.println("var using +=: " + var); // assign value using =* var *= a; System.out.println("var using *=: " + var); ) )

आउटपुट

 var = =: 4 वर्जन का उपयोग करके = =: 8 वर्जन * =: 32 का उपयोग करते हुए

3. जावा रिलेशनल ऑपरेटर्स

रिलेशनल ऑपरेटर्स का उपयोग दो ऑपरेंड्स के बीच संबंधों की जांच करने के लिए किया जाता है। उदाहरण के लिए,

 // check is a is less than b a < b;

यहां, >ऑपरेटर रिलेशनल ऑपरेटर है। यह जांचता है कि क्या बी से कम है या नहीं।

वह trueया तो लौटता है या false

ऑपरेटर विवरण उदाहरण
== के बराबर है 3 == 5झूठा लौटता है
!= असमान 3 != 5सच लौटाता है
> से अधिक 3> 5झूठा लौटता है
< से कम 3 < 5सच लौटाता है
>= इससे बड़ा या इसके बराबर 3>= 5झूठा लौटता है
<= से कम या बराबर 3 <= 5झूठा लौटता है

उदाहरण 3: रिलेशनल ऑपरेटर्स

 class Main ( public static void main(String() args) ( // create variables int a = 7, b = 11; // value of a and b System.out.println("a is " + a + " and b is " + b); // == operator System.out.println(a == b); // false // != operator System.out.println(a != b); // true //> operator System.out.println(a> b); // false // < operator System.out.println(a = operator System.out.println(a>= b); // false // <= operator System.out.println(a <= b); // true ) )

नोट : रिलेशनल ऑपरेटर्स का उपयोग निर्णय लेने और लूप में किया जाता है।

4. जावा लॉजिकल ऑपरेटर्स

एक अभिव्यक्ति है trueया नहीं यह जाँचने के लिए लॉजिकल ऑपरेटर्स का उपयोग किया जाता है false। इनका उपयोग निर्णय लेने में किया जाता है।

ऑपरेटर उदाहरण अर्थ
&& (तार्किक और) expression1 && expression2 true only if both expression1 and expression2 are true
|| (Logical OR) expression1 || expression2 true if either expression1 or expression2 is true
! (Logical NOT) !expression true if expression is false and vice versa

Example 4: Logical Operators

 class Main ( public static void main(String() args) ( // && operator System.out.println((5> 3) && (8> 5)); // true System.out.println((5> 3) && (8 < 5)); // false // || operator System.out.println((5 5)); // true System.out.println((5> 3) || (8 < 5)); // true System.out.println((5 < 3) || (8 3)); // false ) )

Working of Program

  • (5> 3) && (8> 5) returns true because both (5> 3) and (8> 5) are true.
  • (5> 3) && (8 < 5) returns false because the expression (8 < 5) is false.
  • (5> 3) || (8> 5) returns true because the expression (8> 5) is true.
  • (5> 3) && (8> 5) returns true because the expression (5> 3) is true.
  • (5> 3) && (8> 5) returns false because both (5 < 3) and (8 < 5) are false.
  • !(5 == 3) returns true because 5 == 3 is false.
  • !(5> 3) returns false because 5> 3 is true.

5. Java Unary Operators

Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6.

Different types of unary operators are:

Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean

Increment and Decrement Operators

Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decrease it by 1. For example,

 int num = 5; // increase num by 1 ++num;

Here, the value of num gets increased to 6 from its initial value of 5.

Example 5: Increment and Decrement Operators

 class Main ( public static void main(String() args) ( // declare variables int a = 12, b = 12; int result1, result2; // original value System.out.println("Value of a: " + a); // increment operator result1 = ++a; System.out.println("After increment: " + result1); System.out.println("Value of b: " + b); // decrement operator result2 = --b; System.out.println("After decrement: " + result2); ) )

Output

 Value of a: 12 After increment: 13 Value of b: 12 After decrement: 11

In the above program, we have used the ++ and -- operator as prefixes (++a, --b). We can also use these operators as postfix (a++, b++).

There is a slight difference when these operators are used as prefix versus when they are used as a postfix.

To learn more about these operators, visit increment and decrement operators.

6. Java Bitwise Operators

Bitwise operators in Java are used to perform operations on individual bits. For example,

 Bitwise complement Operation of 35 35 = 00100011 (In Binary) ~ 00100011 ________ 11011100 = 220 (In decimal)

Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).

The various bitwise operators present in Java are:

Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR

These operators are not generally used in Java. To learn more, visit Java Bitwise and Bit Shift Operators.

Other operators

Besides these operators, there are other additional operators in Java.

Java instanceof Operator

The instanceof operator checks whether an object is an instanceof a particular class. For example,

 class Main ( public static void main(String() args) ( String str = "Programiz"; boolean result; // checks if str is an instance of // the String class result = str instanceof String; System.out.println("Is str an object of String? " + result); ) )

Output

 Is str an object of String? true

Here, str is an instance of the String class. Hence, the instanceof operator returns true. To learn more, visit Java instanceof.

Java Ternary Operator

The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example,

 variable = Expression ? expression1 : expression2

Here's how it works.

  • If the Expression is true, expression1 is assigned to the variable.
  • If the Expression is false, expression2 is assigned to the variable.

Let's see an example of a ternary operator.

 class Java ( public static void main(String() args) ( int februaryDays = 29; String result; // ternary operator result = (februaryDays == 28) ? "Not a leap year" : "Leap year"; System.out.println(result); ) )

Output

 Leap year

In the above example, we have used the ternary operator to check if the year is a leap year or not. To learn more, visit the Java ternary operator.

अब जब आप जावा ऑपरेटरों के बारे में जानते हैं, तो यह उस ऑर्डर के बारे में जानने का समय है जिसमें ऑपरेटरों का मूल्यांकन किया जाता है। अधिक जानने के लिए, जावा संचालक वरीयता देखें।

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