# operator precedence

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

- 0

hi,

what i dont underatand in the code below is about operator precedence.

class demo

{ static int x;

public static void main(String[] args)

{int i=5*(2+6);// case 1

System.out.println(+i);

int y = z() * (z() + z());//case 2

System.out.println(y);

}

private static int z() {

return ++x;

}

}

this prints out 40 and 5

case 1 and case 2 are similar except that methods have replaced variables.

i know that Java evaluates the operands of each operator in a left-to-right manner but if that is the case then in the first case the result should have been 16 ie 5*2=10 +6=16; but it is not so.

if braces offer precedence then case 1 is correct to print out 40 but then case 2 must print out 9.

regds

Rahul.

what i dont underatand in the code below is about operator precedence.

class demo

{ static int x;

public static void main(String[] args)

{int i=5*(2+6);// case 1

System.out.println(+i);

int y = z() * (z() + z());//case 2

System.out.println(y);

}

private static int z() {

return ++x;

}

}

this prints out 40 and 5

case 1 and case 2 are similar except that methods have replaced variables.

i know that Java evaluates the operands of each operator in a left-to-right manner but if that is the case then in the first case the result should have been 16 ie 5*2=10 +6=16; but it is not so.

if braces offer precedence then case 1 is correct to print out 40 but then case 2 must print out 9.

regds

Rahul.

zhewen

Ranch Hand

Posts: 35

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

- 0

Hi Rahul.

U are right about the execution of the statement and that arithmetic operators group left to right.But remember that this grouping is important only in the case of operators of equal precedence.Here * has a greater precedence than +.And () has highest precedence.Removing () will give 16 and 9 respectively.

But ,I think, for static members , they are initialized at class load time in order of appearance from left to right.So first x=1 and then 1*(2+3) because of the () operator.Looking at this way the answer will come out to be 5.

Please correct me If I am wrong.I am not sure on this one.

I request the moderators to shed some light on this one.

Thanks in advance.

U are right about the execution of the statement and that arithmetic operators group left to right.But remember that this grouping is important only in the case of operators of equal precedence.Here * has a greater precedence than +.And () has highest precedence.Removing () will give 16 and 9 respectively.

But ,I think, for static members , they are initialized at class load time in order of appearance from left to right.So first x=1 and then 1*(2+3) because of the () operator.Looking at this way the answer will come out to be 5.

Please correct me If I am wrong.I am not sure on this one.

I request the moderators to shed some light on this one.

Thanks in advance.

Ajay Kumar

Ranch Hand

Posts: 87

posted 15 years ago

- 0

Hi Rahul,

In the first case the () has got the highest precedence. The expression will be evaluated as 5*(8)=40.

However in the second case since U are invoking methods, the operation on x is the same as the order in which the method is called.

Here the left most z() is evaluated first as 1.

The expression becomes

1*(z() + z()) with the value of x=1

Now the first z() inside the bracket is evaluated returning a value of 2

the expression becomes 1*(2 + z());

Finally the lst z() is evaluated to become the expression

1* (2 + 3) = 5. Hence the answer U get.

Hope this helps

Thanks

Ajay Kumar

In the first case the () has got the highest precedence. The expression will be evaluated as 5*(8)=40.

However in the second case since U are invoking methods, the operation on x is the same as the order in which the method is called.

**int y = z()* (z() + z());**Here the left most z() is evaluated first as 1.

The expression becomes

1*(z() + z()) with the value of x=1

Now the first z() inside the bracket is evaluated returning a value of 2

the expression becomes 1*(2 + z());

Finally the lst z() is evaluated to become the expression

1* (2 + 3) = 5. Hence the answer U get.

Hope this helps

Thanks

Ajay Kumar

Regds<BR>Ajay Kumar

Anonymous

Ranch Hand

Posts: 18944

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

- 0

hi udyan,

if () have the highest precedence then

public class Tester {

public static void main(String[] args) {

int i = 0;

byte b = 0;

b += b -i+ (i= 10);

System.out.println(b);

System.out.println(i);

}

}

should print out 0 but it prints out 10 which is evaluation from left to right.

Help!!!

Regds.

Rahul.

[This message has been edited by rahul_mkar (edited June 21, 2000).]

if () have the highest precedence then

public class Tester {

public static void main(String[] args) {

int i = 0;

byte b = 0;

b += b -i+ (i= 10);

System.out.println(b);

System.out.println(i);

}

}

should print out 0 but it prints out 10 which is evaluation from left to right.

Help!!!

Regds.

Rahul.

[This message has been edited by rahul_mkar (edited June 21, 2000).]

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

- 0

I got the answer to ur question when I changed ur code a bit.I made the method invocation statement as z()+z()*z().The result of this code was 7.How this will evaluate is

1)The statement evaluates from left to right.So first z() is called and x=1.

2)Now the compiler sees * has a higher priority than +.So value of first z() ie x=1 is stored in temp location

3) Again z() is called and now x=2

4) Again z() is called and now x=3

5)2*3 gives 6 which is added to 1 obtained earlier and finally y=7

All statements are fully evaluated before execution proceeds to the next statement.Extending the same explanation to the code u have put up now u will get ur answer of b=10 and i=10

1)The statement evaluates from left to right.So first z() is called and x=1.

2)Now the compiler sees * has a higher priority than +.So value of first z() ie x=1 is stored in temp location

3) Again z() is called and now x=2

4) Again z() is called and now x=3

5)2*3 gives 6 which is added to 1 obtained earlier and finally y=7

All statements are fully evaluated before execution proceeds to the next statement.Extending the same explanation to the code u have put up now u will get ur answer of b=10 and i=10

Suma Narayan

Ranch Hand

Posts: 136

posted 15 years ago

- 0

Hi Rahul,

This is my understanding and I hope it helps you.

There are few things which one needs to remember before evaluating an expression and they are:

All operands are evaluated left to right irrespective of the order of execution of operations. You agree to this. Right.

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

So, applying the above rules to both examples:

a. int i=5*(2+6);// case 1

We will evaluate from left to right. So considering 5*(2+6),the left hand operand (which is 5) is evaluated. Since 5 is a constant, there is nothing to evaluate and hence we keep it as 5.

b. int y = z() * (z() + z());//case 2

We will evaluate from left to right. So considering z()*(z()+z()), the left hand operand (which is z()) is evaluated. After evaluation(x=0 and so return ++x gives 1) the result is x=1.

So, now the above equation is

int y = 1 * (z() + z())

Java also guarantees that every operand of an operator (except the conditional operators &&, | |, and ? appears to be fully evaluated before any part of the operation itself is performed.

If the binary operator is an integer division / or integer remainder % , then its execution may raise an ArithmeticException, but this exception is thrown only after both operands of the binary operator have been evaluated and only if these evaluations completed normally.

a. int i=5*(2+6);// case 1

so, int i = 5*(2+6) is = 5*(2+6) as 2 and 6 are constants and there is nothing to evaluate.

Remember, we have still not started

the operation and hence the equation still has * and () intact.

int y = 1 * (z() + z())

z() is evaluated twice and we get 2 and finally 3 from the evaluation of z(). So, the equation is now

int y = 1 * (2 + 3).

Remember, once again we have still not started the operation and hence the equation still has + and () intact.

Java implementations must respect the order of evaluation as indicated explicitly by parentheses and implicitly by operator precedence.

So parenthesis gets the first preference before other operators in the operator precedence.

So now we have

a. int i=5*(2+6);// case 1

int i = 5 * (8)

b. int y = 1 * (2 + 3);// case 2

int y = 1 * (5)

and finally we get i = 40 and y = 5.

I hope the above explanation helps you and if you want to know more then I suggest that you read topic

Suma

[This message has been edited by Suma Narayan (edited June 21, 2000).]

This is my understanding and I hope it helps you.

There are few things which one needs to remember before evaluating an expression and they are:

**1. Evaluation Order:**All operands are evaluated left to right irrespective of the order of execution of operations. You agree to this. Right.

**2. Evaluate Left-Hand Operand First:**The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

So, applying the above rules to both examples:

a. int i=5*(2+6);// case 1

We will evaluate from left to right. So considering 5*(2+6),the left hand operand (which is 5) is evaluated. Since 5 is a constant, there is nothing to evaluate and hence we keep it as 5.

b. int y = z() * (z() + z());//case 2

We will evaluate from left to right. So considering z()*(z()+z()), the left hand operand (which is z()) is evaluated. After evaluation(x=0 and so return ++x gives 1) the result is x=1.

So, now the above equation is

int y = 1 * (z() + z())

**3. Evaluate Operands before Operation:**Java also guarantees that every operand of an operator (except the conditional operators &&, | |, and ? appears to be fully evaluated before any part of the operation itself is performed.

If the binary operator is an integer division / or integer remainder % , then its execution may raise an ArithmeticException, but this exception is thrown only after both operands of the binary operator have been evaluated and only if these evaluations completed normally.

a. int i=5*(2+6);// case 1

so, int i = 5*(2+6) is = 5*(2+6) as 2 and 6 are constants and there is nothing to evaluate.

Remember, we have still not started

the operation and hence the equation still has * and () intact.

int y = 1 * (z() + z())

z() is evaluated twice and we get 2 and finally 3 from the evaluation of z(). So, the equation is now

int y = 1 * (2 + 3).

Remember, once again we have still not started the operation and hence the equation still has + and () intact.

**4.Evaluation Respects Parentheses and Precedence:**Java implementations must respect the order of evaluation as indicated explicitly by parentheses and implicitly by operator precedence.

So parenthesis gets the first preference before other operators in the operator precedence.

So now we have

a. int i=5*(2+6);// case 1

int i = 5 * (8)

b. int y = 1 * (2 + 3);// case 2

int y = 1 * (5)

and finally we get i = 40 and y = 5.

I hope the above explanation helps you and if you want to know more then I suggest that you read topic

**Evaluation Order**in the JLSSuma

[This message has been edited by Suma Narayan (edited June 21, 2000).]

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |