[英]Is there any difference between “invoking a static method with Classname.staticMethod” & “invoking with Classname::staticMethod” in java8
Is there any difference between “invoking a static method with Classname.staticMethod” and “invoking a static method with Classname::staticMethod” in java?在 Z93F725A07423FE1C8846F448B33D21 中“使用 Classname.staticMethod 调用 static 方法”和“使用 Classname::staticMethod 调用 static 方法”之间有什么区别吗?
Also is there a difference between “invoking a method with Object.method” and “invoking a method with Object::method” in java?在 Z93F725A07423FE1C8846F448B33D21
Classname::staticMethod or Object::method syntax is for Consumer operations are Consumer operation representations where compiler ensures the method is accepts the given type as input type While class.method or object.method are regular method invocations:) Classname::staticMethod 或 Object::method 语法用于消费者操作是消费者操作表示,其中编译器确保方法接受给定类型作为输入类型而 class.method 或 ZA8CFDE6331BD59EB2AC96F8911C4B6 是常规方法调用:)
The use of:: denotes a Method Reference and these can be used in any place where a @FunctionalInterface
is required.使用 of:: 表示方法引用,这些可以在任何需要
@FunctionalInterface
的地方使用。 Examples are Consumer
s, Function
s, Predicate
s, Supplier
s etc.... A lot of the Java 8 features.示例是
Consumer
s、 Function
s、 Predicate
s、 Supplier
s 等...... Java 8 的许多功能。 Eg if a method requires a String which is provided through a Supplier<String>
例如,如果一个方法需要一个通过
Supplier<String>
提供的字符串
public int methodUsingSupplier(Supplier<String> supplier) {
String s = supplier.get();
...
}
and there is a method并且有一个方法
public static String strProvider() {
return "my String";
}
in "MyClass"在“我的班级”中
then you can:那么你也能:
int value = methodUsingSupplier(MyClass::strProvider);
as a way of passing the strProvider
method as a 'reference' to the methodUsingSupplier
method which can then utilise it.作为将
strProvider
方法作为“引用”传递给methodUsingSupplier
方法的一种方式,该方法可以使用它。
As previously said in other answers the compiler ensures that the types and arguments match正如前面在其他答案中所说,编译器确保类型和 arguments 匹配
You can use both approaches to invoke a method, and the method will behave in the same way.您可以使用这两种方法来调用一个方法,并且该方法将以相同的方式运行。 However you need different syntaxes for both cases.
但是,这两种情况都需要不同的语法。 In the first approach you "directly" call the method to run its code.
在第一种方法中,您“直接”调用该方法来运行其代码。 In the second approach you use a reference to the method and apply the methods arguments (if any) to a method defined on the reference to actually run the code.
在第二种方法中,您使用对方法的引用并将方法 arguments(如果有)应用于在引用上定义的方法以实际运行代码。 This works both for static and instance methods.
这适用于 static 和实例方法。
public static void staticMethodExample() {
double d = 1.23;
double d2 = 4.56;
int x = Double.compare(d, d2); // -1
Comparator<Double> c = Double::compare;
int y = c.compare(d, d2); // -1
}
public static void instanceMethodExample() {
Double d = 1.23;
Double d2 = 4.56;
int x = d.compareTo(d2); // -1
DoubleToIntFunction c = d::compareTo;
int y = c.applyAsInt(d2); // -1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.