• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

precedence prob. again..this time 4rm RHE...very urgent

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class increment
{
static public void main(String incre[])
{
int x,a=6,b=7;
x= a++ + b++;//line bla
System.out.println("x="+x); //prints 13 (!!!@@@####???),my prob. //states here
System.out.println("a="+a); //o.k. print 7
System.out.println("b="+b); // print 8...it is also o.k.
/* this is the 1st case where according to RHE 'line bla' is evaluated like this particuler order... x =a + b;a=a++;b=b++ ; But my q. is as we know unary ++ opt. has highest precedence that = opt. or even that unary + opt. So...why this statement is executing like this way.
Another thing,if we execute the follwing 'line bla bla ' in this order
then y should print 6 (b'couse as per RHE ..this statement should evaluate like this way...y=old value of y + old value of y) BUT..this is not heppening here...accually...it is evaluating like this manner..y=y(3) + y(4);So..is in the last case.(y=y(7)+y(8)+y(9))
Now ...can u mixed it up with the 1st one...I can't ..Soo..I am posting for you..GURUS. */

int y=3;
y = y++ + y++; // line bla bla
System.out.println("y="+y); // 7
y = y++ + y++ + y++;
System.out.println("y after another incre="+y); // 24

//In addition..can any1 explain this following one
int z=4;
z = z-- + z--;
System.out.println("z="+z);//print 7..this is o.k.
z = z-- + z++ + z-- - z++;
System.out.println("z after ups and down ="+z);
/* print 14..here my brain is not working !!!I think it should be 12 ..like z=z(7)+z(7)+z(6)-z(8) */
}
}
Pls...explain 2 me why these r differentiating like this.
THANKS IN ADVANCE.
<marquee>Ratul Banerjee </marquee>
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ratul,
Sorry... i tried....but even my brain is not working. pls any experts help us.
Thanks
Avi.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
i think u creat an ambugity in u'r code. plz clear it and precise it.
thx
bybyby
love to all
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int z=4;
z = z-- + z--;
System.out.println("z="+z);//print 7..this is o.k.
z = z-- + z++ + z-- - z++;
z value is : 7;
step#1 :
(z--) : ASSIGN 7, and then decrement for the next process,
now z value become : 6
step#2 :
(z++) : ASSIGN 6, and then increment for the next process,
now z value become : 7
step#3 :
(z--) : ASSIGN 7, and then decrement for the next process,
now z value become : 6
step#4 :
(z++) : ASSIGN 6, and then increment,
now z value become : 7, but this time, don't care about increment or decrement on the last expression, it will not count.
so :
z = step#1 + step#2 + step#3 - step#4;
z = 7 + 6 + 7 - 6
z = 14

hope thhis help
stevie
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


int z=7;
z = z-- + z++ + z-- - z++;
System.out.println("z after ups and down ="+z);
/* print 14..here my brain is not working !!!I think it should be 12 ..like z=z(7)+z(7)+z(6)-z(8) */


Hi,
Steve has explained it all.. but can you guess the value of z in

just look at this thread.. http://www.javaranch.com/ubb/Forum24/HTML/008296.html
I think you will get an idea on ++z and z++ because these have different precedence.
Subbu

[This message has been edited by Subramaniam Venkatesan (edited March 26, 2001).]
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul Banji,
As per RHE book, page 33, the evaluation order in an expression is fixed that is from left to right.
Now let us consider ur first problem.
Your Quote:
int x,a=6,b=7;
x= a++ + b++;//line bla
System.out.println("x="+x); //prints 13 (!!!@@@####???),my prob. //states here
System.out.println("a="+a); //o.k. print 7
System.out.println("b="+b); // print 8...it is also ok.
My intrepretation:
The evaluation order is from left to right always.
Your line bla will be evaluated like this
step 1: x=6++ + 7++;
// Assign values from left to right once u are clear of the variable values.
step 2:
System.out.println("x= "+x);
// Evaluate x and this will yield 13 directly as what u have is a post increment operator.
step 3:
// Now the value of a is 7 that is after the post increment.
// Hence you will get the o/p 7
// | || ly the case with b and u will end up with 8 as the value.
Hope this makes ur first queation clear.

Let us analyse ur second problem.
Your quote
int y=3;
y = y++ + y++; // line bla bla
System.out.println("y="+y); // 7
y = y++ + y++ + y++;
System.out.println("y after another incre="+y); // 24
My intrepretation:
Here also the evaluation order is fixed. But the compiler is smart. It realises that the variable used here is only one and u r doing increment operations on that. So the evaluation is likely to be like this.
y=3++ +y++ // compiler will wait for the evaluation of the first part.
y=3 + 4++ // the post increment on y will yield this.
y= 7 // RHS evaluates to 7 and assigned to y
// assignment operator being lowest priority it happens // after the evaluation.
When u SOP(y) u will get 7.. OK..
Now next part of the problem.
y = y++ + y++ + y++;
Here u already know that y is 7 to start with.
So the evaluation goes like this.
y=7++ + y++ + y++; // wait for the first y evaluation.
y=7 + 8++ + y++; // assign post incremented value of y to next step // and wait that to be evaluated.
y=7 + 8 + 9++;// all assignments are done with.
// let,s evaluate the expression now..
y= 24;
So u get
System.out.println("y after another incre="+y); // 24
Hope this is correct & if so u r clear.
Comments pls..
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks all of u for ur support.
Now...I am understanding little bit.
Thanks all ..1ce again.
Regds.
<marquee> Ratul Banerjee </marquee>
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI TVS,
ur ans:
The evaluation order is from left to right always.
Your line bla will be evaluated like this
step 1: x=6++ + 7++;
// Assign values from left to right once u are clear of the variable values.
step 2:
System.out.println("x= "+x);
// Evaluate x and this will yield 13 directly as what u have is a post increment operator.
step 3:
// Now the value of a is 7 that is after the post increment.
// Hence you will get the o/p 7
// | | | ly the case with b and u will end up with 8 as the value
----------------------
my q:
1st of for assighment opt (=) has associativity from right to left.
As ur ans. states u r first assighning the value then incrementing ...but here states my q. that ++ opt. has higher precedence than assighment(=).Most of all..u have done that when u r explaning my 2nd problem.
What abt. that ?
So...pleze...I am in a big confution..pls..help me.
If I am wrong in understading something ..pls..correct me.
THANKS IN ADVANCE.
<marquee>Ratul Banerjee </marquee>
-------------
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Stevie,
ur ans:
z = step#1 + step#2 + step#3 - step#4;
z = 7 + 6 + 7 - 6
z = 14

As u explained that 1st of all we r doing step1,then step2......
But, same q. in my mind = opt. evaluate from right to left. !!!
I think I am not getting cleanly ...stevie.Ur explabation is good but pls. tell me abt. the evaluation order.
If I am wrong in understading something ..pls..correct me.
THANKS IN ADVANCE.
<marquee>Ratul Banerjee </marquee>

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic