Operators in Java

 Operators in Java

  1. Arithmetic Operators

  2. Unary Operators

  3. Relational Operators 

  4. Logical Operators 

  5. Ternary operator 

  6. Bitwise Operators 

Arithmetic Operators:-
They are used to perform simple arithmetic operations on primitive data types.
  • * : Multiplication
  • / : Division
  • % : Modulo
  • + : Addition
  • – : Subtraction
Unary Operators:-

Unary operators need only one operand. They are used to increment, decrement or negate a value.

– :Unary minus, used for negating the values.
+ :Unary plus, used for giving positive values. Only used when deliberately converting a negative value to positive.
++ :Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.
  • Post-Increment : Value is first used for computing the result and then incremented.
  • Pre-Increment : Value is incremented first and then result is computed.
— : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operator.
  • Post-decrement : Value is first used for computing the result and then decremented.
  • Pre-Decrement : Value is decremented first and then result is computed.
! : Logical not operator, used for inverting a boolean value


Relational Operators:-

These operators are used to check for relations like equality, greater than, less than. They return boolean result after the comparison and are extensively used in looping statements as well as conditional if else statements. General format is,

variable relation_operator value

Some of the relational operators are-

  • ==, Equal to : returns true of left hand side is equal to right hand side.

  • !=, Not Equal to : returns true of left hand side is not equal to right hand side.

  • <, less than : returns true of left hand side is less than right hand side.

  • <=, less than or equal to : returns true of left hand side is less than or equal to right hand side.

  • >, Greater than : returns true of left hand side is greater than right hand side.

>=, Greater than or equal to: returns true of left hand side is greater than or equal to right

Logical Operators :-

These operators are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision.

Conditional operators are-

  • &&, Logical AND : returns true when both conditions are true.

  • ||, Logical OR : returns true if at least one condition is true.


Ternary operator :-

Ternary operator is a shorthand version of if-else statement. It has three operands and hence the name ternary. General format is-

condition ? if true : if false

The above statement means that if the condition evaluates to true, then execute the statements after the ‘?’ else execute the statements after the ‘:’.

Bitwise Operators :-

These operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of Binary indexed tree.

  • &, Bitwise AND operator: returns bit by bit AND of input values.

  • |, Bitwise OR operator: returns bit by bit OR of input values.

  • ^, Bitwise XOR operator: returns bit by bit XOR of input values.

  • ~, Bitwise Complement Operator: This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed.






Comments

Popular posts from this blog

Inheritance in Java

Type Conversions

Life cycle of a Thread and creating thread class