
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Importance of MethodHandles class in Java 9?
MethodHandles class introduced in Java 7 version. This class primarily added some static methods to better the functionality, and falls into several categories like Lookup methods that help to create method handles for methods and fields, Combinator methods that combine or transform pre-existing method handles into new ones, and factory methods to create method handles that emulate other common JVM operations or control flow patterns. MethodHandles class has enhanced in Java 9 to introduce many changes and added new static methods like arrayLength(), arrayConstructor(), zero(), and etc.
Syntax
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(); } }
Output
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
- Related Articles
- Importance of Thread.onSpinWait() method in Java 9?
- Importance of Collectors.flatMapping() method in Java 9?
- Importance of ofInstant() method in Java 9?
- Importance of Optional.or() method in Java 9?
- Importance of destroyForcibly() method in Java 9?
- Importance of StringReader class in Java?\n
- Importance of a Dictionary class in Java?
- Importance of a Locale class in Java?
- Importance of the Collectors.filtering() method in Java 9?
- Importance of transferTo() method of InputStream in Java 9?
- What is the importance of REPL in Java 9?
- What is the importance of SwingUtilities class in Java?
- What is the importance of Runtime class in Java?
- What is the importance of JViewport class in Java?
- What is the importance of JSeparator class in Java?

Advertisements