Operators

117 views.

Operator

Any symbol that performs an operation will called as operators.

Operand:

The values on which the operations are performed are called as operands.

a  + b

Here + Operator

a,b are operands

Based on the number of operands, the operators are classified into following categories:

  • Unary Operators: 

These operators will be perform operations on one operand.

  • Binary Operators

These operators will be performed operations on two operands.

  • Ternary Operators

These operators will be perform operations on three operands.

Based on the task that is performed, the operators are classified into following categories:

Arithmetic operators

These operators perform simple mathematical calculations. The various arithmetic operators are +,-,*,/,%

If both the operand is of floating point type, then the result will be of integer type. If at least one of the operand is of floating point type, then the result will be of floating point type.

Example: 7+2=9 , 7-2=5, 7*2=14, 7/2=3, 7/2.0=3.5, 7%2=1

Unary operators

These operators will perform operations on a single operand. The various unary operators are -,++,–

Unary Minus(-): 

It can be used to convert values from positive to negative or negative to positive.

int temp=23;

int temp=-23;

int temp=-(-23);

Increment Operator(++)

The increment operator will increase the value of a variable of variable by 1. Based on the position of the increment operator, the increment operator is classified into 2 types.

PreIncrement(++):

If the increment operator is placed before the variable then it is called preincrement operator. The preincrement operator will use the value after increasing.

PostIncrement(++):

If the increment operator is placed after the variable then it is called postincrement operator. The postincrement operator will use the value before increasing.

Decrement Operator(–):

The decrement operator will decrease the value of a variable of variable by 1. Based on the position of the decrement operator, the decrement operator is classified into 2 types.

PreDecrement(++):

If the decrement operator is placed before the variable then it is called predecrement operator. The predecrement operator will use the value after decreasing.

PostIncrement(++):

If the decrement operator is placed after the variable then it is called postdecrement operator. The postdecrement operator will use the value before decreasing.

Example Program:

Rule1: The increment and decrement operator can be applied to all numeric datatypes(byte, short, int, long, double, float, char)

Rule2: The increment and decrement operator can be applied to only variables, not to constants.

Rule3: The increment and decrement operator can not be nested.

Assignment operators

This operator can be used for assigning a value to a variable. The assignment operator is =

The assignment operator will copy the value from right side to left side. On the right side we can specify either a variable or a value or an expression etc. but on the left side we must specify only a variable.

Example: x=5, y=x, z=x+y

The assignment operator can be nested after the declaration of variable but not during the  declaration of the variable.

int a=b=c=d=6;   not valid because nested during declaration

int a,b,c,d;     valid beacuse nested after declaration

int a=6, b=6,c=6, d=6; valid beacuse there is no nesting.

If the assignment operator is combined with other operator then it is called as compound assignment operator (+= -= *= /= %=)

Relational operators

These operators can be used for comparing the values. These operators are also called as comparison operators. The various relational operators are <,<=,>,>=,!= etc.

The relational operators can be used for creating conditions.

Example: x<y, x>y, x<=y, x>=y

The result of the condition will be of Boolean type, i.e. if the condition is satisfied, then the result will be true, otherwise the result will be false.

Rule1: The relational <,<=,>,>= can be applied to any combination of numeric types only.

Rule2: The relational operators ==, != also called as equality operators can be applied to any combination of numeric types or any combination of Boolean types, but not to mixed types , i.e. one Boolean and one numeric data.

Logical operators

The logical operators can be used for combining the conditions or complimenting the result of condition.

The various logical operators are &, &&, |,||,^,!

AND Operator(&)

This operator can be used for combining conditions.

Syntax: (cond1) & (cond2)

The result of & operator will be of Boolean type i.e. the result will be either true or false. The result will be true only if all the conditions are true.

F F F
F T F
T F F
T T T

The & operator will evaluate all the conditions and then decide  the result

AND Operator(&&)

This operator can be used for combining multiple conditions.

Syntax: (cond1) && (cond2)

The result of && operator will be of Boolean type i.e. the result will be either true or false. The result will be true only if all the conditions are true.

F F F
F T F
T F F
T T T

The && operator will evaluate the first condition. If the result of the first condition is false, then it will skip the evaluation conditions and directly decide the result as false, but if result of first condition is true , then it will evaluate the next condition and then decide the result.

Note: The && operator is designed to improve the performance of the application sometimes, when the result of first condition is false.

AND Operator(&&)

This operator can be used for combining multiple conditions.

Syntax: (cond1) && (cond2)

The result of && operator will be of Boolean type i.e. the result will be either true or false. The result will be true only if all the conditions are true.

F F F
F T F
T F F
T T T

The && operator will evaluate the first condition. If the result of the first condition is false, then it will skip the evaluation conditions and directly decide the result as false, but if result of first condition is true , then it will evaluate the next condition and then decide the result.

Note: The && operator is designed to improve the performance of the application sometimes, when the result of first condition is false.

OR Operator(|)

This operator can be used for combining multiple conditions.

Syntax: (cond1) | (cond2)

The result of | operator will be of Boolean type i.e. the result will be either true or false. The result will be true , if at least one  the condition is true.

F F F
F T T
T F T
T T T

The | operator will evaluate all the conditions and decide the result.

OR Operator(||)

This operator can be used for combining multiple conditions.

Syntax: (cond1) ||(cond2)

The result of ||operator will be of Boolean type i.e. the result will be either true or false. The result will be true , if at least one  the condition is true.

F F F
F T T
T F T
T T T

The || operator will evaluate the first condition. If the result of the first condition is true, then it will skip the evaluation conditions and directly decide the result as true, but if result of first condition is false, then it will evaluate the next condition and then decide the result.

Note: The || operator is designed to improve the performance of the application sometimes, when the result of first condition is true.

.

X-OR Operator(^)

This operator can be used for combining multiple conditions.

Syntax: (cond1) ^(cond2)

The result of ^ operator will be of Boolean type i.e. the result will be either true or false. The result will be true , if the inputs are different and if the inputs are same   the result is false.

F F F
F T T
T F T
T T F

Not Operator(!)

This operator can be used for complementing the result of a condition i.e. it converts true to false or false to true.

Syntax: !(cond1)

Rule: The logical operator can be applied to any combination of Boolean type only.

Bitwise operators

This operator will perform the operation on the bits of a number. The various bit wise operators are ~ & | ^ <<  >> >>>

Negation Operator(~)(tilde)

This operator will convert the bits from 0’s to 1’s and 1’s to 0’s

X=5        0      0      0      0      0       1      0       1

~X          1       1      1       1        1       0      1       0

If the first is ‘1’ then it represents a negative number. The value of the negative number is calculated by performing 2’s compliment.

2’s compliment=1’s compliment+1

1      1      1      1      1       0      1       0

0     0      0      0     0     1      0       1

1

__________________________

0     0      0      0     0     1      1      0      =-6

___________________________

Note: ~X=X+1

Bitwise AND Operator(&):

This operator will perform AND operation on the bits of number

0 0 0
0 1 0
1 1 0
1 1 1

x=5      0      0      0      0      0       1      0       1

y=6    0      0      0      0     0        1      1       1

__________________________

x&y        0     0      0      0     0     1      1      0      =4

___________________________

Bitwise OR Operator(|):

This operator will perform OR operation on the bits of number

0 0 0
0 1 1
1 1 1
1 1 1

x=5      0      0      0      0      0       1      0       1

y=6    0      0      0      0     0        1      1       0

__________________________

x&y        0     0      0      0     0     1      1      1      =7

___________________________

Left Shift Operator(<<):

This Operator will shift the bits of a number towards left side by the specified number of positions.

Lost
X==10 0 0 0 0 1 0 1 1
X<<1 0 0 0 1 0 1 0 0
Inserted

Shifting the bits of a number towards left side by one position is equivalent to multiplying a number by 2.

Right Shift Operator(>>):

This operator will shift the bits of a number towards right side by the specified number of positions.

Lost
X==10 0 0 0 0 1 0 1 0
X<<1 0 0 0 1 0 1 0 0
Inserted

Shifting the bits of a number towards right side by one position is equivalent to dividing a number by 2.

Unsinged Right Shift Operator(>>>):

This operator will shift the bits of a number towards right by the specified number of positions.

Difference between >> & >>>:

When we shift the bits of a number towards right side by using>> operator, the sign bit of the input will copied as it is into resultant i.e. positive input will lead to positive output and negative input will lead to negative output.

When we shift bits of number towards right side by using >>> operator, the sign bit will be never copied into the resultant. We will always insert a bit 0 into  the sign bit of the result. i.e. the result will be always positive, whether the input is positive or negative.

Rule:

Bitwise operators can be applied to any combination of numbers without decimal point (byte, short, int, long, char)

Note:

The & | ^ operators can be applied to any combination of Boolean type or any combination of number without decimal point.

Conditional operators

This operator will perform a task based on condition.

The condition operators is ?:

Syntax:  (condition)? exp1:exp2

If the condition is satisfied , then it will evaluate the expression (expression1) available after question mark (?) and if the condition is not satisfied, then it will evaluate the expression (expression2) available after colon(:). The conditional operator is also called as ternary operator.

max= (x=y)? x:y

diff= (x>y)?x-y:y-x

5>6? “hai”:”bye”

The conditional operator can be nested any number of times.

2<3?4<5? “hai”:”bye”

New operators

The new operator is used for creating the object. Creating and object means allocating memory for a class in heap memory.

class Student{

}

Student stu= new Student();

 

Stu is called as the reference variable. It is used to refer or point to an object. The name of the reference variable can be valid java identifier.

For every class we can create any number of objects but without a class we can not create an object. The new operator can be object of predefined classes, and user defined classes.

Dot(.) operators

This operator can be used for accessing the members (variables and method) of a class or access a class from a package.

ClassName.variable

ClassName.method

 

reference.variable

reference.method

 

pacakagename.ClassName

 

Leave a Reply