OPERATORS IN JAVA
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:
Java divides the operators into the following groups:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Unary Operators
- Ternary Operators
ARITHMATIC OPERATORS |
Arithmetic operators are used to perform common mathematical operations.
1. ADDITION
Adds together two value.
Eg: X+Y.
package operators; public class Arithmaticperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a+b); } } |
OUTPUT: 30 |
package operators; public class ArithmaticOperators { public static void main (String[]args] { int a = 100; int b = 20; System.out.printIn(a-b); } } |
OUTPUT: 80 |
package operators; public class ArithmaticOperators { public static void main (String[]args] { int a = 10; int b = 20; int c = a*b; System.out.printIn(c); } } |
OUTPUT: 200 |
package operators; public class ArithmaticOperators { public static void main (String[]args] { int a = 10; int b = 100; int c = a/b; System.out.printIn(c); } } |
OUTPUT: 10 |
package operators; public class ArithmaticOperators { public static void main (String[]args] { int a = 10; int b = 1000; int c = a%b; System.out.printIn(c); } } |
OUTPUT: 1 |
package operators; public class ArithmaticOperators { public static void main (String[]args] { System.out.printIn(10+10-5*2/15); } } |
OUTPUT: 20 |
package operators; import java.util.Scanner; public class ArithmaticScanner{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("input the first number: "); int a = input.nextInt(); System.out.println("input the second number: "); int b = input.nextInt(); System.out.println(" Addition of a and b is : " + a+b); } } |
OUTPUT: input the first number: 10 input the second number: 20 Addition of a and b is : 30 |
ASSIGNMENT OPERATORS |
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: A list of all assignment operators:
OPOERATOR EXAMPLE
= X=5
+= X+=5
-= X-=3
*= X*=3
%= X%=3
&= X&=3
|= X|=3
^= X^=3
>>= X>>=3
<<= X<<=3
package operators; public class AssinmentOperators { public static void main (String[]args] { int a = 10; a=10; System.out.printIn(a); a+=3; System.out.printIn(a); a-=4; System.out.printIn(a); a*=2; System.out.printIn(a); a/=2; System.out.printIn(a); } } |
OUTPUT: 10 13 9 18 9 |
package operators; public class AssignmentOperators { public static void main (String[]args] { short a = 10; short b = 20; c=a+b; System.out.printIn(a); } } |
OUTPUT: Error |
package operators; public class CasteOperators { public static void main (String[]args] { short a = 10; short b = 20; c =(short ) (a*b); System.out.printIn(c); } } |
OUTPUT: 200 |
RELATIONAL OPERATORS |
Relational operators returns Boolean value
Comparison operators are used to compare two values:
1. Equal to
Eg: X==Y
EXAMPLE:
package operators; public class RelationalOperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a==b); } } |
OUTPUT: False |
2.. Not Equal to
Eg: X!=Y
EXAMPLE:
package operators; public class RelationalOperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a!=b); } } |
OUTPUT: True |
3. Greater then
Eg: X>Y
EXAMPLE:
package operators; public class RelationalOperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a>b); } } |
OUTPUT: False |
4. Less then
Eg: X<Y
EXAMPLE:
package operators; public class RelationalOperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a<b); } } |
OUTPUT: True |
5. Less then or Equal to
Eg: X<=Y
EXAMPLE:
package operators; public class RelationalOperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a<=b); } } |
OUTPUT: True |
6. Greater then or Equal to
Eg: X>=Y
EXAMPLE:
package operators; public class RelationalOperators { public static void main (String[]args] { int a = 10; int b = 20; System.out.printIn(a>=b); } } |
OUTPUT: False |
LOGICAL OPETATORS |
- Logical operators are used to determine the logic between variables or values:
- logical && operators doesn't check second condition if first condition is fail.it checks second condition only if first one is true.
- Logical || operator doesn't check second condition if first condition is true. It checks if second condition only if first one is fail.
EXAMPLE 1:
package operators;
public class LogicalOperators {
public static void main (String[]args] {
boolean a = true;
boolean b = false;
System.out.printIn(a && b);
System.out.printIn(a || b);
}
}
OUTPUT: False True |
EXAMPLE 2:
package operators;
public class LogicalOperators {
public static void main (String[]args] {
int a = 10;
int b = 20;
System.out.printIn(a>b && a<b);
System.out.printIn(a>b || a<b);
}
}
OUTPUT:
FalseTrue
BIT WISE OPERATORS |
~ Unary bitwise complement
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
SHIFT OPERATORS
SIGNED LEFT SHIFT (<<)
Java left shift operator
Left shift Operator << is used to shift all of the bits in a value to the left side of a specified number of times.
EXAMPLE:
package operators;
public class ShiftOperators {
public static void main (String[]args] {
System.out.printIn(10<<2); // 10X2^2
System.out.printIn(15<<4); // 15X4^2
}
}
OUTPUT:
40240
SIGNED RIGHT SHIFT (>>)
Java Right shift operator
Right shift Operator >> is used to move left Operator value to right by the No.of.bits specified by the right oprand
EXAMPLE:
package operators;
public class ShiftOperators {
public static void main (String[]args] {
System.out.printIn(40>>2); // 40/2^2
System.out.printIn(32>>4); // 15/4^2
}
}
OUTPUT:
102
UNSIGHNED SHIFT (<<<)
EXAMPLE:
package operators;
public class ShiftOperators {
public static void main (String[]args] {
}
}
OUTPUT:
UNARY OPERATORS
Java Unary operator required one operand. unary operators are used to perform various operators like
- Increment / Decrement
- Negating an expression
- Inverting the value of boolean
INCREMENT
Increases the value of a variable BY 1.
Eg; ++X.
EXAMPLE:
package operators;
public class UnaryOperators {
public static void main (String[]args] {
int a = 10;
System.out.printIn(++a);
System.out.printIn(++a);
}
}
OUTPUT:
1112
DECREMENT
Decreases the value of a variable by 1.
Eg: --X.
EXAMPLE:
package operators;
public class UnaryOperators {
public static void main (String[]args] {
int a = 10;
System.out.printIn(--a);
System.out.printIn(--a);
}
}
OUTPUT:
98
UNARY OPERATORS ~ AND !
package operators;
public class UnaryOperators {
public static void main (String[]args] {
int a = 10; int b = -10;
boolean c = true; boolaean d= false;
System.out.printIn(~a); System.out.printIn(~b); System.out.printIn(!c); System.out.printIn(!d);
}
}
OUTPUT:
-119falsetrue
TERNARY OPERATORS
Ternary operators are also known as Short hand if else because it contains of three operands it can be used to replace multiple lines of code with a single line .
SYNTAX:
variable = (condition )?ExpressionTrue:ExpressionFalse;
EXAMPLE:
package operators;
public class Arithmaticperators {
public static void main (String[]args] {
int age = 20;
String result = (age>18)?"Eligible" : "Not-Eligible"; System.out.printIn(result);
}}
OUTPUT:
Eligible