Saturday, July 2, 2016

Operators

Operators:

An operator is a symbol that performs an operation. An Operator acts on some variables, called operands to get the desired result





Arithmetic Operators ( Binary Operators because they act on two operands always)
 + , - ,* ,/ and %

Addition operator also use to join two strings,so its also called as String Concatenation operator.

For example:
String str1 ="Net";
String str2 = "Solutions";
String Joinstr = str1+str2;

Unary Operators 
This operator act on only one operand
 Increment Operator (++)
Decrement Operator(--)

Increment Operator –This operator increment the value of variable by 1
int x =1;
int y =x++; //Post Increment
int z =++x; // Pre Increment
System.out.println("Post Increment operator would be: "+y);
System.out.println("Pre Increment operator would be: "+z);

Output:
Post Increment operator would be: 1
Pre Increment operator would be: 3

Decrement Operator –This operator  decrement the value of variable by 1
int x =1;
int y =x--; (Post Decrement)
int z =--x; (Pre Decrement)
System.out.println("Post Decrement operator would be: "+y);
System.out.println("Pre Decrement operator would be: "+z);

Output
Post Decrement operator would be: 1
Pre Decrement operator would be: -1

Assignment Operator  (=)–This operator  is used to store some value into a variable.
for example:int x = 20;

Relational Operator: These operators are used to comparing the values.
  > greater than operator
  >= greater than or equal to
  < less than operator
  <= less than or equal to
  == equal to operator
  != not equal to operator


Mainly these relational operator is used in the conditional statement for example
int a=10;
int b=20;
if (b>a){
System.out.println("b is greater than a");
}

Logical Operator:
These operators are used to construct compound conditions.A compound condition is a combination of several simple conditions.
&& and operator
|| or operator
! not operator
for example :
int a=10;
int b=20;
if (a==10 || b==20);{
System.out.println("Value of a and b is :"+a+","+b);
}

Output:
Value of a and b is :10,20

Another example:
String str1 ="Net";
String str2 = "Solutions";
String Joinstr = str1+str2;
if (!str1.equals(str2)){
System.out.println("String 1 is not equal to string2");
}

Output:
String 1 is not equal to string2

Boolean Operators:
These operators act on boolean variables and produce boolean type result.
& boolean and operator
| boolean or operator
! boolean not operator

For example:
boolean d= true;
boolean e= false;
System.out.println("Value with and boolean operator is: "+(d&e));
System.out.println("Value with or boolean operator is: "+(d|e));
System.out.println("Value with not boolean operator is: "+(!e));

Output:
Value with and boolean operator is: false
Value with or boolean operator is: true
Value with not boolean operator is: true

New operator and Cast Operator will discuss  later

Thanks....

No comments:

Post a Comment