Tuesday 3 February 2015

chapter 3.5 : 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 

chapter 3.4 :T h e Logical Operators, Boolean Logical Operators, The Assignment Operator,Misc Operators and Conditional Operator ( ? : ):



T h e Logical Operators :

T he following table lists the logical operators :

Assume Boole an variable s A holds true and variable B holds false , the n:



Operator

Description

Example

&&

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

(A && B) is false .

||

Called Logical OR Operator. If any of the two operands are non-zero, the n the condition be comes true .

(A || B) is true .

!

Called Logical NOT Operator. Us e to re verses the logical state of its operand. If a condition is true the n Logical NOT operator will make false .

!(A && B) is true .



Boolean Logical Operators :

The Boolean logical operators shown here operate only on boolean operands. All
of the binary logical operators combine two boolean values to form a resultant
boolean value.
Operator                                                                              Result

&                                                                                     Logical AND
|                                                                                       Logical OR
^                                                                                      Logical XOR (exclusive OR)
||                                                                                       Short-circuit OR
&&                                                                                  Short-circuit AND
!                                                                                       Logical unary NOT
&=                                                                                  AND assignment
|=                                                                                    OR assignment
^=                                                                                   XOR assignment
==                                                                                   Equal to
!=                                                                                    Not equal to
?:                                                                                   Ternary if-then-else




The Assignment Operator
The assignment operator is the single equal sign, =. The assignment operator works in Java much as it does in any other computer language. It has this general form:

var = expression;

Here, the type of var must be compatible with the type of expression. For example, consider
this fragment:

int x, y, z;
x = y = z = 100; // set x, y, and z to 100

This fragment sets the variables x, y, and z to 100 using a single statement.

Misc 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 Test {
 
   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 );
   }
}
This would produce the following result:
Value of b is : 30
Value of b is : 20