This week's giveaway is in the Spring forum.We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of REST with Spring (video course) this week in the Spring forum!

# ++b + b++ and b++ + ++b

William Yan
Ranch Hand

Joined: Sep 26, 2006
Posts: 69
Hi everyone,
i cannot understand how ++b + b++ and b++ + ++b work, who'd drop me a light? more details are prefered. Thank you, ranchers.

Regards,
William

Java is better and better, yet bitter and bitter.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9999

3

Hi William,

Say for example int b = 1;
Now ++b + b++ is eveluated as (++b) + (b++) = (2) + (2) = 4
and b++ + ++b is evaluated as (b++) + (++b) = (1) + (3) = 4

Note that post fix and prefix operators have higher precedence than + operator and they associate from left to right.

Regards,
Jothi Shankar Kumar. S

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
Hi,

then for first one, if b=1..
++b+b++
(++b)+(b++)
1+1
2

and for second one, if b=1
b+++++b
b+++(++b)
1+++(2)
1+(++2)
1+3
4

Bijendra R.

Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9999

3

Hi Bijendra,

then for first one, if b=1..
++b+b++
(++b)+(b++)
1+1
2

It will print 4. Run that and check for yourself.

Regards,
Jothi Shankar Kumar. S
[ October 18, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
sorry i did wrong by mistake....

for first one, if b=1..
++b+b++
(++b)+(b++)
2+2
4

and for second one, if b=1
b+++++b
b+++(++b)
1+++(2)
1+(++2)
1+3
4

And one more thing...for assignment operator. Before assigning prefix is done and after assigning post fix is done.

Regards,

Bijendra R.
William Yan
Ranch Hand

Joined: Sep 26, 2006
Posts: 69
Thankyou..

regards,
William
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
then what happens:
bb+ + + ++bb
it prints 3 if b=1
anyone can explain?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9999

3

Hi James,

bb+ + + ++bb //here for the bb++, you are not incrementing it. Watch closely it is bb+ and not bb++. Hence the result 3.

Regards,
Jothi Shankar Kumar. S
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi James,

bb+ + + ++bb //here for the bb++, you are not incrementing it. Watch closely it is bb+ and not bb++. Hence the result 3.

Regards,
Jothi Shankar Kumar. S

I understand there is a space between first and second +. This spaces does matter.
but why does it result in 3? What's the meaning of three + signs here?
if a=1, i can do:
a+ + + + + + + +a
it still 2, same as a+a.
how come does compiler allows this?
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
Hi James and Jothi,

Line b++ + ++b // printing 4

Line b+ + + + +b // printing 2

Line b+ + + ++b // printing 3

closely watch the spaces.

and when I am writing bb+, I am getting the Javascript error. How do not know how you guys are comiling.

Regards,

Bijendra R.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Bijendra S. Rajput:
I am getting the Javascript error.

Javascript?

Getting someone to think and try something out is much more useful than just telling them the answer.
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
Hi Barry

Javascript?

sorry...sorry.....

Actually I have a junk of coding in javascript, so by mistake i wrote.

I mean, I got compile error.

Please explain this and focus some light on a + + +a how compiler is compiling this.

Thanks,

Regards,
Bijendra R.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9999

3

Hi Above,

Ok, now I'm also interested if anyone can shed some light on this.
a + + +a how compiler is compiling this

I was tracing this back to basic mathematics where we perform operations like this,

a- -b - -c + -d >> a - (-b) - (-c) + (-d) >> a+b+c-d

So does the compiler also work the same way? Anyone please explain on this.

Regards,
Jothi Shankar Kumar. S
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Bijendra S. Rajput:
Hi James and Jothi,

Line b++ + ++b // printing 4
Line b+ + + + +b // printing 2
Line b+ + + ++b // printing 3

but my result is different..

b = 1;
++b + b++; //printing 4
b+ + + + +b; //priting 6
b+ + + ++b; //printing 7

by which value you initialize b variable..?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9999

3

Hi Saif,

Originally posted by Bijendra S. Rajput:
Hi James and Jothi,

Line b++ + ++b // printing 4
Line b+ + + + +b // printing 2
Line b+ + + ++b // printing 3

The above code meant that each and every line is compiles seperatly with b=1.

Regards,
Jothi Shankar Kumar. S
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Thanks Jothi,

Yeah you are right
but have to mension if something like that like compiling separately.. isn't it.
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Bijendra S. Rajput:

when I am writing bb+

after read this "bb+" compiler start searching to find variable with name of bb if nothing, then it come up with the error : cannot resolve symbol.
[ October 18, 2006: Message edited by: Saif Uddin ]
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi,

b=1;
b++ + ++b //This evaluates to 4

However,
b + + ++b // This evaluates to 3

Can anyone please explain to me why the second expression evaluates to 3?
The only thing that is different is that I have removed the post increment operator on the first b, which shouldn't affect the value of the expression anyway since it is a post increment operator.

Thanks
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 392
Friends,
please let me know will we get these sort of questions in SCJP 5.0 eal exam.

I agree. Here's the link: http://aspose.com/file-tools

subject: ++b + b++ and b++ + ++b