Last active
October 23, 2015 20:02
-
-
Save OutOfBrain/e6d2769255c80f72af7b to your computer and use it in GitHub Desktop.
Java testing
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
| import java.util.function.IntFunction; | |
| public class ConstructorReference { | |
| public static void main(String...args) { | |
| IntFunction<Integer> intMakerSingle = Integer::new; | |
| int intSingle = intMakerSingle.apply(40); // int value | |
| IntFunction<int[]> intMakerArray = int[]::new; | |
| int[] intArray = intMakerArray.apply(100); // array size | |
| } | |
| } |
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
| public class OuterClass { | |
| int x = 1; | |
| Sub sub; | |
| public OuterClass() { | |
| sub = this.new Sub(); | |
| } | |
| class Sub { | |
| int x = 2; | |
| void f() { | |
| System.out.println("sub f" + OuterClass.this.sub.x); | |
| } | |
| } | |
| public static void main(String...args) { | |
| OuterClass outerClass = new OuterClass(); | |
| Sub sub = outerClass.new Sub(); | |
| sub.f(); | |
| } | |
| } |
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 MyFunctionalInterface { | |
| void call(); | |
| } | |
| // not implementing MyFunctionalInterface | |
| class SampleClass { | |
| static void printer() { | |
| System.out.println("Printer"); | |
| } | |
| } | |
| public class TestFunctionalInterface { | |
| public static void main(String...args) { | |
| user(SampleClass::printer); // still callable on matching interface | |
| } | |
| static void user(MyFunctionalInterface f) { | |
| f.call(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment