DEV Community

Cover image for Operators in Java
Kumar Sanskar
Kumar Sanskar

Posted on

Operators in Java

Hello readers it's day seven of my learning Java journey and today I will share with you my learning of Operators in java.

As we have symbols in mathematics to perform certain calculation say the plus '+' for addition and so on similarly we have operators in Java to perform either a mathematical or logical manipulation

  • Java has four types of operators:- i. Mathematical ii. Logical iii. Relational iv. Bitwise

Mathematical Operators -

  • it contains all the symbols that are used in mathematical computation only just be a bit careful with the use of / and %.
Symbol Function
+ addition
- subtraction
* multiplication
/ integer division (returns quotient)
% modulus (returns remainder)
++ increment
-- decrement
  • the increment (++) and decrement (--) operators increase or decrease the value of operands by one.
  • it can be used in two ways either prefix or postfix.

Relational and Logical Operators -

  • relational and logical operators are used to show decide or compare two operands and they produce result as of type Boolean that is either true or false.
  • in case of logiacal operators the operands must be in form of boolean values.

    • Relational Operataors:-
Symbol Function
> greater than
< less than
>= greater than equal to
<= less than equal to
== equal to
!= not equal to
  • Logical Operators:-
Symbol Function
& AND
^ XOR
&& SHORT-CIRCUIT AND
! NOT
  • the difference between normal and short-circuit logical operators is that normal ones evaluate each operand but in case of short-circuit they only evaluate the second condition of necessary
  • the short-circuit operators are also known as conditional-and and conditional-or operators.

Assignment Operator -

  • it is used for assigning values to variables.
  • it is represented by normal '=' sign and should not be confused with '==' of relational operators.
  • java also allows you to make a chain of assignments that is to make them get a common value.

     int a, b, c;
     a = b = c =1;
    

Shorthand Assignment -

  • it is used to simplify the coding convention and save some time. for example-
  x = x + 10;
can be re-written as 
  x += 10;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)