Java Operators : Java Operators is a symbol which is used to perform logical or mathematical manipulations. There are different types of operators available in java such as

Arithmetic Operators

Assignment Operators

Bitwise Operators

Conditional Operator

Logical Operators

Relational Operators

Arithmetic Operators : Java is providing the Arithmetic operators with eight Arithmetic Operators. Arithmetic Operators are similar to basic mathematics that we use for our calculations. Like we can do Addition, Subtraction , Multiplication, Division, Modulo, Increment, Decrement.

Addition ‘+’ operand : Addition operand adds values on either side of the operator

Subtraction ‘-‘ operand : Subtraction operand subtracts right hand side operand from the left hand side operand

Multiplication ‘*’operand : Multiplication operand multiplies values on either side of the operator

Division ‘/’operand : Division operand divides left hand side operand by the right hand side operand

Modulo ‘%’operand : Modulo operand divides left hand side operand by the right hand side operand and which returns remainder

Increment ‘++’operand : Increament operator increases the value of operand by one.

Decrement ‘- -‘operand : Decrement operator decreases the value of operand by one.

Example :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class javaArithmetic_Operators { public static void main(String[] args) { int a = 6, b = 4, c; c = a + b; System.out.println(“Adding of values a and b results to ” + c); d = a – b; System.out.println(“Subtracting of values a and b results to ” + d); e = a * b; System.out.println(“Multiplication of values a and b ” + e); f = a / b; System.out.println(“Division of values a and b” + f); g = a % b; System.out.println(“Modulus of values a and b ” + g); a++; System.out.println(“After increamenting the value of a is ” + a); b–; System.out.println(“After decreamenting the value of b is ” + b); } } |

OUTPUT :

Java Arithmetic Operator Example

Assignment Operators : Assignment Operator is used to store the values of the right side variable to left side of the variable.

Assignment ‘=’ operand : Assignment operator assigns the values of the right hand side to left hand side variable.

1 2 3 4 5 6 7 8 9 10 11 12 |
public class javaAssignmentOperators { static int weight= 80;// the value of weight is assigned with 80 static String name = “qacreators”; // name variable gets assigned to qacreators static boolean exam = true; // exam variable gets assigned to boolean value true public static void main(String[] args) { System.out.println(“the value available in weight variable is : ” + weight); System.out.println(“the value available in name variable is : ” + name); System.out.println(“the value available in name variable is :” + exam); } } |

OUTPUT :

JavaAssignmentOperatorOutput

Logical Operators : Logical operators are used to store the values based on the variables, normally it stores only Boolean values such as true or false.

And Operator ‘&&’in java : Logical And operator returns true if the outcome of both the operands are true

OR Operator ‘||’ in java : Logical OR operator returns true if the outcome of either operands are true

NOT Operator ‘!’in java : Logical Not operator gives the invertion state of the condition

Example :

1 2 3 4 5 6 7 8 9 10 11 12 |
public class javaLogicalOperators { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println(“a & b : ” + (a & b)); System.out.println(“a && b : ” + (a && b)); System.out.println(“a | b : ” + (a | b)); System.out.println(“a || b: ” + (a || b)); System.out.println(“a ^ b : ” + (a ^ b)); System.out.println(“!a : ” + (!a)); } } |

OUTPUT :

JavaLogicalOperatorOutput

Relational Operator : Relational operators are used to find the comparsion between two or more objects.

Greater than ‘> ’ operand : Greater than operator validates if the value of left hand operand is greater than the value of right hand operand, then condition satisfies which will return true.

Less than ‘<‘ operand : Less than operator checks if the value of left operand is less than the value of right operand. , then condition satisfies which will return true.

Greater than or Equal to ‘>= ’ operand : Greater than or equal to operand checks if the value of left operand is greater than or equal to the value of right operand then condition satisfies which will return true.

Less than or Equal to ‘<=’ operand : Less than or equal to operand checks if the value of left operand is less than or equal to the value of right operand then condition satisfies which will return true.

Equal ‘==’ operand:Equal operand checks if the value of both operands are equal then condition satisfies which will return true.

Not Equal ‘!=’ operand : Not Equal operand checks if the value of two operands are not equal, which will return true if both the values are not equal.

Example :

1 2 3 4 5 6 7 8 9 10 |
public class javaRelationalOperators { public static void main(String args[]){ int a = 10, b = 5; System.out.println(“check whether a > b : “+(a > b)); System.out.println(“check whether a < b : “+(a < b)); System.out.println(“check whether a>= b : “+(a >= b)); System.out.println(“check whether a <= b : “+(a <= b)); System.out.println(“check whether a == b : “+(a == b)); System.out.println(“check whether a != b : “+(a != b)); } } |

OUTPUT :