Java

Advertisements

Operator in Java

An operator is a symbol that tells the compiler to perform specific mathematical or logical calculations on operands(variables).

Types of operators available in Java

  • Assignment operator
  • Arithmetic / Mathmetical operator
  • Increment Decrement operator
  • Relational operator
  • Logical operator
  • Conditional or Ternary operator
  • Binary operator
  • Unary operator

Java Assignment operator

Assignment operator is used to copy value from right to left variable.

		Suppose we have,

		float X = 5, Y = 2;
		
OperatorNameDescriptionExample
=Equal signCopy value from right to left.X = Y, Now both X and Y have 5
+=Plus Equal toPlus Equal to operator will return the addition of right operand and left operand.X += Y is similar to X = X + Y, now X is 7
-=Minus Equal toMinus Equal to operator will return the subtraction of right operand from left operand.X -= Y is similar to X = X - Y, now X is 3
*=Multiply Equal toMultiply Equal to operator will return the product of right operand and left operand.X *= Y is similar to X = X * Y, now X is 10
/=Division Equal toDivision Equal to operator will divide right operand by left operand and return the quotient.X /= Y is similar to X = X / Y, now X is 2.5
%=Modulus or Mod Equal toModulus Equal to operator will divide right operand by left operand and return the mod ( Remainder ).X %= Y is similar to X = X % Y, now X is 1

Java Arithmetic operator

Arithemtic operators are used for mathematical operations.

		Suppose we have,

		int X = 5, Y = 2;
		
OperatorNameDescriptionExample
+PlusReturn the addition of left and right operands.(X + Y) will return 7
-MinusReturn the difference b/w right operand from left operand.(X - Y) will return 3
*MultiplyReturn the product of left and right operands.(X * Y) will return 10
/DivisionReturn the Quetiont from left operand by right operand.(X / Y) will return 2(both are int, int doesn't support decimal)
%Modulus or ModReturn the Modulus ( Remainder ) from left operand by right operand.(X % Y) will return 1

Java Relational Operators

Relational operators are used for checking conditions whether the given condition is true or false. If the condition is true, it will return non-zero value, if the condition is false, it will return 0.

		Suppose we have,

		int X = 5, Y = 2;
		
OperatorNameDescriptionExample
>Greater thenCheck whether the left operand is greater then right operand or not.(X > Y) will return true
<Smaller thenCheck whether the left operand is smaller then right operand or not.(X < Y) will return false
>=Greater then or Equal toCheck whether the left operand is greater or equal to right operand or not.(X >= Y) will return true
<=Smaller then or Equal toCheck whether the left operand is smaller or equal to right operand or not.(X <= Y) will return false
==Equal toCheck whether the both operands are equal or not.(X == Y) will return false
!=Not Equal toCheck whether the both operands are equal or not.(X != Y) will return true

Java Logical Operators

Logical operators are used in situation when we have more then one condition in a single if statement.

		Suppose we have,

		int X = 5, Y = 2;
		
OperatorNameDescriptionExample
&&ANDReturn true if all conditions are true, return false if any of the condition is false.if(X > Y && Y < X) will return true
||ORReturn false if all conditions are false, return true if any of the condition is true.if(X > Y || X < Y) will return true
!NOTReturn true if condition if false, return false if condition is true.if(!(X>y)) will return false

Java Conditional Operator ?:

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. First is condition, second and third is value. The conditional operator check the condition, if condition is true, it will return second value, if condition is false, it will return third value.

Syntax :

		val = condition ? val1 : val2;

Example :


class ConditionalOperatorDemo
{
       public static void main()
		{
              int X=5,Y=2,lrg;

              lrg = (X>Y) ? X : Y;

              System.out.print("\nLargest number is : " + lrg);
        }
}

   Output :

              Largest number is : 5

		

Java Binary operator

Binary operators are those operators that works with at least two operands such as (Arithmetic operators) +, -, *, /, %.

Java Unary operator

Unary operators are those operators that works with singal operands such as (Increment or Decrement operators) ++ and --.

Advertisement