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
ARITHMATIC OPERATORS |
Arithmetic operators are used to perform common mathematical operations.
1. ADDITION
Adds together two value.
Eg: X+Y.
package mypackage;
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
RELATIONAL OPERATORS |
Relational operators returns Boolean value
Comparison operators are used to compare two values:
1. Equal to
Eg: X==Y
EXAMPLE:
2.. Not Equal to
Eg: X!=Y
EXAMPLE:
3. Greater then
Eg: X>Y
EXAMPLE:
4. Less then
Eg: X<Y
EXAMPLE:
5. Less then or Equal to
Eg: X<=Y
EXAMPLE:
6. Greater then or Equal to
Eg: X>=Y
EXAMPLE:
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 mypackage;
public class operators {
public static void main (String[]args] {
boolean a = true;
boolean b = false;
System.out.printIn(a && b);
System.out.printIn(a || b);
}
}
OUTPUT:
FalseTrue
EXAMPLE 2:
package mypackage;
public class operators {
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 (<<)
EXAMPLE:
package mypackage;
public class operators {
public static void main (String[]args] {
int a = 10;
System.out.printIn(++a);
System.out.printIn(++a);
}
>> Signed right shift
>>> Unsigned right shift
UNARY OPERATORS
INCREMENT
Increases the value of a variable BY 1.
Eg; ++X.
EXAMPLE:
package mypackage;
public class operators {
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 mypackage;
public class operators {
public static void main (String[]args] {
int a = 10;
System.out.printIn(--a);
System.out.printIn(--a);
}
}
OUTPUT:
98
UNARY OPERATORS ~ AND !
package mypackage;
public class operators {
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