Fri. Nov 15th, 2024

Any symbol that performs an operation will be called as operators.

Operand: The values on which the operator are performed are called as operands.

a+b

here a & b are operands and + is operator

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

1) Unary Operators

These operators will perform operations on the operand.

2) Binary Operators

These operators will be performed operations on two operands.

3) Ternary Operators

These operators will be performed operators three operands.

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

1) Arithmetic Operators

These operators perform simple mathematical calculations. The various arithmetic operators are +,-,*, /, %. If both the operands are of integer type, then the result will be of integer type. If at least one of the operand is of floating point type, then the will be of floating point type.

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

2) 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 by 1. Based on the position of the increment operator, the increment operator is classified into 2 types.

PreIncrement(++X) 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(++X) 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 by 1. Based on the position of the decrement operator, the decrement operator is classified into 2 types.

Predecrement(–X) If the decrement operator is placed before the variable then it is called Predecrement operator. The Predecrement operator will use the value after decreasing.

Postdecrement(++X) If the decrement operator is placed after the variable then it is called postdecrement operator. The postdecrement operator will use the value before decreasing.

Program 3:

class Operators {
  public static void main (String[] args){
      int a=6;
      int b=++a;
      int c=b--;
      int d= a++ + --b - --c;
      system.out.println(a);
      system.out.println(b);
      system.out.println(c);
      system.out.println(d);
}
}

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

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

Rule3: The increment and decrement operators cant not be nested.

3) Assignment Operator

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 value or expression erc. 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 allowed because nested during decalration

int a,b,c,d;

a=b=c=d=6; valid because nested after declaration

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

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

4) Relational Operator:

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

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 true, otherwise the result will be false.

Rule1: The relational operators <,<=,>,>=, 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.

4) LogicalOperator:

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

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

AND Operators(&):

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.

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

FFF
FTF
TFF
TTT

AND Operators(&&):

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.

The && operator will evaluate all the first condition. If the result of the first condition is false, then it will skip the evaluation of remaining conditions and directly decide the result as false, but if the 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 the first condition is false.

FFF
FTF
TFF
TTT

OR Operators(|):

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 at least one of the condition are true.

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

FFF
FTT
TFT
TTT

OR Operators(||):

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.

The || operator will evaluate all the first condition. If the result of the first condition is true, then it will skip the evaluation of remaining conditions and directly decide the result as true, but if the 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 the first condition is true.

FFF
FTT
TFT
TTT

X-OR Operators(^):

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 inputs are same the the result will be false.

FFF
FTT
TFT
TTF

Not Operators(!):

This operator can be used for complimenting the result of 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.

6) 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

X=500000101
~X11111011

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

2’s complement= 1’s complement

11111010 
00000101 
       1 
00000110=-6

Note: ~x=x+1

Bitwise AND Operator (&):

The operator will perform AND operation on the bits of a number.

000
010
100
111
X=500000101 
Y=600000110 
X|Y00000100=4

Bitwise OR Operator (^):

The operator will perform OR operation on the bits of a number.

000
011
101
111
X=500000101 
Y=600000110 
X|Y00000111=7

Bitwise X-OR Operator (|):

The operator will perform OR operation on the bits of a number.

000
011
101
110
X=500000101 
Y=600000110 
X^Y00000011=3

Left Shift Operator(<<):

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

  Lost       
X=10 0001010 
X<<10001010 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 rightside by the specified number of positions.

        Lost
X=10 0001010
X<<1001010 0
 Inserted       

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

Unsigned Right Shift Operator(>>>):

This operator will shift the bits of a number towards right side 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 number will be copied as it is into the resultant i.e. positive input will lead to positive output and negative input will lead negative output.

When we shift the 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 operator can be applied to any combination of number without decimal point (byte, short, int, long, char).

Note:

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

7) Conditional Operataor

This operator will perform a task based on a condition.

syntax (condition)? exp1: exp2

If the condition is satisfied, when it will evaluate the expression (expression1) available after question mark (?) and if the condition is not satiesfied, 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? “hi”:”hello”:”bye”

8) New operator

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

class Student {

}

Student stu=new Student();

student———> object (Heap)

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

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

9) Dot (.) Operator

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

ClassName.variable

ClassName.method

referencd.variable

reference.method

packagename.ClassName

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *