Home >Java >javaTutorial >What is Variable Handle in Java 9?

What is Variable Handle in Java 9?

PHPz
PHPzforward
2023-09-02 23:05:02936browse

Java 9中的Variable Handle是什么?

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

Grammar

<strong>public abstract class VarHandle extends Object</strong>

Example

is:

Example

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));
   }
}

Output

<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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete