9) Operators

Java Operators

Operator is a special symbol that tells the compiler to perform specific mathematical or logical Operation. We can divide all the Java operators into the following groups:

  • Arithmetic Operators

  • Relational Operators

  • Logical Operators

  • Assignment Operators

  • Ternary or Conditional Operators

Arithmetic Operators

Given table shows the entire Arithmetic operator supported by Java Language. Let's suppose variable A hold 8 and B hold 3.

Relational Operators

Which can be used to check the Condition, it always returns true or false. Let’s suppose variable A hold 8 and B hold 3.

Logical Operator

Which can be used to combine more than one Condition? Suppose you want to combined two conditions A<B and B>C, then you need to use Logical Operator like (A<B) && (B>C). Here && is Logical Operator.

Truth table of Logical Operator

Assignment operators

This can be used to assign a value to a variable. Lets suppose variable A hold 8 and B hold 3.

Ternary operator

If any operator is used on three operands or variable is known as ternary operator. It can be represented with " ?: "

Syntax

result = testCondition ? value1 : value2

this statement can be read as “If testCondition is true, assign the value of value1 to result; otherwise, assign the value of value2 to result.

Example:

 int a=6,b=7;
 int minVal = (a < b) ? a : b;
 System.out.println(minVal);

Output: 6

Last updated