Java Operators
Operators are used to perform operations on variables and values.
Operator in Java is a symbol which is used to perform operations. For example: +, -, *, / etc.
Java provides a rich set of operators to manipulate variables.
There are many types of operators in Java which are given below:
- Arithmetic Operators
- Unary Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Ternory Operators
- Bitwise Operators
- Shift Operators
- Instance of Operators
- Precedence and Associativity
Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.
- * : Multiplication
- / : Division
- % : Modulo
- + : Addition
- – : Subtraction
public class operators {
public static void main(String[] args)
{
int a = 20 , b = 10 , c = 0 , d = 20 , e = 40 , f = 30 ;
String x = "Thank" , y = "You" ;
System.out.println( "a + b = " + (a + b));
System.out.println( "a - b = " + (a - b));
System.out.println( "x + y = " + x + y);
System.out.println( "a * b = " + (a * b));
System.out.println( "a / b = " + (a / b));
System.out.println( "a % b = " + (a % b));
}
}
Output:
a + b = 30
a - b = 10
x + y = ThankYou
a * b = 200
a / b = 2
a % b = 0
Unary Operators:
Unary operators need only one operand. They are used to increment, decrement or negate a value.
- – :Unary minus, used for negating the values.
- + :Unary plus, used for giving positive values. Only used when deliberately converting a negative value to positive.
- ++ :Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.
- Post-Increment : Value is first used for computing the result and then incremented.
- Pre-Increment : Value is incremented first and then result is computed.
- — : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operator.
- Post-decrement : Value is first used for computing the result and then decremented.
- Pre-Decrement : Value is decremented first and then result is computed.
- ! : Logical not operator, used for inverting a boolean value.
public class operators {
public static void main(String[] args)
{
int a = 20 , b = 10 , c = 0 , d = 20 , e = 40 , f = 30 ;
boolean condition = true ;
c = ++a;
System.out.println( "Value of c (++a) = " + c);
c = b++;
System.out.println( "Value of c (b++) = " + c);
c = --d;
System.out.println( "Value of c (--d) = " + c);
c = e--;
System.out.println( "Value of c (e--) = " + c);
System.out.println( "Value of !condition ="
+ !condition);
}
}
|
Output:Value of c (++a) = 21
Value of c (b++) = 10
Value of c (--d) = 19
Value of c (e--) = 40
Value of !condition =false
Java 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: public class MyClass {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
}
Output:
Relational Operators : These operators are used to check for relations like equality, greater than, less than. They return boolean result after the comparison and are extensively used in looping statements as well as conditional if else statements.
Some of the relational operators are- - ==, Equal to : returns true of left hand side is equal to right hand side.
- !=, Not Equal to : returns true of left hand side is not equal to right hand side.
- <, less than : returns true of left hand side is less than right hand side.
- <=, less than or equal to : returns true of left hand side is less than or equal to right hand side.
- >, Greater than : returns true of left hand side is greater than right hand side.
- >=, Greater than or equal to: returns true of left hand side is greater than or equal to right hand side.
public class operators {
public static void main(String[] args)
{
int a = 20 , b = 10 ;
String x = "Thank" , y = "Thank" ;
int ar[] = { 1 , 2 , 3 };
int br[] = { 1 , 2 , 3 };
boolean condition = true ;
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 != b :" + (a != b));
System.out.println( "x == y : " + (ar == br));
System.out.println( "condition==true :"
+ (condition == true ));
}
}
Output:a == b :false
a b :true
a >= b :true
a != b :true
x == y : false
condition==true :true Logical Operators :
These operators are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Conditional operators are-
- &&, Logical AND : returns true when both conditions are true.
- ||, Logical OR : returns true if at least one condition is true.
import java.util.*;
public class operators {
public static void main(String[] args)
{
String x = "Sher" ;
String y = "Locked" ;
Scanner s = new Scanner(System.in);
System.out.print( "Enter username:" );
String uuid = s.next();
System.out.print( "Enter password:" );
String upwd = s.next();
if ((uuid.equals(x) && upwd.equals(y))
|| (uuid.equals(y) && upwd.equals(x))) {
System.out.println( "Welcome user." );
}
else {
System.out.println( "Wrong uid or password" );
}
}
}
|
Output: Enter username:Sher
Enter password:Locked
Welcome user.
|
0 Comments