AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
programming-basics: Why it is printing same value even after four post increment?
class PostIncrement {
public static void main(String arg[]){
int a=10;
a=a++;
System.out.println(a);
a=a++;
a=a++;
a=a++;
System.out.println(a);
}
}
how the output is 10 after 3 increment operators?
programming x 169
basics x 171
Posted On :
2016-04-30 23:14:36.0
Saksham Kumar
734
339
32708
2
Answers
a=a++ is evaluated as:
int oldValue = a;
a = a + 1;
a = oldValue; // 3rd Step
if you remove assignment operator it´ll print the new value incremented every time whenever you used increment operator.
class PostIncrement{
public static void main(String arg[]){
int a=10;
a=a++; //with assignment operator
System.out.println(a);
a++; //without assignment operator
a++;
System.out.println(a);
}
}
Output:
-----------------------------
10
12
Posted On :
2016-05-01 13:25:24
Satisfied :
1 Yes
0 No
Rishi Kumar
523
1882
41268
Reply This Thread
4
"x = x++;" is almost certainly a mistake in any program.
http://stackoverflow.com/a/12033710/452708
Postfix operator means First use then increment.
Additionally, assigning value back to the variable "a" using "=" will never let the value increment using Postfix operator.
int a=10;
a=a++; // Variable a gets assigned Original Value 10 here before postfix can operate
System.out.println(a); //Prints 10
a=a++; // Variable a gets assigned Original Value 10 here before postfix can operate
a=a++; // Variable a gets assigned Original Value 10 here before postfix can operate
a=a++; // Variable a gets assigned Original Value 10 here before postfix can operate
System.out.println(a); //Prints 10
Posted On :
2016-05-19 16:04:46
Satisfied :
2 Yes
0 No
Abhijeet
0
3
0
Reply This Thread
3
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in