Java method signature. Friends who want to do java development also know that the importance of method signature is a better explanation of method overloading, especially in subsequent optimization. Here is a record. Friends who have seen it can also read it.
The meaning of method signature
For methods with different names and different names of the same name, method signature is not very meaningful, but for overloaded methods, method signature is very meaningful. Since the method names between overloaded methods are the same, we must find another element from the other elements that constitute the method and the method name composition that can uniquely indicate the method, and the method body will not be considered. Then it is the formal parameter list and the return value. However, for those who call the method, the importance of the method's formal parameter data type list is much higher than the return value, so the method signature is composed of the method name + the formal parameter list. That is to say, the method name and the formal parameter data type list can uniquely determine a method, and have no relationship with the method's return value. This is an important basis for judging overloading, so the following code is not allowed.
public long aaaa(){ } public int aaaa(){ }Method signature format
First, let’s look at several methods and their method signatures:
public void test1(){} test1()Vpublic void test2(String str) test2(Ljava/lang/String;)Vpublic int test3(){} test3()I From the above three examples, we can simply see some small rules:
The method signature provided by JVM is actually composed of three parts: method name (the above example did not write out the full class name for simplicity), formal parameter list, and return value. The basic form is:
Full class name. Method name (formal parameter data type list) return value data type
Special characters/letter meanings in Java method signature
| Special characters | Data Type | Special Instructions |
|---|---|---|
| V | void | Generally used to represent the return value of the method |
| Z | boolean | |
| B | byte | |
| C | char | |
| S | Short | |
| I | int | |
| J | long | |
| F | float | |
| D | double | |
| [ | Array | Start with [, and combined with other special characters, represent an array of corresponding data types, and several [represents a few-dimensional arrays |
| L | Full category name; | The reference type begins with L; ends with the entire class name of the reference type in the middle |
It must be noted that when the method is overloaded, the method return value has no meaning, it is determined by the method name and parameter list.
Generate method signature using Javap
Class library class
$ 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); Signature: ([BIII)V public java.lang.String(byte[], 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 Classes
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.SigTestCompiled 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: ()Vpublic int getName(int[], long); Signature: ([IJ)Istatic {}; Signature: ()V}-s means printing signature information
-p means printing the signature information of all functions and members. By default, only public signature information is printed
Thank you for reading, I hope it can help you. Thank you for your support for this site!