# Parentheses in Expression

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.

**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).]

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

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).]

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).]

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

Sheriff

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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

**BODMAS**It is a mathematics rule for solving alegbraic equations & it is an abbreviation for

**B**racket

**O**pen

**D**ivide

**M**ultiply

**A**ddition

**S**ubtraction.

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!

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

Sheriff

JLS 15.7

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, fromleft 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).]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

Sheriff

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).]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

(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/)

Cheers, <img src="smile.gif" border="0"> <br /><a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">Manoj</a><br />(<a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">http://www7.brinkster.com/manoj9/</a>)

**associativity<>.?**

Bye.

Viki.

------------------

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

Bye.

Viki.

------------------

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

**
**

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

**
Marilyn de Queiroz
Sheriff
Posts: 9059
12
posted 14 years ago
**

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
Malar Ravi
Ranch Hand
Posts: 51

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).]

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

**
**