aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is this java's Bug Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this java Watch "Is this java New topic
Author

Is this java's Bug

Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
// 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?
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
What do you mean by bug?
The answer is 4.
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.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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).]
Trupti Samel
Greenhorn

Joined: May 15, 2001
Posts: 24
Hello Mohammad;

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.
Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
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

Joined: Dec 16, 2000
Posts: 625
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.
Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
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

Joined: Nov 22, 2008
Posts: 18944
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).]
Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
Hi Shyam,
Thanks to join with me.
It is also my question that where is 5.
Aktar
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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.
Dilip Varma
Greenhorn

Joined: Feb 11, 2001
Posts: 3
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.
Your original example:
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).]
goddy007
Greenhorn

Joined: Sep 12, 2001
Posts: 1
Hi Mohammad,
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

Joined: Nov 22, 2008
Posts: 18944
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).]
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
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.


SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 15, 2001
Posts: 24
Hello Mohammad,
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.
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
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.
umar hathab
Ranch Hand

Joined: Aug 20, 2001
Posts: 52
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

Sun Certified Java Programmer for Java 2.0 (93%) <p>----Winners always make the learning a habit
Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
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
Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
Hi Jennifer
Thanks fo joinning.
Would you tell me more detail please.
Aktar
Mohammad Aktaruzzaman
Greenhorn

Joined: Sep 04, 2001
Posts: 16
Hello Trupti,
Thanks for URL.
Aktar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this java's Bug