aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Postfix Increment/Decrement Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Postfix Increment/Decrement Operators" Watch "Postfix Increment/Decrement Operators" New topic
Author

Postfix Increment/Decrement Operators

Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
I would be very grateful if someone could confirm something for me:

When using an postfix increment operator that the value to the variable increments immediatly after using the original value of the variable in the expression, and does not "wait" until the entire expression has been evaluated or statement completed.

For example:



leaves x=1 and y=1 (which it does - I've tried it).

Is this the case in ALL circumstances?

Thanks

Michal
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
Also, for those with Sierra & Bates, on the bottom of p166 / top of p167 it has



And then it says


The first expression compares x and y, and the result is false, because the increment on x doesn't happen until after the == test is made


So is it true that the increment does not happen immediatly after the value is returned, but after the == test is made? (although admittedly I don't think it affects the result in this case).
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
The postfix/prefix operators will always alter the value of the operand, whether they form part of an assignment (=) expression or a boolean expression (==)
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
So is it true that the increment does not happen immediatly after the value is returned, but after the == test is made?


Yes, the increment will only happen after the expression is evaluated
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
Originally posted by Fletcher Estes:
The postfix/prefix operators will always alter the value of the operand, whether they form part of an assignment (=) expression or a boolean expression (==)


Yes, but when does the increment occur? In the boolean express (==) is it after the boolean expression is evaluated or before?

Michal
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Try reading this and see if that helps clear up your questions. If not, let me know and I'll do my best.


SCJP Tipline, etc.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093


Effectively does this:

in that order.
This translates into:

Therefore both end up as 1.

Had you used ++x instead of x++ the outcome would have been quite different.

evaluates to

thus


42
Deepa Korecherla
Ranch Hand

Joined: Jul 14, 2004
Posts: 197
Can anyone solve this..my questions are in BOLD in the code commented beside the doubted lines..


public class plusTest {
public static void main(String[] args) {
int[] a = new int[]{1, 2, 3, 4};
int b = 1;
System.out.println(b+++b); //HERE THE O/P IS 3
System.out.println("b="+b);
a[b++] = b += 0; // WHAT IS A[B++] HERE?? A[4] OR A[3]??
for(int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
}
} // WHY IS THE RESULT COMIG AS 1234



public class pp {

public static void main(String[] args) {
int x=1;
int y=2;
System.out.println(x+++y); // IS IT X++ + Y OR X + ++Y ??
System.out.println("x="+x);
System.out.println("y="+y);

}
}

HELP ME
thanx,
Deepa


DKR<br />SCJP1.2,SCWCD1.4,SCBCD1.3,SCJA
Anand Jayaraman
Ranch Hand

Joined: Jan 28, 2004
Posts: 104
Hi!!

System.out.println(x+++y); // IS IT X++ + Y OR X + ++Y ??


I think, the compiler will parse (I am not sure if thats the right word) the expression and once it finds two +'s following the variable x, it accounts it for a valid postfix operator and performs an addition to y. Hence, it evaluates the expression as 'x++ + y'.

I am not entirely sure why it works like that.

I'd actually love a more comprehensive answer than this.


Cheers
Anand Jayaraman
Ranch Hand

Joined: Jan 28, 2004
Posts: 104
Another thing!!!

Deepa Korecherla
Ranch Hand

Joined: Jul 14, 2004
Posts: 197
Actually i know how the pp class works , and the expression is evaluated as x++ + y ...And the reason for that is the ++ operator has high priority than the + operator. But the plustest class ( code given) problem is not yet solved for me...the a[b++]...will it take as a[3] or a[4]...anyone pls reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Postfix Increment/Decrement Operators
 
Similar Threads
increment post and pre, when used in println
Marcus Green Question(can any body explain
What does evaluation mean? post, pre ++ --
i++i+++;
Q on post increment: JLS explanation