Monday, July 5, 2010

Operators

Explain about the operators in java or Explain about the types of operators
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations . java operators can be classified into a number of related categories.

Arithmetic operators
Relational operators
Logical operators
Assignment operator
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators


Arithmetic operators

Java provides all the basic arithmetic operators. The operator +,-,*, / all work the same way as they do in other language.
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo operation

Relational operators:

Relational operators are used to compare two quantities. Java support six relational operators.
< is less than
<=is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to

Logical operators:
In addition to the relational operators. Java has three logical operators which are given below.

&& logical AND
|| logical OR
! logical not
Example
a>b && x==10;
An expression of this kind which combines two or more relational expression is termed as logical expression or compound relational expression. Logical expression yields a value of true or false .

Assignment operators

Assignment operators are used to assign the value of an expression to a variable . the operator is = java has a set of short hand assignment operators which are used in the form.
V op=exp;
Where v is the variable exp is as expression and op is a java binary operator. The operator op = is known as shorthand assignment operator.

Advantage
What appear on the left hand side need not be repeated and therefore it becomes easier to read.
The statement is more concise and easier to read
Efficient code as result


Increment and decrement operators:


Java has two very useful operators not generally found in many other languages. These are increment and decrement operators ++ and --.
The operator ++ adds 1 to the operand while - - subtract 1. Both are unary operators and are used as follows
++m or m++
--m or m - -

++m and m++ means the same thing when they form statement independently. They behave differently when they are used in expressions on the right-hand side of an assignment statement.
Considered the following example
m=5
Y=++m

in this case the value of y and m would be 6 suppose if we rewrite the above statement as
m=5
Y= m++
then the value of y would be 5 and m would be 6.

Prefix operators first add 1 to the operand and then the result is assigned to the variable on the left.

Post fix operator first assigns the value to the variable on left and then increment the operand.

Example

class demo
{
public static void main(String asd[])
{
int x=10,y=20;
System.out.println(“m=”+x);
System.out.println(“n=”+y);
System.out.println(“++x=”+ ++ x);
System.out.println(“y++=”+ y++);
}
}

Conditional operators


The character pair ?: is a ternary operator avialable in java. This operator is used to construct conditional expression of the form.
exp1? exp2 : exp3

Where exp1,exp2,exp3 are the expressions. The operators ?: works as follows exp1 is evaluated first. If it is non zero then the exp2 is evaluated and becomes the value of the conditional expression. If the exp1 is false exp3 is evaluated and its value becomes the value of the conditional expression.

Example
int a=10;
int b=15;
x=(a>b) ? a :b;

x will be assigned the value of b. this can be achieved using the if-else statements as follows

if(a>b)
{
x=a;
}
else
{ x=b; }

Bit wise logical operators
Java has a distinction special operators known as bitwise operators for manipulation of data at values of bit level. These operators are used for testing the bits or shifting them to the right or left. Bit wise operators may not be applied to float or double.

1 comment: