postfix and prefix
Why is the output 10 and not 9?. According to my understanding the count value is assign to b. Then the count value increment. Hence the output should be 9 instead of 10. However in this case the output is 10. Why?.
package com.nowhere.blogger.core.junit;
public class Bunnies {
static int count = 0;
Bunnies(){
while(count < 10) new Bunnies(++count); //prefix
}
Bunnies(int x) {super();}
public static void main(String[] args){
new Bunnies();
new Bunnies(count);
int b = count++; //postfix
System.out.println(b);
}
}
solomon_13000
Junior Poster in Training
88 posts since Jul 2009
Reputation Points: 24
Solved Threads: 0
you get value 10 since, as Thijk posted, the expression count < 10 will only return false when count = 10 (or count > 10).
the next increment is a postfix, so count will only be incremented after the value of b is set, so when you're printing your line:
count == 11
b == 10
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433