Operators in programming


Operators are symbols in java that are used to perform amazing operations on your data.Some of these symbols are &,^,*
and ! .Java consists of huge variety of operators that are as follows.

1) Assignment Operators
Assignment operator assigns a value or value of a variable to other variable. Equal sign '=' is used as assignment operator
in java.Two operands are required to perform this operation.Both can be variable bot both can't be constants.

Syntax
variable=value;
or
variable=other_variable;

Example:
int a;
int b=10;

a=10;

or
a=b;

LValue and RValue problem in assignment operator
Lvalue is an operand that is always written at the left side of the assignment operator.It must be a single value.Rvalue is
an operand that is written at the right side of the assignment operator.It can be more then one variable or more then one
value.Constants can't be used as Lvalue.

Invalid expression Example

a*a=b ;           //Wrong Lvalue
5=c;                 //Lvalue can't be a constant

Valid Expression Example

b=a*a;
c=5;


2)Arithmetic Operators

Arithmetic operators performs mathematical operations on data like sum of five numbers.Java provides five arithmetic operators that are as follows.

i) Addition (+)
Addition operator can add two or more then two numbers or variables.

ii) Subtraction (-)
Subtraction operator can subtract two or more then two numbers or variables.

iii) Division Operator (/) 
It is used to divide one number with another number.Forward slash is used as division operator in java.

iv) Multiplication Operator (*)
It is used to multiply variables or numbers.Asterisk is used as multiplication operator in java.

v) Modulus Operator (%)
Whenever division is performed on two numbers their must be a remainder that might be 0 or more then zero.Modulus operator
is used to find remainder in java.Percentage sign is used as modulus operator in java.

3) Increment and Decrement Operators
These operators are used to increment or decrement the value of a variable by one.there are two types of both increment  and decrement operators.

Increment Operator
It uses two plus signs to increment a number.
Example:
a++
b++
++a
++n
Prefix form
In prefix form increment operator in written before the variable.Prefix operator first increments the value and then
 assigns value to a variable if any.

Post fix form 
In post fix form increment operator in written After the variable.Prefix operator first assigns the value and then
increments the value by one.
Prefix and post fix example:
a=++b
a=b++

Both will generate Different Result

Decrement Operator
It uses two subtraction signs to decrement a value.
Example
a--
b--

Prefix and Post fix
Prefix and post fix form of decrement operator are same as increment operator.Just sign will be changed.
a=--b
a=b--

Both will generate Different Result.If we wanted to perform more then one decrement or increment then these operators will not be used.To perform more then one increment or decrement use  assignment operator and add or subtract the value with the same variable.

Ten numbers increment
a=a+10;
Ten numbers Subtraction
a=a-10;

4) Logical Operators
Logical Operators are used to evaluate compound conditions.It produces only two results 'true' or 'false'.There are three types of operators in java.

i)And Operator
Two and signs are used for and operator '&&'.Result will be true if both are true other wise result will be false.



ii)Or Operator
Two or signs are used for or operator '||'.Result will be false if both are false, otherwise result will be true.

iii) Not Operator
Not operator is used to reverse the result of a given condition.If condition is false result will be true and if condition is true result will be false.Sign of exclamation is used as not operator '!'.


5)Relational Operators
Relational operators are used to compare two conditions.Their result also comes in true or false form.

Greater than operator (>)
It returns true if value at left side is greater than value at right side.Otherwise it returns false.
5>4  //Returns True
6>7  //Returns False

Less than operator (<)
It returns true if value at right side is greater than value at Left side.Otherwise it returns false.
3<4  //Returns True
8<7  //Returns False

Greater than or equal  operator (>=)
It returns true if value at left side is greater than or equal to the value at right side.Otherwise it returns false.
5>4  //Returns True
3>=3 //Returns True
6>7  //Returns False

Less than equal operator (<=)
It returns true if value at right side is greater than or equal to the value at left side.Otherwise it returns false.
3<4  //Returns True
3<=3 //Returns True
8<7  //Returns False


Equal Operator(==)
It returns true if values at both sides are same otherwise it will return false.
4==4  //Returns True
4==3 //Returns False

Not Equal Operator(!=)
It returns true if values at both sides are not same otherwise it will return false.
5!=4  //Returns True
3!=3 //Returns False
Reactions

Post a Comment

0 Comments