codescracker


java

Java Operators



« Previous Tutorial Next Tutorial »


Java provides a rich operator environment. Most of its operators can be divided into the following four groups :

You will learn all the Java operators in the following groups:

Let's discuss about all these operators one by one.

Java Arithmetic Operators

Arithmetic operators are used for mathematical expressions. You will learn in detail about arithmetic operators in separate chapter.

Java Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation. You will learn in detail about bitwise operator in separate chapter.

Java Relational Operator

Relational operator operates on relationship that one operand has to the other. You will learn in detail about relational operator in separate chapter.

Java Logical Operator

Boolean Logical operator operate only on boolean operands. You will learn in detail about logical operator.

Java Assignment Operators

There are following assignment operators in Java language:

Operator Name Meaning
= Simple Assignment Operator This operators is used to assign the values from the right side operands to the left side operand
-= Subtract and Assignment Operator This operator simply subtracts right operand from the left operand and assign the result to the left operand
+= Add and Assignment Operator This operator simply adds the right operand to the left operand and assign the result to the left operand
/= Divide and Assignment Operator This operator is simply divides the left operand with the right operand and assign the result to the left operand
*= Multiply and Assignment Operator This operator is simply multiplies the right operand with the left operand and assign the result to the left operand
%= Modulus and Assignment Operator This operator simply takes modulus using the two operands and assign the result to the left operand
>>= Right Shift and Assignment Operator This operator is used to shift right the value and then assign the result to the left operand
<<= Left Shift and Assignment Operator This operator is used to left shift the value and then assign the result to the left operand
^= Bitwise Exclusive OR and Assignment Operator This is a bitwise exclusive OR and assignment operator, takes the exclusive OR of the of the value and then assign the result to the left operand
&= Bitwise and Assignment Operator This is a bitwise AND and assignment operator, takes the bitwise AND of the value and then assign the result to the left operand
|= Bitwise Inclusive OR and Assignment Operator This is a bitwise inclusive OR and assignment operator, takes the inclusive OR of the value, and then assign the value to the left operand

Example

Look at the following example program which demonstrates the assignment operators:

/* Java Program Example - Java Operators
 * Java Assignment Operators
 */

public class JavaProgram
{ 
	public static void main(String args[])
	{
       
		int num1 = 10, num2 = 20, res = 0;

		res = num1 + num2;
		System.out.println("res = num1 + num2 = " + res);
	
		res += num1 ;
		System.out.println("res += num1  = " + res);
	
		res -= num1 ;
		System.out.println("res -= num1 = " + res);
	
		res *= num1 ;
		System.out.println("res *= num1 = " + res);
	
		num1 = 10;
		res = 15;
		
		res /= num1 ;
		System.out.println("res /= num1 = " + res);
	
		num1 = 10;
		res = 15;
		
		res %= num1 ;
		System.out.println("res %= num1  = " + res);
	
		res <<= 2 ;
		System.out.println("res <<= 2 = " + res);
	
		res >>= 2 ;
		System.out.println("res >>= 2 = " + res);
	
		res >>= 2 ;
		System.out.println("res >>= num1 = " + res);
	
		res &= num1 ;
		System.out.println("res &= 2  = " + res);
	
		res ^= num1 ;
		System.out.println("res ^= num1   = " + res);

		res |= num1 ;
		System.out.println("res |= num1   = " + res);
		
	}
}

Here is the sample output of the above Java program:

java operators

Java Misc Operators

There are also some other miscellaneous operators available in Java language.

Java Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator. You will learn in detail about ternary operator in separate chapter.

Java instanceof Operator

The instanceof operator in Java, used only for the object reference variables. This operator basically checks whether the object is of a specific type (class or interface type). Here is the general form to use instanceof operator in Java language:

(Object reference variable) instanceof  (class/interface type)

Example

/* Java Program Example - Java Operators
 * Java instanceof Operator
 */

public class JavaProgram
{ 
   public static void main(String args[])
   {
       
      String str_name = "James";
      
      boolean res = str_name instanceof String;  
      
      System.out.println(res);
      
   }
}

Here is the output produced by the above Java program:

java instanceof operator

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test