Java Operators

Operator define the actions to be performed on the operands in an expression. Depending on the number of operands on which an operator acts, operators are classified as unary, binary, and ternary operators.

Unary operator act on a single operand. Binary and ternary operators act on two and three operands respectively.

Unary Operator

They are of three types: minus, increment and decrement.

Then minus operator is represented by the minus (-) symbol. It acts on a variable or constant that follows the operator. The minus operator indicated that the value of the operand on which the operator acts is negated. A variable containing a negated value becomes a postive.

Example: -(-6) would be 6.

The increment operator is represented by two plus(++) signs. It causes the value of its operand to be increased by the value 1. The increment operator can be placed before or after the operand. If the increment operator precedes an operand, then the value of the operand will be increased by the value 1 before a statement in a program uses the operand. This type of increment is called prefix increment. [Refer Fig 1.1]

Fig 1.1-Prefix Increment

If the increment operator follows an operand, then the value of the operand is altered after the operand is utilized in a specifix statement in a program. This type of increment operator is called a postfix increment operator. [Refer Fig 1.2]

Fig 1.2-Postfix Increment

The decrement operator is represented by two minus(–) signs. The decrement operator causes an operand to be decreased by the value 1. It can be placed before or after an operand to perform a prefix or postfix decrement operation. The operation of the decrement operator is similar to the increment operator. However, the effect of the decrement operator is to decrease the value of the operand on which it acts.

Arithmetic Operators

To perform arithmetic operations or calculations in a program, you use the arithmetic operators in expressions. The expressions that contain these operators are called arithmetic expressions. There are five arithmetic operators: the slash mark (/), the asterisk (*), the percent sign (%), the plus sign (+), and the minus sign (-). These operators represent division, multiplication, remainder, addition, and subtraction operations respectively.

The operands with which arithmetic operators work must have numeric values. Therefore, operands, or their values can be integers or real numbers.

Arithmetic operators have a specific order of evaluation. This order of evaluation is called precedence. The division, multiplication, and remainder operators belong to one precedence group. The addition and subtraction operators belong to another precedence group. The group of the division, multiplication and remainder operators has precedence over the group of addition and subtraction operators.

When evaluating arithmetic expressions, the order in which consecutive operations occur within the same precedence group is called associativity.

Within each precedence group, the associativity of arithmetic operators is from left to right. The consecutive division, multiplication, and remainder operations are carried out from left to right in the order of their occurrence.

EXAMPLE

12/4*2 = 6
Division is performed first and then multiplication.

15+6-3=18
Both operators + and – belong to the same precedence group, and their associativity is from left to right. In the example, addition is performed first. Next, subtraction is performed.

You can alter the precedence of arithmetic operators by using parentheses. Parentheses have precedence over all arithmetic operators. You can also have nested parentheses in an expression. The innermost parentheses have precedence over the outer parentheses.

EXAMPLE

(8*(x+y))
Here the addition of x and y is carried out first and the sum is then multiplied by the value 8.

HINT:
Operator Precedence = BEDMAS Rule
B – > Brackets [Parentheses]
E – > Exponential
D – > Division
M – > Multiplication
A – > Addition
S – > Subtraction

Assignment Operators

To assign a value to a variable, an assignment operator is used.

SYNTAX:
var = expression

The assignment operator is represented by the equal to symbol (=). A statement that contains the assignment operator is written as var=expression. The variable var stores the value of the expression.

EXAMPLE:
age = 25
total = a + b

In the statement total=a+b, the sum of a and b is assigned to total. Such assignments are called single assignments.

In addition to a single assignment, multiple assignments are also possible in Java. This means that two or more variables can be assigned the same value by using the assignment operator.

EXAMPLE:
var1=var2=10
Assigns the value 10 to the variables var1 and var2.

In addition to the assignment operator, Java has compound assignment operators. These are +=,-=,*=,/= and %=.

Compound assignment operators enable you to write shorthand notation expressions.

EXAMPLE
var1+=var2;
This is equivalent to var1 = var1 + var2;

var1 -= exp ====> Equivalent to var1 = var1 – exp
var1 *= exp ====> Equivalent to var1 = var1 * exp
var1 /= exp ====> Equivalent to var1 = var1 / exp
var1 %= exp ====> Equivalent to var1 = var1 % exp

Assignment operators have right to left associativity. This indicates that in the expressions that contain assignment operators, the expression is evaluated from right to left.

Relational Operators

Relational operators are used for comparing values and taking appropriate actions based on the comparison. For example, a program calculates the levels of the employees in a department by comparing their salaries with different values.

The result of the expression containing a relational operator is either true or false. Relational operators accept two operands.

There are six relational operators in java: greater than (>), less than (<), less than or equal to (<=), greater than or equal to (>=), equal to (==), and not equal to (!=).

EXAMPLE

if (salary < 50000) {
Level = B
}

if (salary >= 50000) {
Level = A
}

If the salary is below $50000 a year, an employee is categorized as a level B employee. If the salary is $50000 or above in a year, the employee is categorized as a level A employee. To determine the level of an employee, the salary of the employee has to be compared with the value 50000.

If the salary of an employee is $50000 and is stored in the variable empSal, the expression empSal<50000 compares the value of empSal with 50000 by using a relational operator.

The equal to and not equal to operators are also called equality operators.

Relational operators have a precedence that determines the order in which they are evaluated in a multiple-operator expression. The operators greater than (>), less than (<), less than or equal to (<=), and greater than or equal to (>=) fall into one precedence group. The equality operators equal to (==) and not equal to (!=) fall into another precedence group. The equality operators have a lower precedence than other relational operators.

The associativity of relational operators is from left to right. Therefore, in an expression that contains more than one relational operator from the same precedence group, the expression is evaluated from left to right.

Logical Operators

Logical operators enable you to combine and compare two conditions

EXAMPLE:

If basePay is below $2000 and above $1000, allowance is 10% of basePay.

This means that an employee receives the allowance only when both these conditions are satisfied. The result of an expression that contains a logical operator is either true or false.

There are six boolean logical operators.

  • Boolean Logical AND – &
  • Boolean Logical OR – |
  • Boolean Logical XOR – ^
  • Conditional AND – &&
  • Conditional OR – ||
  • Boolean Logical NOT – !

In Java, there are two short-circuit logical operators. These are Conditional AND (&&) and Conditional OR (||). Java does not evaluate the operand on the right when the result can be determined by the operand on the left only. This can be important when there are side affects from the operand on the right.

The &&,||,&,| and ^ are binary operators. The ! operator is a unary operator.

Associativity relates to precedence and resolves any ambiguity over the grouping of operators with the same precedence. The associativity of the &&,||,&,|, and ^ operators is from left to right.

The && operator evaluates two subexpressions, Op1 and Op2, and generates the value true only if both the subexpressions individually evaluate to true. The result of the expression that combines the expressions oP1 and oP2 by using the && operator is false if either oP1 or oP2 evaluates to false.

If two expressions, op1 and op2 are combined using the || operator, the result of the combined expression is true if either op1 or op2 evaluates to true.

The ! operator negates the value of an expression. This indicates if the value of an expression op1 is true initially, the ! operator alters its value to false. If the value of an experssion, op1 is originally false, the ! operator alters the value to true.

The & operator works the same as the && operator, except that the operand on the right is always evaluated. The expression evaluates to true only if both of the operands are true.

The | operator works the same as the || operator, except that the operand on the right is always evaluated. The expression evaluates to true if either of the operands are true.

The ^ operator is the exclusive OR (XOR) operator. The expression evaluates to true if only one of the operands is true. If both operands are false or both operands are true, the expression will evaluate to false.

Bitwise Operators

It enables you to calculate the result of an expression by using the individual bits of the operands in the expression. There are four bitwise operators in Java: AND, OR, XOR, and NOT. These operators are represented by the symbols &, |, ^, and ~ respectively. The AND, OR, and XOR operators are binary operators. The NOT operator is a unary operator.

The AND operator evaluates the binary equivalent of the two operands, op1 and op2 and generates 1 if the value of both the operands is 1. If the value of any of the operands is 0, the result is 0.

The OR operator results in the value 0 when the value of both the bits is 0. Otherwise, the result of the OR operation is the value 1.

The XOR operator is an exclusive OR operator. This operator results in the value 1 if the two operand bits are different. Otherwise, the result of the XOR operation is the value 0.

In addition to the bitwise operators, some shortcut assignment operators perform bitwise operations. These assignment operators are &=, |=, ^=.

The &= shortcut assignment operator works the same as & operator and the results are assigned to op1. If the value of the corresponding bits in both operands is 1, then the operator generates a result of 1, otherwise the result is 0.

The |= shortcut assignment operator works the same as | operator and results are assigned to op1. If the value of the either, or both of the corresponding bits in both operands is 1, then the operator generates a result of 1, otherwise the result is 0.

The ^= shortcut assignment operator works the same as ^ operator and the results are assigned to op1. If the value of only one of the corresponding bits in both operands is 1, then the operator generates a result of 1. If both of the corresponding bits are 0 or 1, the result is 0.

Related Posts: Introduction to Java | Classes, Methods & Objects | Objects and Variables | Datatypes | Conditional Flow Control | Iterative Flow Control | Jump Statements

Advertisements

One response to “Java Operators

  1. Good article… nice and detailed and contained exactly the information I wanted about logical shortcut assignment operators. Thanks!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s