Java Operators

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:

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operators
  4. Relational Operators
  5. Logical Operators
  6. Ternory Operators
  7. Bitwise Operators
  8. Shift Operators
  9. Instance of Operators
  10. Precedence and Associativity

Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.

  • * : Multiplication
  • / : Division
  • % : Modulo
  • + : Addition
  • – : Subtraction
// Java program to illustrate
// arithmetic operators
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";
  
        // + and - operator
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
  
        // + operator if used with strings
        // concatenates the given strings.
        System.out.println("x + y = " + x + y);
  
        // * and / operator
        System.out.println("a * b = " + (a * b));
        System.out.println("a / b = " + (a / b));
  
        // modulo operator gives remainder
        // on dividing first operand with second
        System.out.println("a % b = " + (a % b));
  
        // if denominator is 0 in division
        // then Arithmetic exception is thrown.
        // uncommenting below line would throw
        // an exception
        // System.out.println(a/c);
    }
}
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.
// Java program to illustrate
// unary operators
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;
  
        // pre-increment operator
        // a = a+1 and then c = a;
        c = ++a;
        System.out.println("Value of c (++a) = " + c);
  
        // post increment operator
        // c=b then b=b+1
        c = b++;
        System.out.println("Value of c (b++) = " + c);
  
        // pre-decrement operator
        // d=d-1 then c=d
        c = --d;
        System.out.println("Value of c (--d) = " + c);
  
        // post-decrement operator
        // c=e then e=e-1
        c = e--;
        System.out.println("Value of c (e--) = " + c);
  
        // Logical not operator
        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 Opera

tors:

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:
10

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.
// Java program to illustrate
// relational operators
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;
  
        // various conditional operators
        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));
  
        // Arrays cannot be compared with
        // relational operators because objects
        // store references not the value
        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.
// Java program to illustrate
// logical operators
  
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();
  
        // Check if user-name and password match or not.
        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.








Post a Comment

0 Comments