在Java 9中,MethodHandles类的重要性是什么?

2023年 8月 28日 45.0k 0

在Java 9中,MethodHandles类的重要性是什么?

public class MethodHandles extends Object

Example

的中文翻译为:

示例

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

public class MethodHandlesTest {
public void MethodHandle1() {
try {
MethodHandle methodHandleLength = MethodHandles.arrayLength(int[].class);
int[] array = new int[] {5, 10, 15, 20};
int arrayLength = (int) methodHandleLength.invoke(array);
System.out.println("Length of Array using Method Handle is: " + arrayLength);

MethodHandle methodHandleConstructor = MethodHandles.arrayConstructor(int[].class);
int[] newArray = (int[]) methodHandleConstructor.invoke(3);
System.out.println("Array Constructed using Method Handle of Size: " + newArray.length);

int x = (int) MethodHandles.zero(int.class).invoke();
System.out.println("Default Value of Primitive Integer using Method Handles is: " + x);
String y = (String) MethodHandles.zero(String.class).invoke();
System.out.println("Default Value of String using Method Handles is: " + y);
} catch(Throwable e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new MethodHandlesTest().MethodHandle1();
}
}

登录后复制

输出

Length of Array using Method Handle is: 4
Array Constructed using Method Handle of Size: 3
Default Value of Primitive Integer using Method Handles is: 0
Default Value of String using Method Handles is: null

登录后复制

以上就是在Java 9中,MethodHandles类的重要性是什么?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论