PAGE-5

CHAPTER-1(Introduction to Java)                                             Bottom

 

Operators

Java provides a rich operator environment. Most of its operators can be divided into the following four groups: arithmetic, bitwise, relational, and logical. Java also defines some additional operators that handle certain special situations.

Operators are used to perfom operations on operends and in turn return result based on those operations.

 

Postfix operators [] . (parameters) expression++ expression--
Unary prefix operators ++expression , --expression , +expression , -expression , ~, !
Unary prefix creation and cast new (type)
Multiplicative * / %
Additive + -
Shift << >> >>>
Relational < <= > >= instanceof
Equality == !=
Bitwise/logical AND &
Bitwise/logical XOR ^
Bitwise/logical OR |
Conditional AND &&
Conditional OR ||
Conditional ?:
Assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=

Figure showing the list of operaters used in java

Simple Assignment Operator =

The assignment statement has the following syntax:-
       <variable> = <expression>


which can be read as "the destination <variable>, gets the value of the source <expression>". The previous value of the destination variable is overwritten by the assignment operator = . The destination <variable> and the source <expression> must be type compatible. The destination variable must also have been declared. Since variables can store either primitive data values or object references, <expression> evaluates to either a primitive data value or an object reference.

Assigning Primitive Values

The following examples illustrate assignment of primitive values:


int j, k;
j = 10; // j gets the value 10.
j = 5; // j gets the value 5. Previous value is overwritten.

 

Arithmetic Operators:

* , / , % , + , -

 

Unary Arithmatic Operators

 

The unary operators have the highest precedence of all the arithmetic operators. The unary operator - negates the numeric value of its operand. The following example illustrates the function of unary operators:


int value = - -10; // (-(-10)) is 10

Notice the blank needed to separate the unary operators; otherwise, these would be interpreted as the decrement operator -- . The unary operator + has no effect on the evaluation of the operand value.

 

Multiplicative Binary Operators: *, /, %


Multiplication Operator: * .


Multiplication operator * multiplies two numbers.


int sameSigns = -4 * -8; // result: 32
double oppositeSigns = 4.0 * -8.0; // result: -32.0
int zero = 0 * -0; // result: 0


Division Operator / -:

Integer division always returns the quotient as an integer value, i.e. the result is truncated toward zero. Note that the division performed is integer division if the operands have integral values, even if the result will be stored in a floating-point type.An ArithmeticException is thrown when attempting integer division with zero, meaning that integer division by zero is an illegal operation. If any of the operands is a floating-point type, the operation performs floating-point division.

Examples:-

int var1 = 4 / 5; // result: 0
int var2= 8 / 8; // result: 1
double dvar = 12 / 8; // result: 1 by integer division. d1 gets the value 1.0.

 

Remainder Operator: %

In mathematics, when we divide a number (the dividend) by a another number (the divisor), the result can be expressed in terms of a quotient and a remainder. For example, dividing 7 by 5, the quotient is 1 and the remainder is 2. The remainder operator % returns the remainder of the division performed on the operands.

Example:-

int quotient = 7 / 5;    // Integer division operation: 1
int remainder = 7 % 5;    // Integer remainder operation: 2

 

Additive Binary Operators: +, -


The addition operator + and the subtraction operator - behave as their names imply: add or subtract values. The binary operator + also acts as string concatenation if any of the operands is a string . Additive operators have lower precedence than all the other arithmetic operators.

int var1=10, var2 =10;

int var3=var1+ var2      //result will be 20

 

Boolean Logical Operators: !, ^, &, |


Boolean logical operators include the unary operator ! (logical complement) and the binary operators & (logical AND), | (logical inclusive OR), and ^ (logical exclusive OR, a.k.a. logical XOR). Boolean logical operators can be applied to boolean operands, returning a boolean value. The operators &, |, and ^ can also be applied to integral operands to perform bitwise logical operations . Given that x and y represent boolean expressions.

Logical complement      !x      Returns the complement of the truth-value of x.
Logical AND            x & y Returnstrue if both operands are true; otherwise, false.
Logical OR            x | y true if either or both operands are true; otherwise,false.
Logical XOR       x ^ y true if and only if one operand is true; otherwise, false.

These operators always evaluate both the operands, unlike their counterpart conditional operators && and || .

Examples:-

boolean b1, b2, b3 = false, b4 = false;
b1 = 4 == 2 & 1 < 4;    // false, evaluated as (b1 = ((4 == 2) & (1 < 4)))
b2 = b1 | !(2.5 >= 8);    // true
b3 = b3 ^ b2;    // true
b4 = b4 | b1 & b2;    // false

Order of evaluation is illustrated for the last example:-
(b4 = (b4 | (b1 & b2)))
(b4 = (false | (b1 & b2)))
(b4 = (false | (false & b2)))
(b4 = (false | (false & true)))
(b4 = (false | false))
(b4 = false)

Relational Operators: <, <=, >, >=

 

a < b a less than b?
a <= b a less than or equal to b?
a > b a greater than b?
a >= b a greater than or equal to b?

All relational operators are binary operators, and their operands are numeric expressions.The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic operators, but higher than that of the assignment operators.

Examples:-

double hours = 45.5;
boolean overtime = hours >= 35.0;      // true.
boolean order = 'A' < 'a';      // true. Binary numeric promotion applied.


Conditional Operators: &&, ||

Conditional operators && and || are similar to their counterpart logical operators & and | , except that their evaluation is short-circuited. Given that x and y represent values of boolean expressions, the conditional operators are given below.

Conditional AND :-   x && y true if both operands are true; otherwise, false.

Conditional OR:-    x || y true if either or both operands are true; else false.

 

Examples:-

boolean b1 = 4 == 2 && 1 < 4;     // false, short-circuit evaluated as
//(b1 = ((4 == 2) && (1 < 4)))

boolean b2 = !b1 || 2.5 > 8;      // true, short-circuit evaluated as
// (b2 = ((!b1) || (2.5 > 8)))

boolean b3 = !(b1 && b2);    // true

boolean b4 = b1 || !b3 && b2;      // false, short-circuit evaluated as
// (b4 = (b1 || ((!b3) && b2)))

 

 

Shift Operators: <<, >>, >>>

 


The shift operators form a new value by shifting bits either left or right a specified number of times in a given integral value. The number of shifts (also called the shift distance) is given by the right-hand operand, and the value that is to be shifted is given by the left-hand operand. The value returned has the promoted type of the left-hand operand. Also, the value of the left-hand operand is not affected by applying the shift operator.
The shift distance is calculated by AND-ing the value of the right-hand operand with a mask value of 0x1f (31) if the left-hand has the promoted type int, or using a mask value of 0x3f (63) if the left-hand has the promoted type long. This effectively means masking the five lower bits of the right-hand operand in the case of an int left-hand operand, and masking the six lower bits of the right-hand operand in the case of a long left-hand operand. Thus, the shift distance is always in the range 0 to 31 when the promoted type of left-hand operand is int, and in the range 0 to 63 when the promoted type of left-hand operand is long.

Shift left a << n                     Shift all bits in a left n times, filling with 0 from the right.
Shift right with sign bit a >> n     Shift all bits in a right n times, filling with the sign bit from the left.
Shift right with zero fill a >>> n      Shift all bits in a right n times, filling with 0 from the left.

Shift left Operator<<

As the bits are shifted left, zeros are always filled in from the right.
int i = 12;

int result = i << 4; // 192
The bits in the int value for i are shifted left four places as follows:
i << 4
= 0000 0000 0000 0000 0000 0000 0000 1100 << 4
= 0000 0000 0000 0000 0000 0000 1100 0000
= 0x000000c0
= 192

The Shift-right-with-sign-fill Operator >>

As the bits are shifted right, the sign bit (the most significant bit) is used to fill in from the left. So, if the left-hand operand is a positive value, zeros are filled in from the left, but if the operand is a negative value, ones are filled in from the left.

Example:

int i = 12;
int result = i >> 2;      // 3
The value for i is shifted right with sign-fill two places.
i >> 2
= 0000 0000 0000 0000 0000 0000 0000 1100 >> 2
= 0000 0000 0000 0000 0000 0000 0000 0011
= 0x00000003
= 3
Each right-shift corresponds to integer division of the value being shifted by two

The Shift-right-with-zero-fill Operator >>>.

For positive values, the shift-right-with-zero-fill >>> and shift-right-with-sign-fill >> operators are equivalent. The expression (12 >> 2) and the expression (12 >>> 2) return the same value:

Example:-

12 >>> 2
= 0000 0000 0000 0000 0000 0000 0000 1100 >>> 2
= 0000 0000 0000 0000 0000 0000 0000 0011
= 0x00000003
= 3
Individual unary numeric promotion of the left-hand operand is shown in the following example:

byte b = -42;         // 1101 0110
int result = b >>> 4;       // 268435453



 
Note :
After Successful completion of Training Candidate will be provided with Project Report and Training Certificate.

Home  |   FeedBack  |   Terms of Use  |   Contact Us  |   Report Error
                                                                            Copyright © 2009 R.M Infotech (P) Ltd.                                             Designed by: Raman