Java Basic Operators

Java provides a rich operator environment to write the program.

What are the different types of operator in java?

  • Arithmetic Operators
  • Bitwise Operators
  • Relational Operators
  • Logical Operators

Java also defines some additional operators for certain special cases.

  • Arithmetic Operators: are used in mathematical expressions in the java code.

    What are the arithmetic operators in java?

    Operator Description
    + Addition
    - Subtraction or unary minus
    * Multiplication
    / Division
    % Modulus
    ++ Increment
    -- Decrement
    += Addition assignment
    -= Subtraction assignment
    *= Multiplication assignment
    /= Divison assignment
    %= Modulus assignment

    Example Program for arithmetic operators in java:
    // Arithmetic operations in java                                                
    class ArithmeticTest {                                                          
     public static void main(String args[]) {                                       
      int a = 5, b = 4, c = 2;                                                      
                                                                                    
      System.out.println("Addition of " + a + " and " + b + ": " + (a + b));        
      System.out.println("Subtraction of " + a + " and " + b + ": " + (a - b));     
      System.out.println("Multiplication of " + a + " and " + b + ": " + (a * b));  
      System.out.println("Division of " + a + " and " + b + ": " + ((double)a / (double)b));
      System.out.println("Modulus of " + a + " and " + b + ": " + (a % b));         
      //Increment by 1                                                              
      int d = ++a;                                                                  
      //Decrement by 1                                                              
      int e = --b;                                                                  
      //Addition assignment                                                         
      a += 1;                                                                       
      // Subtraction assignment                                                     
      b -= 1;                                                                       
      System.out.println("Increment of a:" + d);                                    
      System.out.println("Decrement of b:" + e);                                    
      System.out.println("Addition assignment of a:" + a);                          
      System.out.println("Subtraction assignment of b:" + b);                       
     }                                                                              
    }     
    
    Output:
    Addition of 5 and 4: 9
    Subtraction of 5 and 4: 1
    Multiplication of 5 and 4: 20
    Division of 5 and 4: 1.25
    Modulus of 5 and 4: 1
    Increment of a:6
    Decrement of b:3
    Addition assignment of a:7
    Subtraction assignment of b:2
    
  • Bitwise Operators: can be applied to the integer types: long, int, short and byte.
    These operators work upon the individual bits of their operands.

    What are the bitwise operators in java?

    Operator Description (Bitwise)
    ~ Unary NOT
    & Unary AND
    | Unary OR
    ^ Exclusive OR (XOR)
    >> Shift right
    >>> Shift right fill
    << Shift left
    &= AND assignment
    |= OR assignment
    ^= exclusive OR assignment
    >>= Shift right assignment
    >>>= Shift right zero fill assignment
    <<= Shift left assignment

    Example program for bitwise operators in java:
    //Bitwise  operations in java                                                   
    class BitwiseOperatorsTest {                                                    
     public static void main(String args[]) {                                       
      int a = 5, b = 4, c = 2;                                                      
                                                                                    
      System.out.println("Bitwise " + a + " AND " + b + ": " + (a & b));            
      System.out.println("Bitwsie of " + a + " OR " + b + ": " + (a | b));          
      System.out.println("Bitwise of " + a + " XOR " + b + ": " + (a ^ b));         
      System.out.println("Bitwise Unary NOT of" + a +": " + ~a);                    
      System.out.println("Shift right by 2 of " + a + " : " + (a >> 2));            
      //Shift right zero fill                                                       
      int d = a >>> 2;                                                              
      //Shift left by 3                                                             
      int e = b << 3;                                                               
      //Bitwise AND assignment                                                      
      a &= 4;                                                                       
      // Shift right assignment                                                     
      b >>= 3;                                                                      
      System.out.println("Shift right zero fill of " + a + " by 2:" + d);           
      System.out.println("Shift left by 3 of "+ b +": " + e);                       
      System.out.println("Bitwise AND assignment of a  and 4:" + a);                
      System.out.println("Shift right assignment of b by 3:" + b);                  
     }                                                                              
    }   
    
    Output:
    Bitwise 5 AND 4: 4
    Bitwsie of 5 OR 4: 5
    Bitwise of 5 XOR 4: 1
    Bitwise Unary NOT of5: -6
    Shift right by 2 of 5 : 1
    Shift right zero fill of 4 by 2:1
    Shift left by 3 of 0: 32
    Bitwise AND assignment of a  and 4:4
    Shift right assignment of b by 3:0
    
  • Relational Operators: Outcome of these operators is a boolean value. Relational operations are mostly used in expressions that control the control statements and various looping statements. Mostly used for comparison.

    What are the relational operators in java?

    Operator Description
    == Equal to
    != Not equal to
    > Greater than
    < Less than
    >= Greater than or equal to
    <= Less than or equal to

    Example program for relational operators in java:
    //Relational operators in java                                                  
    class RelationalOperatorsTest {                                                 
     public static void main(String args[]) {                                       
      int a = 5, b = 4;                                                             
                                                                                    
      boolean c = a > b;                                                            
                                                                                    
      System.out.println(a + " is greater than  " + b +": " + c);                   
                                                                                    
      if (a == 5) {                                                                 
       System.out.println("a value is 5");                                          
      }                                                                             
      if (b != 2) {                                                                 
       System.out.println("b values is not 2");                                     
      }                                                                             
     }                                                                              
    }  
    
    Output:
    5 is greater than  4: true
    a value is 5
    b values is not 2
    
  • Boolean Logical Operators: These operators are only for boolean operands.

    What are the logical operators in java?

    Operator Description
    & Logical AND
    | Logical OR
    ^ Logical XOR(exclusive OR)
    || Short-circuit OR
    && Short-circuit AND
    ! Logical Unary NOT
    &= AND assignment
    |= OR assignment
    ^= XOR assignment
    == Equal to
    != Not equal to
    ?: Ternary if-else

    Example program for logical operators in java:
    //Logical operators in java                                                     
    class LogicalOperatorsTest {                                                    
     public static void main(String args[]) {                                       
      boolean a = true;                                                             
      boolean b = false;                                                            
                                                                                    
      boolean c = a & b;                                                            
      boolean d = a | b;                                                            
      boolean e = a ^ b;                                                            
      boolean f = !a;                                                               
                                                                                    
      System.out.println("    a: " + a);                                            
      System.out.println("    b: " + b);                                            
      System.out.println("a & b: " + c);                                            
      System.out.println("a | b: " + d);                                            
      System.out.println("a ^ b: " + e);                                            
      System.out.println("   !a: " + f);                                            
                                                                                    
      // Short circuit AND                                                          
      if (a && b) {                                                                 
       System.out.println("Both a and b are not true");                             
      }                                                                             
      //Short circuit OR                                                            
      if (a || b) {                                                                 
       System.out.println("Either a or b is true");                                 
      }                                                                             
     }                                                                              
    }   
    
    Output:
        a: true
        b: false
    a & b: false
    a | b: true
    a ^ b: true
       !a: false
    Either a or b is true
    
  • Assignment Operators: assignment operator in java is single equal sign (=).
    // here var_name must be compatible with the type of expression.
    var_name = expression;
    
    Example:
    int a, b, c;
    
    // Assigning 205 value for variable a, b and c.
    a = b = c = 205;
    
  • What is ternary operator?

    Ternary Operator ( ? operator): Java has a special ternary(three way) operator that can replace if-else statements for certain types.
    expression1?:expression2:expression3
    
    Example:
    int a, b, c;
    
    a = 5;
    b = 2;
    
    //Assigns 10 to variable c if a is greathan b, otherwise assigns 20 to variable c.
    c = (a > b)? 10 : 20;
    
Java Examples

GCD and Rational Number Java Program

Java Queue Program using exception

Java Stack Program using exception

Addition of three integers java program

Biggest of three integers java program

Fibonacci numbers java program

Arithmetic Operations Menu java program

Second Smallest Element In Array Java Program

Transpose Of A Matrix Java Program

Java Program to Display triangle of stars (*)

Java Programming Prints Product Tables

Java Program to Display triangle of numbers

Java Program to Get Current Date

Java Program to Find Character Vowel or Consonent

Java Program to Compute HCF and LCM

Java Program to Sum the Command Line Integer Arguments

Java Programm to Multiply the Command Line Integer Arguments

Java Program to Find Multiplication of Command Line Floating Point Numeric Arguments

Java Program to Check String Contains or Not

Java Program to Get Grade Description using Switch

Java Program for CSV File Reader

Java Program to Find Character Frequency Count in String using for each

Java Program to Find Min from Integer Array by Passing Array to Method

Java Program Linear Search

Binary Search Java Program

Ternary Search Java Program

Java Program to Generate Range of Numbers

Java Program to Generate Range of Characters

Java Program to Compute Square Root Value

Java Program to Check Number is Positive or Negative

Java Program to Check Number is Odd or Even

Java Program to Compute Plot Area

Java Program to Convert Number of Days into Years

Java Program to Check a Year is Leap Year, Century Year or Not

Java Program to Check a Character is Digit, Letter or neither digit nor letter

Java Program to Check a Number is Palindrome or not

Java Program to Sum Two Matrix

Java Program to Compute Power

Java Program to Check Number is an Armstrong Number or not

Java Program for Temperature Unit Conversions

Java Program to Generate Random Numbers in Specified Range

Java Program to Compute Sum of Digits and Product of Digits

Java Program to Compute Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Program to Compute Harmonic Series

Java Program Generate Floyd's Triangle

Java Program to Reverse String

Java Program to Check Palindrome String or not

Java Program to Open Notepad

Java Program to Search String using RegEx Pattern

Java Program to Search Word in String

Java Program to Get System Environment Variables

Java Program to Get IP Address of Server Name

Java Program for Arrays Sorting and Reverse using sort method

Java Program for Bubble Sorting

Java Program for Selection Sorting

Java Program for Insertion Sorting

Java Program for Merge Sorting

Java Program for Quick Sorting

Java Program for Counting Sort

Java Program for Radix Sorting

Java Program for Sorting Array of Strings

Java Program for String Characters Sorting

Java Program to Sum First N Numbers

Java Program to Product First N Numbers

Java Program to get URL details

Java Program to get URL HTML Content

Java Program to get URL details

Java Program to get URL HTML Content

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^