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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Is this java's Bug

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
// Code Start
public class Test {
public static void main(string args[]){
int x = 1;
x = ++x + x++;
System.out.println(x);
}
}
//Tell me please what is the result and why?

Ranch Hand
Posts: 625
• Number of slices to send:
Optional 'thank-you' note:
What do you mean by bug?
The x= ++x + x++
= 3
But don't forget about that last operator which still will be applied which leads to the answer of 4.

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Sean,
If x = 1, can you please explain how the expression ++x + x++ would evaluate to 3?
To quell all misconceptions, run this code...

Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 11, 2001).]

Greenhorn
Posts: 24
• Number of slices to send:
Optional 'thank-you' note:

This example is regarding the difference in
pre & post increment operators.
----------------------------------------------------------------
I will try to explain this in steps:
public class Test
{

public static void main(String args[])
{

int x = 1; // here x is assigned the value of 1
x = ++x + x++;
x=(++x) + (x++);//evaluated in this way.
/*
1. ++x =2 assigns the it with the value 2 as it is preincrement operator.

2. x++ assigns the value of 3 to x(as now the value of x is 2) ,i.e x =3 ,BUT since it is post increment the actual value assigned is 2 and then the increment takes place.

3. so now the expression is evaluated as:
x= 2 (2) + 2(3) ;
4. x = 4. this is the value of x now after the addition.
*/

System.out.println(x);
}
}
------------------------------------------------------------
Hope I have not confused u anymore :-).
Do correct me if I am wrong .
HTH,
Trupti.

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hello Trupti;
Thanx to join with me.
I partially agree with u.But
if x is increment after assign 4 can u tell me how can I
get the increment value of x. Here is my question.And another question is that here I want to print of x why x does not return his last value with increment.

akter,Dhaka

Sean Casey
Ranch Hand
Posts: 625
• Number of slices to send:
Optional 'thank-you' note:
I'm not sure what you mean by your questions, but maybe I can explain things more clearly.
int x = 1;
x = ++x + x++;
Post increment operators occur after evaluation, while pre increment operators occur before evaluation. So the above can also be expressed as two statements:
x= ++x +x; x=3
x= ++x; now x=4
So let me run through the whole thing:
int x=1; // x=1 that's obvious
x= ++x +x++; //x=3 only the pre increment operator is applied
during evaluation.
System.out.println(x); //x=4 due to the post incrementing that occurred after the previous statement.
I know this may be a bit confusing but it is fundamental to java and you need to know it. Believe me, I found this on my exam. Post again if something isn't clear.

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Sean,
int x = 1;
x = ++x + x;
System.out.println(x)// Result x = 4;
you say, x= ++x +x; x=3 //??? Its worng compiled it for test
---------
Aktar

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Well, my understanding is...
If x = 1 and x = ++x + x++, the ++x portion would evaluate to 2 and the x++ portion uses this new value of 2 and therefore creates a latent value of 3. But since the post increment operator excludes the use of latent values in the same operation that generated them, prints out (2 + 2) 4. I guess the original post was wondering "Where is that 5?"..
I might be wrong in my understanding...Please do correct me if so.
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 12, 2001).]

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hi Shyam,
Thanks to join with me.
It is also my question that where is 5.
Aktar

Sean Casey
Ranch Hand
Posts: 625
• Number of slices to send:
Optional 'thank-you' note:
I was talking figuratively.
x= ++x +x
can be translated to:
x= ++(1) + 1 ;
x= 2 +1;
x=3
hence
x= ++x +x++
can be translated to:
x= ++(1) + (1)++;
x= 2 + (1)++;
x= 3
final value of x= 4 with the post increment operator.

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
Aktar,
The best thing to do when the the same variable is used on both sides of the expression is to split it up and use a different variable on the left hand side.
int x = 1;
x = ++x + x++;

can be split up as
int x=1, j;
j = ++x + x++;
x = j;

This will give you the correct result.
Regards,
Dilip

[This message has been edited by Dilip Varma (edited September 12, 2001).]

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
begin code
...
int x = 1;
x = ++x + x++;
System.out.println(x);
...
end code
From my understanding, as following:
x=1
++x get incremented to x = 1 + 1 = 2
Now, here is the key, x now is 2!!!
x = 2 + x ++ = 2 + 2 = 4
x did NOT get post-incremented!!!
thus, it will print 4. ^_~
Correct me if I am wrong.
Kind regards,
Goddy

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Guys,
I wrote this small code that should clear all questions...

Thanks, All
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 12, 2001).]

Ranch Hand
Posts: 697
• Number of slices to send:
Optional 'thank-you' note:
i agree with goddy007...
the post increment occurred after the assignment so it didn't have an effect on x.
here's a similar situation:
int x = 1;
x = x++;
x would still be 1.
it's different to what Shyamsundar did. he used another variable j in the assignment that's why x pushed through with the post increment.

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
I researched some of the earlier posts and found this...
Assume x = 1 and j = ++x + x++
Now, read this Post. Especially the explanation provided by Jim Yingst.
What I understood from that post is as follows...
++x produces 2...x++ is assigned 2 and it does a latent incrementation to 3 (which does not show up in the very same expression, But the value of x is 3 now). J = 4 (2+2) is very obvious now and must not come as a surprise.
The output of my code in my previous post reflects this very same idea.
I totally agree with Sean in that this is a very important part of the Certification Exam preparation and must be mastered.
Regards
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 13, 2001).]

Trupti Samel
Greenhorn
Posts: 24
• Number of slices to send:
Optional 'thank-you' note:
Sorry for delay in replying.I learnt this concept clearly after reading Mahaanna's Post .I have provided the link below .
http://www.javaranch.com/ubb/Forum24/HTML/000775.html
HTH,
Trupti.

Ranch Hand
Posts: 53
• Number of slices to send:
Optional 'thank-you' note:
Sorry,
I differ silightly. As my understading is;
x= ++x + x++;
first the LHS x is evaluated. After that the expression is evaluated from right to left. So the x++ is evaluated and the expression is now;
x= ++x + 1; // value of x is 2 now.
then before the value of x is substituted for ++x it has to be increamented first, so the value of x becomes 3 which in turn is put in the expression, so the expression is;
x= 3 + 1;// x is still three here.
x=4;
I hope it helps.
thanx.
Jennifer.

Ranch Hand
Posts: 52
• Number of slices to send:
Optional 'thank-you' note:
Hi,
x=1;
x=(++x) + (x++);
(++x)--->makes x value as 2;
Now the current x value is =2;
when evaluating (x++),the x value will be 2.
As x++ is the post incrementer, increment will not be used for the expression
So x=2+2=4;
Even if u remove post increment operator ,u would get the same answer.
If the expression had some more operation with x,the value of x would have been 3 by then.
Hope I may be right.
A.umar

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hi umar hathab:
Don't mind....
I have just one question to you.
You say.........>
-----------------------
As x++ is the post incrementer, increment will not be used for the expression
So x=2+2=4;
Even if u remove post increment operator ,u would get the same answer.
---------------------
If you agree that last x is post increase but not assign here.So,
How can I get this increment value of x of this situation.
Aktar

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hi Jennifer
Thanks fo joinning.
Would you tell me more detail please.
Aktar