Operators in java
Operators are used to perform operations on different values and variables. An operator can also be used with different values, value and variable or with different variables. The following program shows arithmetic operators with different values and variables.
package com.company;
public class Operators {
public static void main (String[] args) {
int a = 7, b =4;
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));
int x = 5 + 4;
System.out.println("The addition of 5 + 4 is " + x);
int z = 7;
System.out.println("The addition of 3 + z is " +z);
}
}
Output:
a + b = 11
a - b = 3
a * b = 28
a / b = 1
a % b = 3
The addition of 5 + 4 is 9
The addition of 3 + z is 7
Logical operators are used in decision making. Logical operators are Logical AND, Logical OR and Logical NOT. Logical AND returns true only if all statements are true. Logical OR returns true if any one statement is true. Logical NOT returns the opposite result. The following program shows the use of logical operators.
package com.company;
public class Operators {
public static void main(String[] args) {
int x = 4;
int y = 3;
int z = 4;
System.out.println("Use of Logical AND");
System.out.println(x > y && y < z); //Both statements are true
System.out.println(x > y && y > z); //One statement is false
System.out.println(x < y && y > z); //Both statements are false
System.out.println("Use of Logical OR");
System.out.println(x > y || y < z); //Both statements are true
System.out.println(x > y || y > z); //One statement is false
System.out.println(x < y || y > z); //Both statements are false
System.out.println("Use of Logical NOT");
System.out.println(!(x > y && y < z)); //Both statements are true
System.out.println(!(x > y && y > z)); //One statement is false
System.out.println(!(x < y && y > z)); //Both statements are false
System.out.println(!(x > y || y < z)); //Both statements are true
System.out.println(!(x > y || y > z)); //One statement is false
System.out.println(!(x < y || y > z)); //Both statements are false
}
}
output:
Use of Logical AND
true
false
false
Use of Logical OR
true
true
false
Use of Logical NOT
false
true
true
false
false
true
Unary operators are used with a single operand. Unary operators are like increment operator- “++”, decrement operator- “–“, logical complement / not operator- “!”. There are also unary plus “+” and unary minus “-” unary operators.
Increment and decrement operators increases and decreases the value of an operand by 1. Increment operators are pre-increment and post-increment. In pre-increment the operands value is increased first and then displayed. In post-increment the operands value is displayed first and then increased. Decrement operators are also pre-decrement and post-decrement. In pre-decrement operands value is decreased first and then displayed. In post-decrement operands value is displayed first and then decreased. The following program shows the use of unary operators.
package com.company;
public class Operators {
public static void main(String[] args) {
int x = 4, y = 5, z = 3, m = 7, n = 11;
x = +x; //Unary plus
System.out.println("The value of integer x after using unary plus operator is " + x);
x = -x; //Unary minus
System.out.println("The value of integer x after using unary minus operator is " + x);
y = ++y; //pre-increment operator
System.out.println("The value of integer y after using pre increment operator is " +y);
z = --z; //pre-decrement operator
System.out.println("The value of integer z after using pre decrement operator is " +z);
//post-increment operator
System.out.println("The value of integer m after using post increment operator is " + m++);
System.out.println("The value of integer m after using post increment operator is " + m);
//post-decrement operator
System.out.println("The value of integer n after using post decrement operator is " + n--);
System.out.println("The value of integer n after using post decrement operator is " + n);
//Use of not operator
System.out.println("The value of 15>16 is false: " + !(15>16));
}
}
Output:
The value of integer x after using unary plus operator is 4
The value of integer x after using unary minus operator is -4
The value of integer y after using pre increment operator is 6
The value of integer z after using pre decrement operator is 2
The value of integer m after using post increment operator is 7
The value of integer m after using post increment operator is 8
The value of integer n after using post decrement operator is 11
The value of integer n after using post decrement operator is 10
The value of 15>16 is false: true
There are also other operators used in Java.