Operators in Java

We know all the operators used in the Java language.

The operators are very similar to those in C++, as we noted at the time.

Arithmetic Operators: The usual ones

  • Add + .
  • Subtraction – .
  • Multiplication * .
  • Division / .
  • Rest of Division % .

Assignment Operators: The main one is ‘=’ but there are more assignment operators with different functions that we briefly explain now.

  • ‘+=’ : op1 += op2 to op1 = op1 + op2
  • ‘-=’ : op1 -= op2 to op1 = op1 – op2
  • ‘*=’ : op1 *= op2 to op1 = op1 * op2
  • ‘/=’ : op1 /= op2 to op1 = op1 / op2
  • ‘%=’ : op1 %= op2 to op1 = op1 % op2

Unary Operators: The plus (+) and the minus (-). To change the sign of the operand.

Instanceof operator: It allows us to know if an object belongs to a class or not.

  • ObjectName instanceof ClassName

Incremental Operators: These are the operators that allow us to increase the variables by one unit. They can be used before and after the variable depending on what we want, that is, if we want it to increase or vice versa before using it or the opposite. Relational Operators: They allow comparing variables according to a relationship of equality/inequality or a greater/lesser relationship. They always return a boolean value.

  • ‘>’: Greater than
  • ‘<': Less than
  • ‘==’: Equals
  • ‘¡=’: Different
  • ‘>=’: Greater than or equal to
  • ‘<=': Less than or equal to

Logical Operators: They allow us to build logical expressions.

  • ‘&&’ : returns true if both operands are true.
  • ‘||’ : returns true if any of the operands are true.
  • ‘!’ : Negates the operand passed to it.
  • ‘&’ : returns true if both operands are true, evaluating to both.
  • ‘|’ : returns true one of the operands is true, evaluating both of them.
See also  How to know if a field is NULL in a SQL query with MySQL

Concatenation operator with string ‘+’:

  • For Example: System.out.println(“The total is”+ result +”units”);

Operators that act at the bit level: They are much less used, that is why we explain them more above.

  • ‘>>’: right shift of operand bits
  • ‘<<': left shift of operand bits
  • ‘&’: bitwise and operator.
  • ‘|’: bitwise or operator
Loading Facebook Comments ...
Loading Disqus Comments ...