What are the difference between Descriptor and Signature

B

bearice

When looking at java class file. many thing has two attribute:
Descriptor and Signature. at most time they looks the same. So who can
tell what are the differences between them? Thank you.
 
L

Lew

When looking at java class file. many thing has two attribute:
Descriptor and Signature. at most time they looks the same. So who can
tell what are the differences between them? Thank you.

The term "descriptor" (lower-case "d") in a Java context usually refers to a
text file (often XML) that contains configuration information for a program or
a system, such as for a Web app. Literally it just means "something that
describes."

The "signature" (lower-case "s") of a method is the formal pattern of a
method, that is the argument types and their order, the return type and the
exception types.
 
T

Tom McGlynn

When looking at java class file. many thing has two attribute:
Descriptor and Signature. at most time they looks the same. So who can
tell what are the differences between them? Thank you.

While descriptor has a lot of possible meanings, if you are comparing
it with signatures, you may be thinking of descriptors in the context
of usage
in the JVM (e.g., http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html).

If so, then the signature of a method is the specification of the
types of the formal parameters of the method. I don't know that there
is any canonical representation of the signature of a method beyond
the method name and argument list as used in a Java program.
Generally the signature is something that the compiler worries about
but isn't used so much at run time. The descriptor includes the return
type of the method, which is not part of the signature, and has a very
specific string representation for all of this that is used within the
class files.


Regards,
Tom McGlynn
 
T

Tom McGlynn

The term "descriptor" (lower-case "d") in a Java context usually refers to a
text file (often XML) that contains configuration information for a program or
a system, such as for a Web app. Literally it just means "something that
describes."

The "signature" (lower-case "s") of a method is the formal pattern of a
method, that is the argument types and their order, the return type and the
exception types.

I believe the signature includes neither the return type nor the
exceptions thrown, just the method name and the types of the
parameters. This is discussed in the JLS (in 8.4.3 in the 3rd
edition). E.g.,
"Two methods have the same signature if they have the same name and
argument types."
See <http://java.sun.com/docs/books/jls/third_edition/html/
classes.html#8.4.2>

Regards,
Tom McGlynn
 
J

Joshua Cranmer

When looking at java class file. many thing has two attribute:
Descriptor and Signature. at most time they looks the same. So who can
tell what are the differences between them? Thank you.

A method descriptor is the name followed by the formal parameter types
followed by the return type. The signature is the same, but includes
generic types.

E.g.:
Set.get(K k):
Descriptor: put(Ljava/lang/Object;)Ljava/lang/Object;
Signature: put(TK;)TV;
 
L

Lew

Tom said:
I believe the signature includes neither the return type nor the
exceptions thrown, just the method name and the types of the
parameters. This is discussed in the JLS (in 8.4.3 in the 3rd
edition). E.g.,
"Two methods have the same signature if they have the same name and
argument types."
See <http://java.sun.com/docs/books/jls/third_edition/html/
classes.html#8.4.2>

You are correct. However, it is still useful to think of return type and
exception types as part of the signature. If you don't respect those, you can
get compiler errors. So informally I think of the signature as including
those factors.

For example, if you declare two methods in a class with the same signature,
it's a compiler error. If you declare two methods that differ only in
(non-covariant) return type, you get an error. If you add checked exceptions
to a method override, you get an error. The mental dodge of pretending that
exceptions and return types are part of the signature helps me keep that straight.

Still, it is pretending. You are right about the definition.
 
D

Daniele Futtorovic

You are correct. However, it is still useful to think of return type
and exception types as part of the signature. If you don't respect
those, you can get compiler errors. So informally I think of the
signature as including those factors.

For example, if you declare two methods in a class with the same
signature, it's a compiler error. If you declare two methods that
differ only in (non-covariant) return type, you get an error. If you
add checked exceptions to a method override, you get an error. The
mental dodge of pretending that exceptions and return types are part of
the signature helps me keep that straight.

The examples you give tend to prove the opposite of what you seem to
intend, rather. If the return type and the exceptions clause were part
of the signature, then two methods which differ only in return type
would have a _different_ signature, and thus it wouldn't be an error.

df.
 
L

Lew

Daniele said:
> The examples you give tend to prove the opposite of what you seem to
intend, rather. If the return type and the exceptions clause were part
of the signature, then two methods which differ only in return type
would have a _different_ signature, and thus it wouldn't be an error.

An excellent point. Actually, the JVM's definition of "descriptors" matches
what I was saying before. Thus am I corrected and have learned something new.
 
D

Daniele Futtorovic

Actually, the JVM's definition of "descriptors"
matches what I was saying before.

And so the loose ends are joined together and it seems like we have
answered the original question in a rather exhaustive manner. Not bad
for a runaway OP. :x)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top