Home > Java > javaTutorial > body text

What is Variable Handle in Java 9?

PHPz
Release: 2023-09-02 23:05:02
forward
894 people have browsed it

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>
Copy after login

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));
   }
}
Copy after login

Output

<strong>[0, 0, 0, 0, 0]
[0, 0, 5, 0, 0]
5</strong>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template