Skip to content

Instantly share code, notes, and snippets.

@OutOfBrain
Last active October 23, 2015 20:02
Show Gist options
  • Select an option

  • Save OutOfBrain/e6d2769255c80f72af7b to your computer and use it in GitHub Desktop.

Select an option

Save OutOfBrain/e6d2769255c80f72af7b to your computer and use it in GitHub Desktop.
Java testing
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
}
}
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();
}
}
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