ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Basic Operators

 Basic Operators

These are the types of operators in java:

  • Arithmetical operators: These are applied to perform arithmetical calculations like addition,subtraction,multiplication ,division ,modulus (Returns the remainder).

Mathematical expression:  abc

Java expression:         a*b*c

Unary increment and decrement operators:

These operators increase or decrease the value of an operand by 1 respectively.

Prefix:When the increment or decrement operator is applied before the operand we call it prefix operator.In this case,we change the value of the variable before the operation.

Example:

  1. int  x=5;

x=++x; /*x increases to 6 */

So,the final value of x is 6.

  1. int x=5;

x=–x;

In this case the value of x becomes 4.

Postfix:When the increment or decrement operator is applied after the operand we call it prefix operator.In this case,we change the value of the variable after the operation.

Example:

  1. int x=5;

x= x++ *4;

In this case x remains 5 before multiplication.So, we get 20 stored in x.

2.int x=5;

x=x– *5;

 

In this case x remains 5 before multiplication.So,the final value of x is 25.

  1. Relational operators:As the name suggests these operators show relationship among the operands.They result in either True or False.

1.Less than (<) : Example: 3<7 Results in True

2.Greater than(>):Example: 3>7   Results in False

3.Greater than or equal to(>=):Example:6>=6  Results in True

4.Less than or equal to(<=): Example: 7<=12 Results in True

5.Equal to(==):Example: 7==7 Results in True

6.Not equal to(!=):Example: 7!=7 Results in False

 

3.Logical Operators: Java uses AND(&&),OR(||) or NOT(!).

Logical AND: It results in true if both expressions are true else false.

Example: 

6>8 && 4!=5

This will result in false because the first condition is false.

Logical OR: It results in true if either condition is true.

Example:

6>8 || 4!=5

This will result in true because the first condition is true.

Logical NOT:It will simply revert the outcome of the expression.

Example:

!(10==11)

This will result in true.

 

4.Bitwise operators: These perform operations on bit level of operands.These can be applied to byte,short,int,long and char.

Bitwise AND(&):It will give high(1)if both operands are high else low(0).

Eg.

1&0   results in 0.

Bitwise OR(|):It will give high if either operand is high.

Eg.

1|0   results in 1.

Bitwise complement(~):It will give the complement.

Bitwise XOR(^):It results in high if we have different value of operands and low if we have same value of operands.

Eg.

1^0 results in 1.

0^0 results in 0.

Left shift(<<):

Example:int x=3;

x<<2;

Now the binary of 3 is 11.

We need to shift it left by 2 bits.

So,the final value becomes 1100 whose decimal equivalent is 12.

Right shift:

Example:

int x=12;

x>>2;

Now the binary of 12 is 1100.

We need to shift it right by 2 bits.

So,the final value becomes 11 whose decimal equivalent is 3.

 

Zero fill right shift (<<<):

Example:

int x=14;

x<<<2;

Now the binary of 14 is 1110

Shift the value right by two bits and fill the shifted values with 0.So,the value comes out to

be 3.

 

5.Assignment operators:

=   It assigns the value like a=10;

+=  Example: int a=1;a+=4 /*a=a+4*/;this results in change in value of a to 5

-=  Example: int a=4;a-=1;this results in change in value of a to 3.

*=  Example:int a=2;a*=3;this results in change in value of a to 6.

/=  Example: int a=4;a/=1;this results in change in value of a to 4.

%=  Example:int a=8;a%=3;this results in change in value of a to 2.

<<=  a<<=2 is same as a=a<<2

>>=  a>>=2 is same as a=a>>2

&=   a&=3is same as a=a&2

 

6.Miscellaneous

Ternary operator(Conditional Assignment)

They deal with three operands.

Syntax:

Variable=(test expression)?value if true:value if false.

 

Eg.

int a=12;b=13;

int c=(a==b)?12:13;

Since the expression is false so c will store value 13.

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!