I would suggest that Java should support closure first, and yield is an extension. Java tends to be a minimalistic language, and there are many ways to do this (although not so elegantly)
In the above example, it would be more appropriate to embed a Power method like this.
static void Main(String... args) {
As you can see, this is much shorter, although it may not be as clear how this is done.
BTW: 1 - 2 ^ 0 and degree 2.
EDIT: Java is not a rich language and does not encourage different styles of programming without giving them good support (and looking really ugly as a result). However, this can be done as follows.
public static Iterable<Integer> power(int base, int exponent) { List<Integer> ints = new ArrayList<Integer>(); for (int i = 1; exponent-- >= 0; i *= base) ints.add(i); return ints; } public static void main(String... args) { for (int i : power(2, 8)) System.out.print(i + " "); }
Print
1 2 4 8 16 32 64 128 256
Peter Lawrey
source share