• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator precedence

 
Nitin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.
class tt1 {
public static void main (String args []) {
int k=1;
int i = ++k + k++ + +k;
System.out.println("k = " + k);
}
}

This gave the ans as 3. But as per the operator precedence rules:
1.Postfix unary operator (x++) is evaluated ahead of unary prefix (++x) and
unary + operator. All of these unary operators are right assocoative.

So should'nt the val of i be evaluated as:

int i = (++k + ((k++) + +k));
= (++k + (1 + +2)); // k=2
= 3+6 = 6

2. double x=4.5;
x = x + ++x;

Same with this stmt. ++x should be evaluated ahead of x +. So that val of x is:
x = x + 5.5 //x=5.5
x= 11
But this gives ans as 10 ?

Can someone plz explain this ?

Nitin
 
Corey McGlone
Ranch Hand
Posts: 3271
 
Udaya Raj
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only K is printed and not i. So the value of k will be 3 only
 
Nitin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey. This has cleared all the doubts.

But just one thing, i was going thru this example which u had given:

int i = 0;
for (; i < 12
{
i = i++;
}

Does this go into a loop only because we are assigning to the same variable.
Because the way this stmt wud evaluate is i will be assigned to 0 and then it will be incremented to 1.

Thanks for ur help.
Nitin
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitin:
Does this go into a loop only because we are assigning to the same variable.
Because the way this stmt wud evaluate is i will be assigned to 0 and then it will be incremented to 1.


Nitin,

Yes, the reason this goes into a loop is because we are assigning back to the same variable. The variable i is incremented to 1 but, because that example uses the post-increment operator, we assign the previous value of i, 0, to i. Therefore, we never get out of that loop.

In addition, I would appreciate it if you would make a conscious effort to use proper grammar on JavaRanch. For many people here, English is not their native language. Using "IM Talk" such as "wud" instead or "would" only makes things more difficult. For me (English is my native language), I find posts that have excessive "IM Talk" to be difficult to read and, more often than not, I'll skip them without responding.
[ November 04, 2004: Message edited by: Corey McGlone ]
 
Nitin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for confirming the same Corey. I'll make it a point.

Nitin
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,
I followed your inc/Decrement operators above and was working through them and when i try to confirm how this one comes out and i cant. I used int y instead of x for the main problem and then came back through and tried to step it through with the x's and they dont agree.


public class Delta1{

public static void main(String[]argv ){
int x = 0 ;
int y = 0 ;
y = ++y + y++ + y-- - ++y + y++ + ++y;
System.out.println(y);
x = x + 1;
System.out.println(x);
x = x + x + 1;
System.out.println(x);
x = x + x - 1;
System.out.println(x);
x = x - ++x;
System.out.println(x);
x = x + x + 1;
System.out.println(x);
x = x + ++x;
System.out.println(x);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic