Posted By:
4 Feb, 2014 10:09 am PST

Operators in Java

OPERATORS 

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Miscellaneous Operators

The Arithmetic Operators: 

·  Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
·  Assume integer variable A holds 10 and variable B holds 20, then: 

Operator

Description

Example

+

Addition - Adds values on either side of the operator

A + B will give 30

-

Subtraction - Subtracts right hand operand from left hand operand

A - B will give -10

*

Multiplication - Multiplies values on either side of the operator

A * B will give 200

/

Division - Divides left hand operand by right hand operand

B / A will give 2

%

Modulus - Divides left hand operand by right hand operand and returns remainder

B % A will give 0

++

Increment - Increases the value of operand by 1

B++ gives 21

--

Decrement - Decreases the value of operand by 1

B-- gives 19

 

EXAMPLE:

 public class Test{

 public static void main(String[] args) {

 int a = 10;

 int b = 20;

 int c = 25;

 int d = 25;

 System.out.println("a + b = " + (a + b) );

 System.out.println("a - b = " + (a - b) );

 System.out.println("a * b = " + (a * b) );

 System.out.println("b / a = " + (b / a) );

 System.out.println("b % a = " + (b % a) );

 System.out.println("c % a = " + (c % a) );

 System.out.println("a++= " +(a++) );

 System.out.println("b--= " +(a--) );

 // Check the difference in d++ and ++d

 System.out.println("d++= " +(d++) );

 System.out.println("++d= " +(++d) );

 }

 }

  OUTPUT:

a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++= 10
b--= 11
d++= 25
++d= 27

 

 

The Relational Operators: 

There are following relational operators supported by Java language

Assume variable A holds 10 and variable B holds 20, then

Operator

Description

Example

==

Checks if the values of two operands are equal or not, if yes then condition becomes true.

(A == B) is not true.

!=

Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.

(A != B) is true.

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

(A > B) is not true.

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

(A < B) is true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(A >= B) is not true.

<=

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(A <= B) is true.

 EXAMPLE

 public class RelationalOperator{

 public static void main (String[] args) {

 int a=4, b=6, c=4;

 System.out.println (a==b);

 System.out.println(a !=b);

 System.out.println(a ==c);

 System.out.println (a !=c);

 }

 }              

OUTPUT

 

The Logical Operators: 

The following table lists the logical operators:

Assume Boolean variables A holds true and variable B holds false, then:

Operator

Description

Example

&&

Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

(A && B) is false.

||

Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.

(A || B) is true.

!

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

!(A && B) is true.

 

THE LOGICAL AND && OPERATOR 

This is a binary operator and expects two conditions for checking: one condition on the left of the && and one condition on the right of &&.These two conditions must each evaluate to a boolean expression.

The result is a true only if both the expressions evaluate to a true. Otherwise, the result is a false.

The second condition gets evaluated only if the first condition evaluates to a true. If the first condition evaluates to a false, the second condition does not get checked. 

We can write the if statementusing && in the following ways:

  

EXAMPLE:

class LogicalAnd {

public static void main (String[] args)

{

int time;

for(time = 10; time <=20; time = time + 4)

{

if (( time >=6) && (time <12) )

{

System.out.println( " Good Morning");

}

else

if ((time >=12) && (time < 16))

{

System.out.println("Good Afternoon");

}

else

if ((time >= 16) && (time <22) )

{

System.out.println("Good Evening");

}

else

{

System.out.println("Hello");

   }

  }

 }

}

Output: 

Another Example:

 

class LogicalAnd1 {

 public static void main(String [] args)

 {

 int x, y, z;

 boolean b;

 x = 5;

 y = 2;

 z = 2;

 b=(--x < 5 ) && ((x ++ ) < ( ++y + --z));

 System.out.println(x);

 System.out.println(y);

 System.out.println(z);

 System.out.println(b);

 }

 }

  OUTPUT:

 

EXPLANATION

 Expression 1 evaluates to a true since the value of x is reduced by 1 and then checked by 5. The value of x =4.

There, expression 2 also evaluates. In this, the increments and decrements takeplace. So, x = 4 + 1 = 5, y = 2 + 1 = 3, and z = 2 – 1 = 1. Expression 2 evaluates to 4 < ( 3 + 1 ) = false. So, b is false.

 THE LOGICAL OR || OPERATOR

 Similar to the logical AND operator we have the logical OR operator, which returns a true if at least one of the condition evaluates to a true. If all the conditions evaluate to a false, it returns a false.

The logical OR operator is represented by ||

The result is a true if at least one of the expression evaluate to a true. Otherwise, the result is a false.

The second condition gets evaluated only if the first condition evaluates to a false. If the first condition evaluates to a true, the second condition does not get checked.

 EXAMPLE

 class LogicalOr {

 public static void main (String[] args)

 {

 int x=1, y = 2, z = 3;

 boolean b;

 b=(++x > 1 ) || (--y> 1 ) || ( --z > 1);

 System.out.println(x);

 System.out.println(y);

 System.out.println(z);

 System.out.println(b);

 }

 }

  OUTPUT 

EXPLANATION

In this example, the first condition evaluates itself to a true, so we would not get the second and third conditions to evaluate at all. This you can see in the output.

 THE LOGICAL NOT ! OPERATOR

 The logical NOT operator reverses a true to a false and a false to a true. It is a unary operator in that it operates on one operand only. The operant must evaluate to a booleanvalue.

 EXAMPLE:

class LogicalNot {

public static void main (String [] args)

{

int dayOfTheWeek = 6;

boolean weekDay;

if ( ( dayOfTheWeek < 6))

weekDay =true;

else

weekDay =false;

if (!weekDay)

{

System.out.println("Have a nice weekend");

}

}

}

OUTPUT:

EXPLANATION: 

Above, since the dayOfTheWeekvariable is 6, it is a Saturday, which is a weekend. Therefore, the weekday variable is set to false. Using the NOT operator on this variable returns the opposite of false, which is true. This results in the if (!weekDay) condition being satisfied and the message being displayed on the output.

 The Assignment Operators:

There are following assignment operators supported by Java language:

 

Operator

Description

Example

=

Simple assignment operator, Assigns values from right side operands to left side operand

C = A + B will assign value of A + B into C

+=

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

C %= A is equivalent to C = C % A

<<=

Left shift AND assignment operator

C <<= 2 is same as C = C << 2

>>=

Right shift AND assignment operator

C >>= 2 is same as C = C >> 2

 EXAMPLE:

 public class AssignmentOperator{

 public static void main(String args[]) {

 int a = 10;

 int b = 20;

 int c = 0;

 c = a + b;

 System.out.println("c = a + b = " + c );

 c += a ;

 System.out.println("c += a= " + c );

 c -= a ;

 System.out.println("c -= a = " + c );

 c *= a ;

 System.out.println("c *= a = " + c );

 a = 10;

 c = 15;

 c /= a ;

 System.out.println("c /= a = " + c );

 a = 10;

 c = 15;

 c %= a ;

 System.out.println("c %= a= " + c );

 c <<= 2 ;

 System.out.println("c <<= 2 = " + c );

 c >>= 2 ;

 System.out.println("c >>= 2 = " + c );

 c >>= 2 ;

 System.out.println("c >>= a = " + c );

 }

 }

  OUTPUT:

 Miscellaneous Operators

 There are few other operators supported by Java Language.

 Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:

variable x =(expression)? value if true: value if false

Following is the example:

public class ConditionalOperator{

 public static void main(String args[]){

int a , b;

a =10;

b =(a ==1)?20:30;

System.out.println("Value of b is : "+b );

 b =(a ==10)?20:30;

System.out.println("Value of b is : "+ b );

}

}

 OUTPUT:

 

 Precedence of Java Operators:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category 

Operator 

Associativity 

Postfix 

() [] . (dot operator)

Left to right 

Unary 

++ - - ! ~

Right to left 

Multiplicative  

* / % 

Left to right 

Additive  

+ - 

Left to right 

Shift  

>> >>> <<  

Left to right 

Relational  

> >= < <=  

Left to right 

Equality  

== != 

Left to right 

Bitwise AND 

Left to right 

Bitwise XOR 

Left to right 

Bitwise OR 

Left to right 

Logical AND 

&& 

Left to right 

Logical OR 

|| 

Left to right 

Conditional 

?: 

Right to left 

Assignment 

= += -= *= /= %=>> = <<= &= ^= |= 

Right to left 

Comma 

Left to right 

 

Attachment: JAVABOOK.zip


Video is posted for  better understanding of the concept.

Attachment: Operators.zip
Was it useful?  



OPERATORS 

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Miscellaneous Operators

The Arithmetic Operators: 

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
 
Assume integer variable A holds 10 and variable B holds 20, then: 

Operator

Description

Example

+

Addition - Adds values on either side of the operator

A + B will give 30

-

Subtraction - Subtracts right hand operand from left hand operand

A - B will give -10

*

Multiplication - Multiplies values on either side of the operator

A * B will give 200

/

Division - Divides left hand operand by right hand operand

B / A will give 2

%

Modulus - Divides left hand operand by right hand operand and returns remainder

B % A will give 0

++

Increment - Increases the value of operand by 1

B++ gives 21

--

Decrement - Decreases the value of operand by 1

B-- gives 19

 

EXAMPLE:

 public class Test{

 public static void main(String[] args) {

 int a = 10;

 int b = 20;

 int c = 25;

 int d = 25;

 System.out.println("a + b = " + (a + b) );

 System.out.println("a - b = " + (a - b) );

 System.out.println("a * b = " + (a * b) );

 System.out.println("b / a = " + (b / a) );

 System.out.println("b % a = " + (b % a) );

 System.out.println("c % a = " + (c % a) );

 System.out.println("a++= " +(a++) );

 System.out.println("b--= " +(a--) );

 // Check the difference in d++ and ++d

 System.out.println("d++= " +(d++) );

 System.out.println("++d= " +(++d) );

 }

 }

 

OUTPUT: 

a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++= 10
b--= 11
d++= 25
++d= 27

 

The Relational Operators: 

There are following relational operators supported by Java language

Assume variable A holds 10 and variable B holds 20, then

Operator

Description

Example

==

Checks if the values of two operands are equal or not, if yes then condition becomes true.

(A == B) is not true.

!=

Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.

(A != B) is true.

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

(A > B) is not true.

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

(A < B) is true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(A >= B) is not true.

<=

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(A <= B) is true.

 EXAMPLE

public class RelationalOperator{

 public static void main (String[] args) {

 int a=4, b=6, c=4;

 System.out.println (a==b);

 System.out.println(a !=b);

 System.out.println(a ==c);

 System.out.println (a !=c);

 }

 }              

 

OUTPUT 

 

The Logical Operators: 

The following table lists the logical operators:

Assume Boolean variables A holds true and variable B holds false, then:

Operator

Description

Example

&&

Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

(A && B) is false.

||

Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.

(A || B) is true.

!

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

!(A && B) is true.

 

THE LOGICAL AND && OPERATOR 

This is a binary operator and expects two conditions for checking: one condition on the left of the && and one condition on the right of &&.These two conditions must each evaluate to a boolean expression.

The result is a true only if both the expressions evaluate to a true. Otherwise, the result is a false.

The second condition gets evaluated only if the first condition evaluates to a true. If the first condition evaluates to a false, the second condition does not get checked. 

We can write the if statementusing && in the following ways:

  

EXAMPLE:

class LogicalAnd {

public static void main (String[] args)

{

int time;

for(time = 10; time <=20; time = time + 4)

{

if (( time >=6) && (time <12) )

{

System.out.println( " Good Morning");

}

else

if ((time >=12) && (time < 16))

{

System.out.println("Good Afternoon");

}

else

if ((time >= 16) && (time <22) )

{

System.out.println("Good Evening");

}

else

{

System.out.println("Hello");

   }

  }

 }

}

 

Output: 

 

Another Example:

class LogicalAnd1 {

 public static void main(String [] args)

 {

 int x, y, z;

 boolean b;

 x = 5;

 y = 2;

 z = 2;

 b=(--x < 5 ) && ((x ++ ) < ( ++y + --z));

 System.out.println(x);

 System.out.println(y);

 System.out.println(z);

 System.out.println(b);

 }

 }

 

 OUTPUT: 

 

EXPLANATION

Expression 1 evaluates to a true since the value of x is reduced by 1 and then checked by 5. The value of x =4.

There, expression 2 also evaluates. In this, the increments and decrements takeplace. So, x = 4 + 1 = 5, y = 2 + 1 = 3, and z = 2 – 1 = 1. Expression 2 evaluates to 4 < ( 3 + 1 ) = false. So, b is false.

 

THE LOGICAL OR || OPERATOR

 Similar to the logical AND operator we have the logical OR operator, which returns a true if at least one of the condition evaluates to a true. If all the conditions evaluate to a false, it returns a false.

The logical OR operator is represented by ||

The result is a true if at least one of the expression evaluate to a true. Otherwise, the result is a false.

The second condition gets evaluated only if the first condition evaluates to a false. If the first condition evaluates to a true, the second condition does not get checked.

 

EXAMPLE

class LogicalOr {

 public static void main (String[] args)

 {

 int x=1, y = 2, z = 3;

 boolean b;

 b=(++x > 1 ) || (--y> 1 ) || ( --z > 1);

 System.out.println(x);

 System.out.println(y);

 System.out.println(z);

 System.out.println(b);

 }

 }

 

 

OUTPUT 

 

EXPLANATION

In this example, the first condition evaluates itself to a true, so we would not get the second and third conditions to evaluate at all. This you can see in the output.

 

THE LOGICAL NOT ! OPERATOR

The logical NOT operator reverses a true to a false and a false to a true. It is a unary operator in that it operates on one operand only. The operant must evaluate to a booleanvalue.

 

EXAMPLE:

class LogicalNot {

public static void main (String [] args)

{

int dayOfTheWeek = 6;

boolean weekDay;

if ( ( dayOfTheWeek < 6))

weekDay =true;

else

weekDay =false;

if (!weekDay)

{

System.out.println("Have a nice weekend");

}

}

}

 

OUTPUT:

 

EXPLANATION: 

Above, since the dayOfTheWeekvariable is 6, it is a Saturday, which is a weekend. Therefore, the weekday variable is set to false. Using the NOT operator on this variable returns the opposite of false, which is true. This results in the if (!weekDay) condition being satisfied and the message being displayed on the output.

 

The Assignment Operators

There are following assignment operators supported by Java language:

Operator

Description

Example

=

Simple assignment operator, Assigns values from right side operands to left side operand

C = A + B will assign value of A + B into C

+=

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

C %= A is equivalent to C = C % A

<<=

Left shift AND assignment operator

C <<= 2 is same as C = C << 2

>>=

Right shift AND assignment operator

C >>= 2 is same as C = C >> 2

 EXAMPLE:

public class AssignmentOperator{
 public static void main(String args[]) {
 int a = 10;
 int b = 20;
 int c = 0;
 c = a + b;
 System.out.println("c = a + b = " + c );
 c += a ;
 System.out.println("c += a= " + c );
 c -= a ;
 System.out.println("c -= a = " + c );
 c *= a ;
 System.out.println("c *= a = " + c );
 a = 10;
 c = 15;
 c /= a ;
 System.out.println("c /= a = " + c );
 a = 10;
 c = 15;
 c %= a ;
 System.out.println("c %= a= " + c );
 c <<= 2 ;
 System.out.println("c <<= 2 = " + c );
 c >>= 2 ;
 System.out.println("c >>= 2 = " + c );
 c >>= 2 ;
 System.out.println("c >>= a = " + c );
 }
 }

 

  OUTPUT:

 Miscellaneous Operators

 There are few other operators supported by Java Language.

 Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:

 

variable x =(expression)? value if true: value if false

 

Following is the example:

public class ConditionalOperator{

 public static void main(String args[]){

int a , b;

a =10;

b =(a ==1)?20:30;

System.out.println("Value of b is : "+b );

 b =(a ==10)?20:30;

System.out.println("Value of b is : "+ b );

}

}

 

 OUTPUT:

 

 

Precedence of Java Operators:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category 

Operator 

Associativity 

Postfix 

() [] . (dot operator)

Left to right 

Unary 

++ - - ! ~

Right to left 

Multiplicative  

* / % 

Left to right 

Additive  

+ - 

Left to right 

Shift  

>> >>> <<  

Left to right 

Relational  

> >= < <=  

Left to right 

Equality  

== != 

Left to right 

Bitwise AND 

Left to right 

Bitwise XOR 

Left to right 

Bitwise OR 

Left to right 

Logical AND 

&& 

Left to right 

Logical OR 

|| 

Left to right 

Conditional 

?: 

Right to left 

Assignment 

= += -= *= /= %=>> = <<= &= ^= |= 

Right to left 

Comma 

Left to right 


Was it useful?  




Please login to reply to this problem.