Created
August 14, 2013 01:30
-
-
Save vaskoz/6227287 to your computer and use it in GitHub Desktop.
Examples of Lambda Expressions,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void m1(int x) { | |
int y = 1; | |
foo(() -> x+y); | |
// Legal: x and y are both effectively final. | |
} | |
void m2(int x) { | |
int y; | |
y = 1; | |
foo(() -> x+y); | |
// Legal: x and y are both effectively final. | |
} | |
void m3(int x) { | |
int y; | |
if (..) y = 1; | |
foo(() -> x+y); | |
// Illegal: y is effectively final, but not definitely assigned. | |
} | |
void m4(int x) { | |
int y; | |
if (..) y = 1; | |
else y = 2; | |
foo(() -> x+y); | |
// Legal: x and y are both effectively final. | |
} | |
void m5(int x) { | |
int y; | |
if (..) y = 1; | |
y = 2; | |
foo(() -> x+y); | |
// Illegal: y is not effectively final. | |
} | |
void m6(int x) { | |
foo(() -> x+1); | |
x++; | |
// Illegal: x is not effectively final. | |
} | |
void m7(int x) { | |
foo(() -> x=1); | |
// Illegal: x is not effectively final. | |
} | |
void m8() { | |
int y; | |
foo(() -> y=1); | |
// Illegal: y is not definitely assigned before the lambda (see 15.27.2) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Foo { | |
int someInt(); | |
} | |
public class LambdaExample { | |
public static void main(String[] args) { | |
Foo f = () -> 42; | |
System.out.println(f.someInt()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
() -> {} // No parameters; result is void | |
() -> 42 // No parameters, expression body | |
() -> null // No parameters, expression body | |
() -> { return 42; } // No parameters, block body with return | |
() -> { System.gc(); } // No parameters, void block body | |
() -> { | |
if (true) return 12; | |
else { | |
int result = 15; | |
for (int i = 1; i < 10; i++) | |
result *= i; | |
return result; | |
} | |
} // Complex block body with returns | |
(int x) -> x+1 // Single declared-type parameter | |
(int x) -> { return x+1; } // Single declared-type parameter | |
(x) -> x+1 // Single inferred-type parameter | |
x -> x+1 // Parens optional for single inferred-type case | |
(String s) -> s.length() // Single declared-type parameter | |
(Thread t) -> { t.start(); } // Single declared-type parameter | |
s -> s.length() // Single inferred-type parameter | |
t -> { t.start(); } // Single inferred-type parameter | |
(int x, int y) -> x+y // Multiple declared-type parameters | |
(x,y) -> x+y // Multiple inferred-type parameters | |
(final int x) -> x+1 // Modified declared-type parameter | |
(x, final y) -> x+y // Illegal: can't modify inferred-type parameters | |
(x, int y) -> x+y // Illegal: can't mix inferred and declared types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Examples of void- and value-compatible: | |
//These are value-compatible: | |
() -> { return "done"; } | |
() -> { if (cond) return 1; else return 0; } | |
//These are void-compatible: | |
() -> {} | |
() -> { System.out.println("done"); } | |
//These are both: | |
() -> { throw new RuntimeException(); } | |
() -> { while (true); } | |
//This is neither: | |
() -> { if (cond) return "done"; System.out.println("done"); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment