Variable Handle is a variable or a reference to a set of variables, including static fields, non-static fields and heap data structures External array element in . This means that Variable Handle is similar to the existing Method Handle. You can use the java.lang.invoke.VarHandle class to represent it. We can use java.lang.invoke.MethodHandles.Lookupstatic factory method to create Variable Handle objects. It can also be used to access individual elements in arrays , as well as byte[] arrays. The Chinese translation of
<strong>public abstract class VarHandle extends Object</strong>
import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import java.util.Arrays; public class VarHandleTest { public static void main(String args[]) { <strong>VarHandle </strong>varHandle = <strong>MethodHandles.arrayElementVarHandl</strong>e(<strong>int[].class</strong>); int[] array = new int[5]; printArray(array); varHandle.<strong>set</strong>(array, 2, 5); printArray(array); System.out.println(varHandle.<strong>get</strong>(array, 2)); } private static void printArray(int[] array) { System.out.println(Arrays.toString(array)); } }
<strong>[0, 0, 0, 0, 0] [0, 0, 5, 0, 0] 5</strong>
The above is the detailed content of What is Variable Handle in Java 9?. For more information, please follow other related articles on the PHP Chinese website!