Java Planet

O Javie i jej otoczeniu

Postinkrementacja i warunki logiczne

Ciekawe i podchwytliwe pytanie z JavaBlackBelt dotyczące postinkrementacji (a++) oraz instrukcji if/else if/else

/**
 * Przykład postinkrementacji i warunków logicznych
 */
public class Increment {
	public static void main(String[] args) {
		int a = 0;
		if (a++ == 1) {
			a++;
			System.out.println("1: a="+a);
		} else if (a++ < 0) {
			a++;
			System.out.println("2: a="+a);
		} else {
			a += 2;
			System.out.println("3: a="+a);
		}
		System.out.println("4: a=" + a);
	}
	/**
	 * Rezultat
	 * 3: a=4
	 * 4: a = 4
	 */
}

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

COMMENTS

No Comments

There are no comments posted yet. Be the first one!

Leave a Replay