Java Operators

Relational Operators:

  • Relational operators always result in a Boolean value( true or false)
  • There are six relational operators >, >=, <, <=, == and != The last two (== and !=) are sometimes referred  to as equality operators.
  • When comparing characters, java uses the Unicode value of the character as the numerical value.
  • Equality operators:
  1. There are two equality operators. == and !=
  2. Four types of things can be tested: Numbers, Characters, and Booleans and Reference variables.
  3. When comparing reference variables. == Returns true if both references refer to same object.

Instanceof Operator:

  • Instanceof is for reference variables only, and checks for weather the object is of particular type.
  • The instance of operator can be used only to test objects (or null) against class types that are in the same hierarchy.
  • For interfaces, an object passes the Instanceof test if any of its subclasses implement the interface on the right side of the Instanceof operator.

Arithmetic Operators:

  • There are four primary math operators: add, subtract, multiply, and divide.
  • The remainder operator (%), returns the remainder of division.
  • Expressions are evaluated from left to right, unless you add parenthesis, or unless same operators in the expression have higher precedence than others.
  • The *, /, and % have higher precedence than + and -.

String concatenation Operators:

  • If either operand is a String the + operator concatenates the operands.
  • If both operands are numeric the + operator adds the operands.

Increment / Decrement operators:

  • Prefix operators (++ and –) run before the value is used in expression.
  • Postfix operators (++ and –) run after the value is used in the expression.
  • If any expression, both operands fully evaluated before the operator is applied.
  • Variables marked as final cannot be incremented or decremented.

Ternary (Conditional operator):

  • Returns one of the two values based on the Boolean expression is true of false.
  • Returns the value after the ? if the expression is true.
  • Returns the value after the : if the expression is false.

Logical Operators:

  • The exam covers six “logical” operators &, |, ^, !, &&, and ||.
  • Logical operators work with two operands (except for !) that must resolve Boolean values.
  • The && and & operators returns true if both operands are true.
  • The || and | operator returns true if either or both operands are true.
  • && and || operators are known as short circuit operators.
  • The && operator dose not evaluate the right operand if the left operand is false.
  • The || operator doesn’t evaluate the right operand if the left operand is true.
  • The & and | operators always evaluate both operands.
  • The ^ operator (called logical XOR), returns true if exactly one operand is true.
  • The ! Operator (called the inversion operator), returns the opposite value of both Boolean operand it precedes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s