Image

Java - Core Java - Operator

Operator

It is special symbol which perform arithmetic and logical operation.
1. Arithmetic operator
2. Assignment operator
3. Increment/decrement operator
4. Relation operator
5. Condition operator
6. Logical operator
7. Bitwise operator
8. Instance of operator


1. Arithmetic operator

Java arithmetic operators +,-,*,/, and % these operator performs addition, subtraction, multiplication, division and modulus operator. Arithmetic operator can be applied on any numeric datatype like integer byte or also floating type of datatype. Java also provide unary operator.

	Operator	Result
	+			Addition
	–			Subtraction (also unary minus)
	*			Multiplication
	/			Division
	%			Modulus
	++			Increment
	+=			Addition assignment
	–=			Subtraction assignment
	*=			Multiplication assignment
	/=			Division assignment
	%=			Modulus assignment
	– –			Decrement
2. Assignment operator

Java assignment operator is used to assign value to variable. as we know the behavior of assignment operator basically it is denoted by “=” operator. This operator is used to assign value to variable.

Example
int var = 5;

here we have integer type variable var and we are assigning value 5 to var by using “ = ” assignment operator.

3. Increment/decrements operator

Increment and decrements operators are unary operator it operates on one operands. By using increment operator increment the value that is stored inside variable. and by using decrement operator we decrement the value is stored inside the variable. “++” here plus plus operator are used for increment purpose and “--“ minus minus operator are used for decrement purpose.

Example
int var = 10;
var++;

Here the variable var is initialize with 10 and after initialization it has incremented by ++ operator and after increment the value is 11.

public class HFC {
   public static void operate(int[] arr, int x, int y) {
     x = x++ +y;
     y  += (y = 1);
     arr[y] = x;
     for(int i = 0; i < arr.length; i++) {
       System.out.println(arr[i]);
     }
	 float a=9.0f;
	 System.out.println(a);
   }
 
   public static void main(String[] args) {
     short a = 1;
     byte b = 2;
     HFC.operate(new int[]{a, ++a, ++a, ++a}, b++,  --b);
   }
 }

O/P
D:\classes>javac HFC.java
D:\classes>java HFC
1
2
3
4
9.0
4. Relational operator

By using relational operator we compare two quantities for e.g comparing marks of two student. The following relational operators are given as.

Operator	Meaning
	<		less than
	<=		less than or equal to
	>		greater than
	>=		greater than equal to
	==		equal to
	!=		not equal to
class ComparisonDemo {

     public static void main(String[] args){
          int value1 = 1;
          int value2 = 2;
          if(value1 == value2) System.out.println("value1 == value2");
          if(value1 != value2) System.out.println("value1 != value2");
          if(value1 > value2) System.out.println("value1 > value2");
          if(value1 < value2) System.out.println("value1 < value2");
          if(value1 <= value2) System.out.println("value1 <= value2");
     }
}

O/P
D:\classes>javac ComparisonDemo.java
D:\classes>java ComparisonDemo
value1 != value2
value1 < value2
value1 <= value2
5. Conditional operator

Conditional operator are applied on Boolean expression and the result of Boolean expression is either true or false. Basically there are three conditional operator.

&&	the logical AND operator
||	the logical OR operator
?:	the ternary operator

The logical AND and logical OR operator both takes two operands. The logical AND operators return true if both operands are true otherwise it will return false and in case of logical OR operator if both operands are false than it will return false otherwise it will return true.

6. Bitwise operator

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

Operator	Result
	~		Bitwise unary NOT
	&		Bitwise AND
	|		Bitwise OR
	^		Bitwise exclusive OR
	>>		Shift right
	>>>		Shift right zero fill
	<<		Shift left
	&=		Bitwise AND assignment
	|=		Bitwise OR assignment
public class hfc
{
    public static void main(String[] args)
	{
        System.out.println(9 ^ 2);
    }
} 

O/P
D:\classes>javac hfc1.java
D:\classes>java hfc1
11