Java Tutorial for Beginner # 2 : Basic Operators

Operators are symbols used in Java to preform Operations. Operators are used to manipulate the value of Operands.

Types of Operators

  • Arithmetic Operators = Used to perform mathematical operations like addition, subtraction, multiplication and division.
  • Relational operators  = Used to compare values. Greater than, Less than, Equal to. These operations return a Boolean.
  • Logical operators = Used for And, OR,and Not operations.
  • Bitwise operators = Used to preform bit by bit operations.
  • Assignment operators = Used to assign values to variables.

Arithmetic Operators

Arithmetic Operators Description
+ Addition: add two variables
Subtraction: subtract two variables
/ Division: divide two variables
* Multiplication: multiply two variables  
% Modules: dived two variables and get remainder
Example
import java.util.*; 
class Arithmetic{ 
      
    public static void main(String args[]) 
    { 
      
    int x = 25, y = 5, example;  
   
   //addition 
    example = x+y; 
    System.out.println("x+y is "+example); 
   
   //subtraction
    example = x-y;  
    System.out.println("x-y is "+example); 
   
   //multiplication
    example = x*y; 
    System.out.println("x*y is "+example); 
   
   //division
    example = x/y;  
    System.out.println("x/y is "+example); 
   
   //modulus
    example = x%y;  
    System.out.println("x%y is "+example); 
      
    } 
} 
Output :
x+y is 30
x-y is 20
x*y is 125
x/y is 5
x%y is 0

Relational operators

Relational Operators Description
== Equal to: True if both operands are equal
!= Not Equal to: True if operands are not equal
Greater than: True if left operand is greater than right
Less than: True if left operand is less than right
>= Greater than or Equal to: True if left operand is greater than or equal to right
<= Less than or Equal to: True if left operand is less than or equal to right
++ Incremental
Decrement
Example
import java.util.*; 
class Operators{ 
      
    public static void main(String args[]) 
    { 
      
    int x=10+ 5, y=4/2; 
   
   
    if (x > y) 
        System.out.println("x is greater than y"); 
    else System.out.println("x is less than y"); 
   
    // greater than equal to 
    if (x >= y) 
        System.out.println("x is greater than or equal to y"); 
    else System.out.println("x is less than y"); 
   
    // less than example 
    if (x < y) 
        System.out.println("x is less than y"); 
    else System.out.println("x is greater than or equal to y"); 
   
    // lesser than equal to 
    if (x <= y) 
        System.out.println("x is less than or equal to y"); 
    else System.out.println("x is greater than y"); 
   
    // equal to 
    if (x == y) 
        System.out.println("x is equal to y"); 
    else System.out.println("Not equal"); 
   
    // not equal to 
    if (x != y) 
        System.out.println("x and y are Not equal"); 
    else System.out.println("x is equal y"); 
   
          
    } 
} 
Output :
x is greater than y
x is greater than or equal to y
x is greater than or equal to y
x is greater than y
Not equal
x and y are Not equal

Logical operators

Logical Operators Description
AND True if both operands are true
OR True if either of the operands are true
NOT True of operand is False

Bitwise operators

Bitwise Operators Description
& Bitwise AND
~ Bitwise NOT
| Bitwise OR
^ Bitwise XOR
<<  Bitwise Left
>>  Bitwise Right

Assignment operators

Assignment Operators Description
= Assign vale to right side
+= Add right and left operand
-= Subtract right and left operand
%= Takes modulus
/= Divide left and right operand
*= Multiply operands
public class EX {

   public static void main(String args[]) {
      int x = 20 + 5;
      int y = 40 / 5;
      int z = 5;
//Assignment Operator Examples      
      z = x + y;
      System.out.println("Solution: z = x + y = " + z );

      z += x ;
      System.out.println("Solution: z += x  = " + z );

      z -= x ;
      System.out.println("Solution: z -= x = " + z );

      z *= x ;
      System.out.println("Solution: z *= x = " + z );

      z /= x ;
      System.out.println("Solution: z /= x = " + z );

      z %= x ;
      System.out.println("Solution: z %= x  = " + z );
      
// Bitwise Operator Examples
      z <<= 3 ;
      System.out.println("Solution: z <<= 3 = " + z );

      z >>= 5 ;
      System.out.println("Solution: z >>= 5 = " + z );

      z &= x ;
      System.out.println("Solution: z &= x  = " + z );

      z ^= x ;
      System.out.println("Solution: z ^= x   = " + z );

      z |= x ;
      System.out.println("Solution: z |= x   = " + z );
   }
}
Output :
Solution: z = x + y = 33
Solution: z += x  = 58
Solution: z -= x = 33
Solution: z *= x = 825
Solution: z /= x = 33
Solution: z %= x  = 8
Solution: z <<= 3 = 64
Solution: z >>= 5 = 2
Solution: z &= x  = 0
Solution: z ^= x   = 25
Solution: z |= x   = 25
Click here for Part Three : If Statements