Methodhandle (method handle) series three: the difference between invoke and Invokeexact

Source: Internet
Author: User

Put the code up first, using the same code
/** * * * @author liuyefeng<[email protected]> * @date April 8, 2015 PM 10:41:13 * @CopyRight TopView INC * @versio N V1.0*/ Public classMethodhandletest { PublicMethodhandle gethandler () {Methodhandle MH=NULL; Methodtype MT= Methodtype.methodtype (String.class,int.class,int.class); Methodhandles.lookup LK=Methodhandles.lookup (); Try{MH= Lk.findvirtual (String.class,"substring", MT); } Catch(Throwable e) {e.printstacktrace (); }                 returnMH; }          Public Static voidMain (string[] args) throws Throwable {Methodhandle MH=Newmethodhandletest (). GetHandler (); String Str="Hello World"; Object RESULT1= Mh.invoke (str,1,3); Object result2= (String) mh.invokeexact (str,1,3);//Object result2 = mh.invokeexact (str, new Integer (1), 3);        /** * The above method executes the Times error, because the method type is String.class, Int.class, Int.class * and the returned type is object, and the declaration is a string that does not conform to *         Two parameter type is integer, and declaration of int does not match, then type adaptation does not conform, system error. */System. out. println ("result 1:"+RESULT1); System. out. println ("result 1:"+result2); }}

The difference between the invoke and the Invokeexact method, in terms of name, is clearly the latter being more accurate, or more demanding. The Invokeexact method requires strict type matching at invocation, and the return value type of the method is also considered, as commented out in the above code. If you putObject result2 = (String) mh.invokeExact(str, 13);中的(String)去掉类型转换的话,在调用的时候该方法会认为返回值是Object类型而不是String类型,然后系统报错。

The basic rule for type matching is to compare the return value type and the type of each parameter to match each other. Assuming that the source type is s and the target type is T, the basic rules are as follows: 1, can be done by the Java type conversion, generally from the subclass to the parent class, such as from a string to the object type, 2, can be completed by the basic type of conversion, you can only expand the scope of the type, such as an int tangent        Change to long, 3, can be done by the basic type of automatic boxing and unpacking mechanism, such as from int to Integer, 4, if S has a return value type, and T's return value type is void, then the return value of S is discarded. 5, if the return value of S is void, the return value of T is a reference type, the return value of T is null, 6, if the return value of S is void, and the return value of T is the base type, the return value of T is 0; 1th, 2, 3 well understood, 4th, 5, 6 The sense principle is the same, I use a new example Description
 Public classMethodhandletest { PublicMethodhandle gethandler () {Methodhandle MH=NULL; Methodtype MT= Methodtype.methodtype (void.class); Methodhandles.lookup LK=Methodhandles.lookup (); Try{MH= Lk.findvirtual (methodhandletest.class, "Print", MT); } Catch(Throwable e) {e.printstacktrace (); }                 returnMH; }          Public voidprint () {System.out.println ("Print"); }          Public Static voidMain (string[] args)throwsthrowable {methodhandletest mht=Newmethodhandletest (); Methodhandle MH=Mht.gethandler (); intRESULT1 = (int) Mh.invoke (MHT); Object result2=Mh.invoke (MHT); System.out.println ("Result 1:" +RESULT1); System.out.println ("Result 2:" +result2); }}

The output of the program is: Printprintresult 1:0result 2:null Reference: Java Programmer's Way of cultivation, deep understanding of Java7 core technology and best practices

Methodhandle (method handle) series three: the difference between invoke and Invokeexact

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.