Operators in Java

Operator एक Symbol है जिसका उपयोग operations करने के लिए किया जाता है। उदाहरण के लिए: +, -, *, / आदि

उदाहरण:  5 + 6 = 11। यहाँ, 5 और 6 operand हैं, और + को Operator कहा जाता है। हम कुछ Situations में एक ही Operand के साथ-साथ दो से अधिक Operands के लिए भी operators का उपयोग कर सकते हैं

Types of Java Operators

  1. Arithmetic Operator
  2. Logical Operator
  3. Assignment Operator
  4. Java instance of Operator
  5. Ternary Operator or Conditional Operators
  6. Unary Operator
  7. Relational Operator
  8. Bitwise Operator

 

1. Arithmetic Operator

जब आप normal basic mathematical functions जैसे addition, subtraction, multiplication, division, आदि  Perform करना चाहते हैं, तो Arithmetic Operators का उपयोग किया जाता है। उन्हें primitive data types पर Perform किया जा सकता है।

Operator Name Syntax Function
+ Addition x + y दो values को जोड़ना
Subtraction x – y दो values को घटाना
* Multiplication x * y दो values का गुणा करना
/ Division x / y दो Values को divide करता है। यदि हम किसी number को 0 से divide करते हैं, तो यह या तो ArithmeticException को बढ़ा देगा, या इसका Result NaN या (+-) Infinity values होगा।
% Modulus x % y modulus operator division के बाद शेष दो संख्याओं को return करता है।

 

जब हम एक integer और Floating Point number का उपयोग single arithmetic operation के लिए एक  operands के रूप में किया जाता है, तो answer एक Floating point number होता है। हमने operands के implicit type conversion के आधार पर Arithmetic operators द्वारा return data types को संक्षेप में प्रस्तुत किया है।

Data Type of Result Data Type of Operands
long कोई भी operand float या double नहीं है। कम से कम एक operand long है।
int कोई भी operand float या double या long नहीं है।
double कम से कम एक operand एक double है
float कम से कम एक operand float है: कोई भी operand double नहीं है

 

Example:

class Arithmeticoperator {

public static void main(String[] args) {
int sum = 1 + 2;
// sum is now 3
System.out.println("1 + 2 = " + sum);
int original_sum = sum;

sum = sum - 1;
// sum is now 2
System.out.println(original_sum + " - 1 = " + sum);
original_sum = sum;

sum = sum * 2;
// sum is now 4
System.out.println(original_sum + " * 2 = " + sum);
original_sum = sum;

sum = sum / 2;
// sum is now 2
System.out.println(original_sum + " / 2 = " + sum);
original_sum = sum;

sum = sum + 8;
// sum is now 10
System.out.println(original_sum + " + 8 = " + sum);
original_sum = sum;

sum = sum % 7;
// sum is now 3
System.out.println(original_sum + " % 7 = " + sum);
}
}

Output:

1 + 2 = 3
3 - 1 = 2
2 * 2 = 4
4 / 2 = 2
2 + 8 = 10
10 % 7 = 3

 

2.Logical Operators

Operators जिनका उपयोग यह check करने के लिए किया जाता है कि दिया गया expression True है या False, logical Operators कहलाते हैं। Logical operations दो Boolean expressions पर की जाती हैं।

आमतौर पर, उनका उपयोग ‘Logical AND’ और ‘Logical OR‘ जैसे कुछ functions को करने के लिए किया जाता है। relational operators के विपरीत, जो सिर्फ एक Value की तुलना करते हैं और TRUE या FALSE return करता हैं, Logical operator TRUE या FALSE के रूप में logic को evaluate करते हैं। Logical operation में उपयोग किए जाने वाले values को पहले Boolean values में Convert किया जाता है और फिर उनको evaluate किया जाता है।

Name Function
Logical AND return value True होता है जब दी गई दोनों conditions True होती हैं (यदि पहला operand False है, तो दूसरा operand का evaluation नहीं किया जाता है)।
Logical OR return value True होता है जब कम से कम एक Condition True होती है (यदि पहला operand True है, तो दूसरा operand का evaluation नहीं किया जाता है)।
Logical NOT इसमें overall result को reverse करने की Power है। उदाहरण के लिए, यदि return value True है, तो यह False देता है।

 

Example:

class LogicalOperator {
public static void main(String[] args)
{

int x = 100, y = 200, z = 200;


System.out.println("Variable1 = " + x);
System.out.println("Variable2 = " + y);
System.out.println("Variable3 = " + z);

// using logical AND to verify
// two constraints
if ((x < y) && (y == z)) {
System.out.println("x < y: True, y == z: True. Inside if block");
}
else
System.out.println("Inside else block");
}
}

Output:

Variable1 = 100
Variable2 = 200
Variable3 = 200
x < y: True, y == z: True. Inside if block

 

 

 

Previous articleBasic Input/Output
Next articleJump Statements in Java

LEAVE A REPLY

Please enter your comment!
Please enter your name here