Java Programming 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 Programming 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 Programming Gets Current Date

Java Programming Finds Character Vowel or Consonent

Java Programming HCF and LCM Computation

Java Programming Sum of Command Line Integer Arguments

Java Programming Multiplication of Command Line Integer Arguments

Java Programming Multiplication of Command Line Floating Point Numeric Arguments

Java Programming String Contains or Not

Java Programming Gets Grade Description using Switch

Java Programming CSV File Reader

Java Programming Character Frequency Count in String using for each

Java Programming Finds Min from Integer Array by Passing Array to Method

Java Programming Linear Search

Java Programming Binary Search

Java Programming Ternary Search

Java Programming Generates Range of Numbers

Java Programming Generates Range of Characters

Java Programming Computes Square Root Value

Java Programming Checks Number is Positive or Negative

Java Programming Checks Number is Odd or Even

Java Programming Computes Plot Area

Java Programming Convert Number of Days into Years

Java Programming Checks a Year is Leap Year, Century Year or Not

Java Programming Checks a Character is Digit, Letter or neither digit nor letter

Java Programming Checks a Number is Palindrome or not

Java Programming Sum of Two Matrix

Java Programming Power Computation

Java Programming Checks Number is an Armstrong Number or not

Java Programming Temperature Unit Conversions

Java Programming Generates Random Numbers in Specified Range

Java Programming Computes Sum of Digits and Product of Digits

Java Programming Computes Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Programming Computes Harmonic Series

Java Programming Generates Floyd's Triangle

Java Programming Reverses String

Java Programming Checks Palindrome String or not

Java Programming Opens Notepad

Java Programming Searches String using RegEx Pattern

Java Programming Searches Word in String

Java Programming Gets System Environment Variables

Java Programming Gets IP Address of Server Name

Java Programming Arrays Sort and Reverse using sort method

Java Programming Bubble Sorting

Java Programming Selection Sorting

Java Programming Insertion Sorting

Java Programming Merge Sorting

Java Programming Quick Sorting

Java Programming Counting Sort

Java Programming Radix Sorting

Java Programming Sorting Array of Strings

Java Programming String Characters Sorting

Java Programming Sum of First N Numbers

Java Programming Product of First N Numbers

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^