Java 8 中的实例方法引用类型有什么区别? [英] What's the difference between instance method reference types in Java 8?

查看:33
本文介绍了Java 8 中的实例方法引用类型有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

So Java 8 introduces method references and the docs describe the four types.

My question is what's the difference between the two instance types?

  1. Reference to an instance method of a particular object.
  2. Reference to an instance method of an arbitrary object of a particular type.

Both refer to references but what's significantly different? Is it that the type inference used to resolve them is different? Is it significant that (in their examples) one is a closure and the other is a lambda? Is it something to do with the number of arguments on a method?

解决方案

  1. myString::charAt would take an int and return a char, and might be used for any lambda that works that way. It translates, essentially, to index -> myString.charAt(index).

  2. String::length would take a String and return an int. It translates, essentially, to string -> string.length().

  3. String::charAt would translate to (string, index) -> string.charAt(index).

这篇关于Java 8 中的实例方法引用类型有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆