« Previous Page Next Page »
    

Introduction to Java Operators.

POSTED

AUTHOR: VARUN SINGH

In this tutorial, you are going to learn Java Operators used in Java programming language. Java provides many types of operators which can be used according to the need.

They are classified based on the functionality they provides.


  • 1. Arithmetic Operators
  • 2. Unary Operators
  • 3. Assignment Operators
  • 4. Relational Operators
  • 5. Logical Operators
  • 6. Bitwise Operators



  • 1. Java Arithmetic Operators:

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

  • • Multiplication: ( * )
  • • Division: ( / )
  • • Modulus: ( % )
  • • Addition: ( + )
  • • Subtraction: ( - )
  • Example:

    // Java program to illustrate arithmetic operators
    
    public class Arithmetic
    {
        public static void main(String[] args) 
        {
        
            int a = 20; 
            int b = 10;
            String x = "Coder";
            String y = "Glass";
     
            // + and - operator
            
            System.out.println("a + b = "+(a + b));
            System.out.println("a - b = "+(a - b));
     
            // concatenates the given strings using + operator.
            
            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
            
            System.out.println("a % b = "+(a % b));
     
           
        }
    }
    

    Output:

    a + b = 30
    a - b = 10
    x + y = CoderGlass
    a * b = 200
    a / b = 2
    a % b = 0
    



    2. Java Unary Operator:

    Unary operators needs only one operand. They are used to increment, decrement or negate a value.

  • • Unary Minus (-): For decreasing the value
  • • Unary Plus (+): For converting the negative values into positive
  • • Increment operator (++): Used for increasing of the operand by 1
  • • Post-Increment: The value is incremented first then the result is computed
  • • Pre-Increment: The value is incremented later then the result is computed
  • • Decrement Operator: Used for decreasing the value of operand by 1
  • • Post-Decrement: The value is decremented first then the result is computed
  • • Pre-Decrement: The value is decremented later then the result is computed
  • • Logical not Operator (!): Used for inverting the values of boolean
  • Example:

    public class Unary
    {
       public static void main(String[] args)
       {
        int a = 30, b = 20, c = 0, d = 30, e = 50;
        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) = 31
    Value of c (b++) = 20
    Value of c (--d) = 29
    Value of c (--e) = 49
    Value of !condition = false
    



    3. Java Assignment Operator:

    Assignment operators are used to assigning values to the left operand.

    In many cases assignment operator can be combined with other operators to build a shorter version of statement called Compound Statement.

    += : To add the right and left operator and then assigning the result to the left operator

    -= : To subtract the two operands on left and right and then assign the value to the left operand

    *= : To multiply the two operands on left and right and then assign the value to the left operand

    /= : To divide the two operands on left and right and then assign the value to the left operand

    ^= : To raise the value of left operand to the power of right operator

    %= : To apply modulo operator

    Example:

    // Java program to illustrate assignment operators
    public class Assignment
    {
        public static void main(String[] args) 
        {
            int a = 20, b = 10, c, d, e = 10, f = 4;
     
            // simple assignment operator
            c = b;
            System.out.println("Value of c = " + c);
     
            
            // c must be initialised before assignment,
            //  otherwise program would not compile. 
            
            // assignment operators 
            a = a + 1;
            b = b - 1;
            e = e * 2;
            f = f / 2;
            System.out.println("a,b,e,f = " + a + ","
                               + b + "," + e + "," + f);
            a = a - 1;
            b = b + 1;
            e = e / 2;
            f = f * 2;
     
            // shorthand assignment operator
            a += 1;
            b -= 1;
            e *= 2;
            f /= 2;
            System.out.println("a,b,e,f (using shorthand operators)= " + 
                                a + "," + b + "," + e + "," + f);
        }
    }
    

    Output:

    Value of c = 10
    a,b,e,f = 21,9,20,2
    a,b,e,f (using shorthand operators)= 21,9,20,2
    



    4. Java Relational Operators:

    These operators are used to check for relations like equality, greater than, less than. These operators are extensively used in looping (for, while) loop statements as well as conditional (if else ) statements.

    • == (equals to) ex. x==y True if x equals y, otherwise false

    • != (not equal to) ex. x!=y True if x is not equal to y, otherwise false

    • < (less than) ex. x

    • > (greater than) ex.x > y True if x is greater than y, otherwise false

    • >= (greater than or equal to) ex. x>=y True if x is greater than or equal to y,         otherwise false

    • <= (less than or equal to) ex. x<=y True if x is less than or equal to y, otherwise     false

    Example:

    // Java program to illustrate relational operators
    
    public class Relational 
    {
        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));
     
            System.out.println("x == y : " + (ar == br));
     
            System.out.println("condition==true :" + (condition == true));
        }
    }
    

    Output:

    a == b :false
    a < b :false
    a <= b :false
    a > b :true
    a >= b :true
    a != b :true
    x == y : false
    condition==true :true
    



    5. Java Logical Operators:

    These operators are used to perform "logical AND" and "logical OR" operation.

    • && (Logical AND) : returns true when both conditions are true.

    • || (Logical OR) : returns true if at least one condition is true.

    Example:

    // Java program to illustrate logical operators
    
    import java.util.Scanner;  
    public class Logical 
    {
        public static void main(String args[]) 
        {
            String x = "Varun";
            String y = "Singh";
     
            Scanner s = new Scanner(System.in);
            System.out.print("Enter Your First Name:");
            String fname = s.next();
            System.out.print("Enter Your Last Name:");
            String lname = s.next();
     
            // Check if First Name and Last Name match or not.
            
            if ((fname.equals(x) && lname.equals(y)) || 
                    (fname.equals(y) && lname.equals(x))) {
                System.out.println("Welcome " + " " + fname + " " + lname);
            } else {
                System.out.println("Wrong First or Last Name");
            }
     
        }
    }
    

    Output: If condition is true:

    Welcome  Varun Singh
    

    Output: If condition is false:

    Wrong First or Last Name
    



    6. Java Bitwise Operators:

    Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

    Java Bitwise AND operator (&): The & operator compares corresponding bits of two     operands. If both bits are 1, it gives 1 else 0.

    Java Bitwise OR operator (|): The | operator compares corresponding bits of two        operands. If either of the bits is 1, it gives 1 else 0.

    Bitwise XOR operator (^): The ^ operator compares corresponding bits of two         operands. If corresponding bits are different, it gives 1 else 0.

    Bitwise Complement operator (~): The ~ operator inverts the bit pattern. It makes     every 0 to 1 and every 1 to 0.

    Example:

    // Java program to illustrate bitwise operators
    
    public class Bitwise 
    {
        public static void main(String[] args) 
        {
     
            int a = 0x0005;
            int b = 0x0007;
     
            // bitwise and  0101 & 0111=0101
     
            System.out.println("a&b = " + (a & b));
     
            // bitwise and 0101 | 0111=0111
         
            System.out.println("a|b = " + (a | b));
     
            // bitwise xor 0101 ^ 0111=0010
        
            System.out.println("a^b = " + (a ^ b));
     
            // bitwise and ~0101=1010
          
            System.out.println("~a = " + ~a);
     
            // assignment operator to provide shorthand assignment a=a&b
            
            a &= b;
            System.out.println("a= " + a);
        }
    }
    

    Output:

    a&b = 5
    a|b = 7
    a^b = 2
    ~a = -6
    a= 5
    


    The End:

    Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

    If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

    « Previous Page Next Page »

    Comments: