x = 2;
y = x++;
System.out.println(y)
If x is incremented after, would y print out 2 or will it print out 3 and why?
If this was prefixed, would it print out just the 3?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since the plus signs is after the variable it called a post-increment. If the was before the variable it would be called a pre-increment. With pre-increments y would be assigned x+1. where as with the post-increment y would be assigned x then x would increment by one and there after be 3 but y would be 2.
x = 2;
y = x++;
with that y would be 2 and x would be 3.
x = 2;
y = ++x;
with that x,y would both be 3.