Java method signature explanation and instance code _java

Source: Internet
Author: User

Java method signature, I want to do Java development Friends also know that the importance of method signature is a better explanation of method overload , especially in the follow-up optimization, here record, there are see friends can also see,

Meaning of method signature

The method signature is not very significant for methods with different classes and different names of the same name, but for overloaded methods, the meaning of the method signature is enormous. Because the method names between overloaded methods are the same, we are bound to find another element in the composition of the method and the signature of the method name to uniquely mark the method, which of course is not considered. So that's the parameter list and the return value, however, because the method's shape parameter is much more important than the return value for the person calling the method, the method signature is made up of the method name + parameter list, that is, the method name and the form parameter list of types can uniquely determine a method. is not related to the return value of the method, which is an important basis for judging overloads, so the following code is not allowed

Public long AAAA () { 

} public 
int aaaa () { 

} 


Format of method signatures

First we look at a couple of methods and their method signatures:

public void Test1 () {}          test1 () v Public
void Test2 (String str)   test2 (ljava/lang/string;) v. Public
int Test3 () {}           test3 () I

From the above three examples, we can easily see some of the small rules:

The method signature provided by the JVM is actually made up of the method name (for example, in order to simply not write the full class name), the parameter list, the return value, and the basic form is:

Full class name. Method name (List of parameter types) return value data type

Special character/letter meaning in Java method signature

Special Characters Data Type Special Instructions
V void Typically used to represent the return value of a method
Z Boolean
B Byte
C Char
S Short
I Int
J Long
F Float
D Double
[ Array An array of corresponding data types, in conjunction with other special characters, several [representing several dimensional arrays].
L Full class name; The reference type starts with L; End, the middle is the full class name of the reference type

It is important to note that when the method overload, the method return value has no meaning, and is determined by the method name and the argument list

Using JAVAP to generate method signatures

Class Library Class

$ javap-s java.lang.String Compiled from ' String.java ' public final class java.lang.String extends Java.lang.Object im Plements java.io.serializable,java.lang.comparable,java.lang.charsequence{public static final Java.util.Comparator 
 Case_insensitive_order; 
Signature:ljava/util/comparator; 
 public java.lang.String (); 
 Signature: () V public java.lang.String (java.lang.String); 
 Signature: (ljava/lang/string;) V public java.lang.String (char[]); 
 Signature: ([C) V public java.lang.String (char[], int, int); 
 Signature: ([CII) V public java.lang.String (int[], int, int); 
 Signature: ([III) V public java.lang.String (byte[], int, int, int); 
 Signature: ([biii) V public java.lang.String (byte[], int); Signature: ([BI) V public java.lang.String (byte[], int, int, java.lang.String) throws 
 Java.io.UnsupportedEncodingException; 
 Signature: ([biiljava/lang/string;) V public java.lang.String (byte[], int, int, java.nio.charset.Charset); Signature: ([Biiljava/nio/charset/charset;) V PUblic java.lang.String (byte[], java.lang.String) throws java.io.UnsupportedEncodingException; 
 Signature: ([bljava/lang/string;) V public java.lang.String (byte[], java.nio.charset.Charset); 
 Signature: ([Bljava/nio/charset/charset;) V public java.lang.String (byte[], int, int); 
 Signature: ([BII) V ...

Custom class

Package Com.demo; 
 public class Sigtest {public 
   static final String name = null; 
   public int getName (int[] Data,long index) {return 
     0; 
   } 
 } 

Output

$ javac Sigtest.java 
$ javap-s-P com.demo.SigTest
Compiled from ' Sigtest.java ' public
class Com.demo.SigTest extends java.lang.object{public
static final java.lang.String name;
 signature:ljava/lang/string;
public Com.demo.SigTest ();
 Signature: () V public
int GetName (int[], long);
 Signature: ([IJ) I
static {};
 Signature: () V
}

-S indicates print signature information

-P indicates that all functions and members are printed with signature information, and only public signature information is printed by default

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.