Java Operator & Expression5 min read

Operators are used to perform operations on variables and values or to manipulate them. The operation is performed with two operand and operators to provide what action need to perform.

Example:

In the above example, 10 and 5 are operands and + is the operation performed between these two operands.

Java provides numbers of operation and these are grouped into the following:

  • Arithmetic Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Logical Operators
  • Relational (comparison) operators
  • Bitwise Operators
  • Ternary Operator

1. Arithmetic Operators: 

They are used to perform basic arithmetic operations. They perform on primitive data types.
They are:

  • * : Multiplication
    Multiplies two values
    Example: x * y;
  • / : Division
    Divides one value from another
    Example: x / y;
  • % : Modulo
    Returns the division remainder
    Example: x % y;
  • + : Addition
    Adds together two values
    Example: x + y;
  • – : Subtraction
    Subtracts one value from another
    Example: x - y;

2. Assignment Operators: 

Assignment operator is used to assigning a value to any variable. This operator assigns the value of the right-hand side of an operator to the left-hand side. Various assignment operator in Java:
These are the short version formed by combining the two operators.

For example Instead of writing, int a = a+3, we can write a+= 5.

  • Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.
    a = 20; B = 30; Ch = 'cha';
  • Add AND assignment operator: ‘+=’ This operator first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
    c+=7; a+=b;
  • Subtract AND assignment operator: ‘-=’ This operator first subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left.
    c-=7; a-=b;
  • Multiply AND assignment operator: ‘*=’ This operator first multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
    c*=7; a*=b;
  • Divide AND assignment operator: ‘/=’ This operator first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
    c/=7; a/=b;
  • Modulus AND assignment operator: ‘%=’ It takes modulus using two operands and assigns the result to the left operand.
    C %= A is equivalent to C = C % A ;
  • Right shift AND assignment operator: ‘>>=’ This operator is used for Right shift Operation.
    C >>= 2 ;
  • Left shift AND assignment operator: ‘<<=’ This operator is used for Left shift Operation.
    C <<= 2 ;
  • Bitwise AND assignment operator: ‘&=’ This operator is used for Bitwise AND Operations.
    C &= 2;
  • Bitwise exclusive OR and assignment operator: ‘^= This operator is used for Bitwise exclusive OR Operations.
    C ^= 2;
  • Bitwise inclusive OR and assignment operator: ‘|= This operator is used for Bitwise inclusive OR.
    C |= 2;

3. Increment and Decrement Operators: 

The ++ and the – – are Java’s increment and decrement operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.
They are:

  • Post-Increment or Post-Decrement:
    First, the value is used for operation and then incremented or decremented. Represented like a++ or a–.
  • Pre-Increment Pre-Decrement:
    Here First the value is incremented or decremented then used for the operation. Represented like ++a or –a.

4. Logical Operators:

Logical Operators are used in conditional statements and loops for evaluating a condition with binary values. All of the binary logical operators combine two boolean values that are true and false to form a result value.

Logical Operators with their description:

OperatorDescription Example
&& (logical and)If both the operands are non-zero, then the condition becomes true.(X && Y) is false
|| (logical or)If any of the two operands are non-zero, then the condition becomes true.(X || Y) is true
! (logical not)Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(X && Y) is true

5. Relational (comparison) operators:

Comparison operators are used to comparing two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values. They are used with a loop statement and also with if-else statements.

Following are the relational operators in Java:

OperatorNameExample
==Equal to.A == B
!=Not equal.A != B
>Greater than.A > B
<Less than.A < B
>=Greater than or equal to.A >= B
<=Less than or equal to.A <= B

6. Bitwise operators: 

Bitwise operator works on bits and performs a bit-by-bit operation. There are six Bitwise Operators and can be applied to the integer types, long, int, short, char, and byte.

  • Bitwise AND: ‘&’
    Binary AND Operator copies a bit to the result if it exists in both operands.
  • Bitwise OR: ‘|’
    Binary OR Operator copies a bit if it exists in either operand.
  • Bitwise XOR: ‘^’
    Binary XOR Operator copies the bit if it is set in one operand but not both.
  • Binary Left Shift: ‘<<’
    TThe left operands value is moved left by the number of bits specified by the right operand.
  • Binary Right Shift: ‘>>’
    The left operand’s value is moved right by the number of bits specified by the right operand.
  • Bitwise complement: ‘~’
    Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.
  • zero-fill right shift: ‘>>>’
    The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.

7. Ternary operators: 

Ternary Operator is also known as the Conditional operator. It is used to evaluate Boolean expressions. We can say that it is a short version of the if-else statement. But here instead of operating on to operand, it operates on three operands, and therefore the name ternary.

Syntax: