Java method signature details and example code

  • 2020-05-12 02:38:48
  • OfStack

java method signature, I want to be the development of java friends also know, the importance of method signature, method overload is a good explanation, especially in the subsequent optimization, here recorded, you can see the friends can see,

The meaning of method signatures

For methods with different homonyms and different names, the meaning of method signature is not very big, but for overloaded methods, the meaning of method signature is huge. Since the method names are the same between overloaded methods, we must find another element and the method name from the other elements that make up the method, which can only indicate the signature of the method by 1. Of course, the method body is not considered. So is the parameter list and return values, but because to call a method, method of parameter according to the importance of the type list is far higher than the return value, so the method signature is formed by the method name + parameters list, that is to say, the method name and shape parameters can be determined according to the type of list 1 only one method, and the method of the return value 1 relationship all have no, this is the important basis judge overload, so, the following code is not allowed


public long aaaa(){ 

} 
public int aaaa(){ 

} 


The format of the method signature

First let's look at a few 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 1 some small rules:

The method signature provided by JVM is actually composed of the method name (the above example did not write the full class name for simplicity), the list of formal parameters, and the return value. The basic form is as follows:

Full class name. Method name (form parameter data type list) return value data type

Special character/letter meanings in Java method signatures

特殊字符 数据类型 特殊说明
V void 1般用于表示方法的返回值
Z boolean
B byte
C char
S short
I int
J long
F float
D double
[ 数组 以[开头,配合其他的特殊字符,表示对应数据类型的数组,几个[表示几维数组
L 全类名; 引用类型 以 L 开头 ; 结尾,中间是引用类型的全类名

[

The important thing to note is that when a method is overloaded, the return value of the method is meaningless. It is determined by the method name and the parameter list

]

Generate method signatures using javap

Class library classes


$ javap -s java.lang.String 
Compiled from "String.java" 
public final class java.lang.String extends java.lang.Object implements 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; 
   } 
 } 

The 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 means print signature information

-p means to print the signature information of all functions and members. By default, only the signature information of public is printed

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


Related articles: