Java के Operators
In this page:
result = operand1 operator operand2;
What are Operators?
एक operator एक special symbol है जो एक या ज़्यादा values पर, operands कहलाती हैं, एक operation perform करता है, एक नई value produce करते हुए -- + symbol, उदाहरण के लिए, एक operator है जो दो numeric operands को एक साथ जोड़ता है।
उदाहरण: What are Operators?
public class Main {
public static void main(String[] args) {
int result = 5 + 3; // '+' is the operator, 5 and 3 are the operands
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Categories of Operators
Java operators को इस आधार पर categories में group करता है कि वे किस तरह का result produce करते हैं: arithmetic operators numbers compute करते हैं, relational operators values compare करते हैं और booleans produce करते हैं, और logical operators boolean values combine करते हैं।
उदाहरण: Categories of Operators
public class Main {
public static void main(String[] args) {
int sum = 5 + 3; // arithmetic -> number
boolean isBigger = 5 > 3; // relational -> boolean
boolean both = true && isBigger; // logical -> boolean
System.out.println(sum + " " + isBigger + " " + both);
}
}
Login to try C/C++/Java/PHP code in the editor
Operators and Operands
हर operator operands पर act करता है -- + और * जैसे ज़्यादातर operators binary हैं और दो operands चाहते हैं, जबकि ++ और unary - जैसे कुछ unary हैं और सिर्फ एक single operand पर act करते हैं।
उदाहरण: Operators and Operands
public class Main {
public static void main(String[] args) {
int a = 5, b = 3;
int sum = a + b; // binary - needs two operands
int negative = -a; // unary - needs just one operand
System.out.println(sum + " " + negative);
}
}
Login to try C/C++/Java/PHP code in the editor
Combining Operators in Expressions
अलग-अलग categories के operators अक्सर एक single expression में combined होते हैं, जैसे एक condition बनाने के लिए arithmetic और relational operators mix करना, या एक result compute और store करने के लिए arithmetic और assignment operators।
उदाहरण: Combining Operators in Expressions
public class Main {
public static void main(String[] args) {
int total = 5;
total += 3; // arithmetic + assignment combined
boolean valid = total > 5 && total < 20; // relational + logical combined
System.out.println(total + " " + valid);
}
}
Login to try C/C++/Java/PHP code in the editor
Operator Precedence Preview
जब एक expression कई operators mix करता है, Java उन्हें एक fixed precedence order के अनुसार evaluate करता है -- multiplication और division default रूप से addition और subtraction से पहले चलते हैं, और parentheses हमेशा उस default order को override कर सकते हैं।
उदाहरण: Operator Precedence Preview
public class Main {
public static void main(String[] args) {
int result = 2 + 3 * 4; // multiplication runs first: 2 + 12 = 14
int forced = (2 + 3) * 4; // parentheses override: 5 * 4 = 20
System.out.println(result + " " + forced);
}
}
Login to try C/C++/Java/PHP code in the editor
=(assignment) को==(comparison) के साथ confuse करना।- यह मान लेना कि integers पर
/एक decimal result देता है, जब7 / 23है। - Precedence नज़रअंदाज़ करना, इसलिए
2 + 3 * 414है,20नहीं; clear होने के लिए parentheses उपयोग करें।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: