# Parentheses in Expression

Malar Ravi
Ranch Hand
Posts: 51
According to operator precedence, parenthesis(), square brakets[] and dot operator have the highest precedence. Generally in an expression parentheses are used to alter the precedence of an operation.
int k = 1;
k=k*2+(k=4);
System.out.println("k = " + k);
Prints k = 6.
Here is my doubt... if parantheses has the hightest precedence the above expression should be evaluated as

k = 4*2+4; the final value of k should be 12.
Please bear with me if this question sounds very silly. Can someone explain what I am missing to understand in operator precedence in the above code.
Thanks,
Malar.

Vikrama Sanjeeva
Ranch Hand
Posts: 760
Yes Ravi.It was a bit confusing when i saw this type of question about couple of months agao.But at that time i concluded that, this is a special question of its type.However a/c to me JVM uses BODMAS rule.And a/c to BODMAS brackets should be open first.Therefore JVM just opens the bracket then it used precedence rules.
Well, then where the brackets executes first?What about the priority of brackets.Then a/c to me brackets ruls is apply where nested brackets are used.
I am not sure.Any body with any other good logic will be highly appreciated.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
[This message has been edited by Vikrama Sanjeeva (edited November 21, 2001).]

Malar Ravi
Ranch Hand
Posts: 51
Hi Viki, I dont think I understand your point. See the code below where the expression inside parentheses is evaluated first altering the normal precedence.
1: int k = 2;
2: k=k*(2+3); // k is 10
But without parentheses, * has higher precedence than + operator and hence k will be 7, this is quite obvious.
I am still not able to understand how that expression in my previous post is evaluated.
Thanks,
Malar.
[This message has been edited by Malar Ravi (edited November 21, 2001).]

Vikrama Sanjeeva
Ranch Hand
Posts: 760
OK.No problem Ravi.I try my best to explain it again.
The expression will be executed a/c to BODMAS(this is what i think u may confirm it.).And a/c to BODMAS here is the evaluation.
k=k*2+(k=4);
1: First the brackets will be open i.e it should be sloved
Therefore K=K*2+4; //Since Brackets are open(k=4)
2: Now the precedence will be check by JVM.
Therefor * > + > =
3: According to above precedence *will be applied
K=1*2+4;
4 Now * will be applied
K=2+4;
5: Now + will be applied
Therefore
K=6;
6 Now = will be applied
Therfore
System.out.print(K); prints 6
Hope now it will be clear to you.
But remember this is my logic.U may confirm it from other resource.
Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
[This message has been edited by Vikrama Sanjeeva (edited November 21, 2001).]

Malar Ravi
Ranch Hand
Posts: 51
Viki, according to your explaination dont you think k should be 4 in step:3 so that k=4*2+4; since k is assigned 4 in step:1 ???
BTW I have never heard of "BODMAS", can you explain that as well.
Thanks.

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
malar,
paranthesis have a higher precedence than *,+ but here you have to know that the evaluation is done from left to right so here it goes:
first k gets the value 1
then in k*2+(k=4) the value of k is used (i.e. 1) to multiply 2. Now we have 1*2 +(k=4). 1*2 yields 2. The expression is now 2+(k=4). The left hand side of the addition is 2 and the right-hand side is (k=4). k is assigned a new value of 4 and that value is used in the addition. Now we have 2+4 which is 6.
A high precedence means that in case of multiple possible evaluation path the paranthesis make the decision of who gets evaluated first, but still the evaluation occurs from left to right.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

Vikrama Sanjeeva
Ranch Hand
Posts: 760

BODMAS It is a mathematics rule for solving alegbraic equations & it is an abbreviation for
Bracket Open Divide Multiply Addition Subtraction.

The left to right or right to left rule's is applied when there is two or more operators of same precedence.Here all operators are diffrenet.
A/c to various Java Books, If two or more operators of same precedence came in a same statement then there order of execution will be evaluated by there assosiativity.That is they will evaluate from left to right or from right to left.
Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
Vikrama
JLS 15.7

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

Whatever operators you have if you don't modify yourself the evaluation order by means of paranthesis then the expression is evaluated from left to right !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 21, 2001).]

Vikrama Sanjeeva
Ranch Hand
Posts: 760
Valentin, Can you provide the JLS15.7 Link.And can you explain this topic by some good example.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
Sorry
I forgot to include the link. Here it is:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4779
Some examples are provided on the link
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 21, 2001).]

Manoj Gupta
Greenhorn
Posts: 29
Look at following expression as individual expressions:
(the comment text is the expression result for each case)
Note: consider k=1 for start of each statement.
k=k*2+(k=4); // 6
k=(k=4)+k*2; // 12
k=k*2+(k+=4); // 7
k+=k*2+(k=4); // 7
k+=k*2+(k+=4); // 8
k+=(k+=4)+k*2; // 16
k=(k+=4)+k*2; // 15
Read JLS (15.7.1 Evaluate Left-Hand Operand First) http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4779
Try to understand each of the above cases by relating them to the text from JLS. If you still need some help understanding this, ask for more.
------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)

Vikrama Sanjeeva
Ranch Hand
Posts: 760
Thanks Valentin.I have readed the JLS15.7 but can't get the point .Anyway i am also refering other books as well.Good explanation Manjo.But where one has to use associativity<>.?
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!

Marilyn de Queiroz
Sheriff
Posts: 9067
12
int k = 1;
k=k*2+(k=4);
System.out.println("k = " + k);
Prints k = 6.

The difference is more obvious is you initialize k to 3 rather than 1.

int k = 3; // initialize k to 3
k = k * 2 + ( k = 4 ); // statement to evaluate
k = 3 * 2 + ( k = 4 ); // evaluate k in the left-most expression of the right-hand-side of the statement (k).
k = 3 * 2 + ( k = 4 ); // evaluate the next expression (2)
k = 3 * 2 + ( 4 ); // evaluate k in the next expression
k = 6 + 4; / multiplication has precedence over addition
k = 10; // assign the result to k

Now if you change the parens to read
k = k * ( 2 + ( k = 4 ) );
you will get a different answer because the parens have precedence over multiplication.

and if you change the statement to read
k = ( k = 4 ) + k * 2;
you will get a still different answer because k is being changed in the first (left-most) expression on the RHS of the statement.

[This message has been edited by Marilyn deQueiroz (edited November 22, 2001).]

Malar Ravi
Ranch Hand
Posts: 51
Now I got the point. Thanks Valentin, Manoj & Marilyn.

Malar Ravi
Ranch Hand
Posts: 51
I am sorry to bring up this topic again. Can someone tell me why line 5 is not ok.
int x=1, y = 2, z=3;
boolean b = true;
x = b ? y : z ; //1
x = b ? (x * y) : (x * z); //2
x = b ? (x = y) : (x = z); //3
x = b ? x * y : x * z ; //4
x = b ? x = y : x = z; //5
Thanks,
Malar.