为什么在Java中已经有`.`运算符的情况下还需要`::`运算符?

huangapple go评论145阅读模式
英文:

Why the need of :: operator when we already have . operator in java?

问题

由于我们可以使用.运算符访问类的静态字段和方法(使用类名),以及使用对象引用访问实例方法或变量,所以为什么需要::(方法引用运算符)呢?它也可以做同样的事情,只是更简洁。

英文:

Since, using the . operator we can access the static fields and methods of a class(using the class name) as well as instance methods or variables with the object reference, why do we need the :: (method reference operator) when it does the same thing but only less.

答案1

得分: 2

Java中的“.”操作符用于访问组成对象或类的字段和方法。另一方面,“::”操作符用于引用方法或构造函数,而不实际调用它。有时它被称为方法引用操作符。

“::”操作符在与lambda表达式和函数式编程相关时最常用。它使您可以像处理变量一样使用函数或构造函数,将它们视为一等公民实体。它提供了一种清晰的方式来引用可以放置在变量中或作为参数传递的构造函数或方法。

英文:

The "." operator in Java is used to access the fields and methods that make up an object or class. The "::" operator, on the other hand, is used to refer to a method or constructor without actually calling it. It is sometimes referred to as the method reference operator.

The "::" operator is most frequently used in relation to lambda expressions and functional programming. It enables you to use a function or constructor just like a variable, treating them as first-class entities. It offers a clear way to refer to constructors or methods that may be placed in variables or passed around as arguments.

huangapple
  • 本文由 发表于 2023年7月7日 03:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定