Operators In java
Operator एक symbol होते हैं , हम कह सकते हैं की Operators को कोई operation को perform करने के लिए use करते हैं।
Java Operators Types
Java में different - different action perform करने के लिए different - different Operators है । Java में normally use होने वाले Operators कुछ इस प्रकार हैं -
Arithmetic Operators
Assignment Operators
Comparison / Relational Operators
Logical Operators
Increment / Decrement Operator
Java Arithmetic Operators
Arithmetic Operators simple calculation में use होने wale Operators होते हैं जैसे Addition , Subtraction etc.
File : Operators.java
class Operators
{
public static void main(String[] args)
{
int a = 7;
int b = 89;
// here = is a assignment operator.
System.out.println(a+b);
// here + is arithmetic operator.
}
}
Output
javac Operators.java
java Operators
96
Java Assignment Operators
Assignment Operator को equals to = से represent करते हैं , जो कि value को किसी variable में assign करने के लिए use किया जाता है। हालाँकि इसे Arithmetic Operators के साथ भी use करते हैं। नीचे दिए गए Example में आप देख सकते हैं।
Java Assignment Operator Example
class AssignmentOperator
{
public static void main(String[] args)
{
// add then assign.
int a = 90;
a += 10;
System.out.println(a);
// devide 100 by 2 and assign.
a /= 2;
System.out.println("Quotient : "+ a);
// get module deviding 50 by 11 then assign.
a %= 11;
System.out.println("Modulas : "+ a);
}
}
Output
javac AssignmentOperator.java
java AssignmentOperator
100
quotient : 50
Modulas : 6
Java Incrementing-Decrementing Operators
Incrementing-Decrementing Operators को किसी variable को 1 से increase या decrease करने के लिए use किया जाता है। हालाँकि इसमें Addition और Subtraction operation ही होते हैं , इसलिए इसमें ( ++ ) या ( -- ) sign ही use होते हैं।
Java Incrementing-Decrementing Operator Example
class AssignmentOperator
{
public static void main(String[] args)
{
int a = 90;
System.out.println("Initial value : "+a);
// increment by 1
a++;
System.out.println("After increment : "+a);
// decrement by 1
a--;
System.out.println("After decrement : "+a);
}
}
Output
javac AssignmentOperator.java
java AssignmentOperator
Initial value : 90
After increment : 91
After decrement : 90
Java Comparison Operators
Comparison Operators दी हुई दो values को compare करके condition के according boolean value true / false value return करते हैं . Java में Comparison Operators को कुछ इस तरह से use कर सकते हैं।
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x == y); // returns false because 5 is not equal to 3
}
}
false
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x != y); // returns true because 5 is not equal to 3
}
}
True
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x > y); // returns true because 5 is greater than 3
}
}
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x < y); // returns false because 5 is not less than 3
}
}
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x >= y); // returns true because 5 is greater, or equal, to 3
}
}
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x <= y); // returns false because 5 is neither less than or equal to 3
}
}
Java Logical Operators
Logical Operators , एक या एक से अधिक expression के according Boolean value return करते हैं। जैसे -
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3 AND 5 is less than 10
}
}
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 || x < 4); // returns true because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
}
}
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(!(x > 3 && x < 10)); // returns false because ! (not) is used to reverse the result
}
}