Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Need help to understand Most Specific Method.

Ranji Sura
Ranch Hand
Posts: 84
Hi... I have just read this section in JLS... about "Choosing the Most Specific Method"

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

Could you please look at this..

One fixed-arity member method named m is more specific than another member method of the same name and arity if all of the following conditions hold:

The declared types of the parameters of the first member method are T1, ..., Tn.

The declared types of the parameters of the other method are U1, ..., Un.

If the second method is generic, then let R1 ... Rp (p ≥ 1) be its type parameters, let Bl be the declared bound of Rl (1 ≤ l ≤ p), let A1 ... Ap be the type arguments inferred (§15.12.2.7) for this invocation under the initial constraints Ti << Ui (1 ≤ i ≤ n), and let Si = Ui[R1=A1,...,Rp=Ap] (1 ≤ i ≤ n).

Otherwise, let Si = Ui (1 ≤ i ≤ n).

For all j from 1 to n, Tj <: Sj.

If the second method is a generic method as described above, then Al <: Bl[R1=A1,...,Rp=Ap] (1 ≤ l ≤ p).

For all j from 1 to n, Tj <: Sj.
How should I understand this...

First, what is the different between (Tj < Sj) and (Tj <: Sj) ?

Second,
how can I figure out one declared type of a parameter is greater than or small than other declared type of parameter... ?

Where it has been specified in JLS ?

Thank you...

Campbell Ritchie
Sheriff
Posts: 48954
60
Smaller than means subtype. You are going to have to read lots of the JLS to find out where.

Ranji Sura
Ranch Hand
Posts: 84
Campbell Ritchie wrote:Smaller than means subtype. You are going to have to read lots of the JLS to find out where.

Is this what do you mean by subtype... ?

http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10

I can't understand this.. I haven't learn something like this before..

Any way , can you tell me how should I want to go about JLS ? Like to know how you did that...

Winston Gutkowski
Bartender
Posts: 10417
63
Ranjith Suranga wrote:Hi... I have just read this section in JLS... about "Choosing the Most Specific Method"

I commend your diligence, but I wonder if this is the best use of your time.

The fact is that most of the time, method selection works pretty much the way you'd expect. Unfortunately, the JLS, being a specification, has to include all possible corner cases as well, which is why the language is so formalized.

By all means, read it if you want (or if you've been told to); but I think you might be better off learning how to avoid making the compiler and JVM go through all these complex decisions; of which the simplest remedies are:
2. Don't write methods with lots of parameters. 3 is my usual limit; if I find I need more, I'll check to see if there's another way of doing things.

Remember, if the compiler/JVM is having a hard time trying to choose which of your methods to invoke, then so will the people who use your class.

Winston

Ranji Sura
Ranch Hand
Posts: 84
After some time, I have found this...

4.10. Subtyping >> http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10 in JLS...

Then.. I started to read it...

You know what...

I have confused... just reading five lines of there

Can some one clear this out... ? Please....

The supertypes of a type are obtained by reflexive and transitive closure over the direct supertype relation, written S >1 T, which is defined by rules given later in this section.

what does this mean ?

Ranji Sura
Ranch Hand
Posts: 84
Dear Winston Gutkowski , I totally agree with you...

I will never ever never ever write this kind of class with such methods...

But what do...

I am studying for an exam... they give us crazy exam papers...
Without having good knowledge how can I pass... ?

Thank you..

Campbell Ritchie
Sheriff
Posts: 48954
60
Does that really say an int is a subtype of a long? Good grief, that is miles out. Ignore that book.

Ranji Sura
Ranch Hand
Posts: 84
Campbell Ritchie wrote:Does that really say an int is a subtype of a long? Good grief, that is miles out. Ignore that book.

OK... Ignored...

Ranji Sura
Ranch Hand
Posts: 84
Campbell Ritchie wrote:Does that really say an int is a subtype of a long? Good grief, that is miles out. Ignore that book.

Sir, I have got a question...

Check this...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10.1

The following rules define the direct supertype relation among the primitive types:

double >1 float

float >1 long

long >1 int

int >1 char

int >1 short

short >1 byte

According to this rule...
"T is a direct subtype of S, written T <1 S, if S >1 T. "

So..
long is a direct supertype of int, in other words int is a direct subtype of long...

am I right... ?

Campbell Ritchie
Sheriff
Posts: 48954
60
I never knew that. My apologies.

Winston Gutkowski
Bartender
Posts: 10417
63
Ranjith Suranga wrote:am I right... ?

Looks like it.

The fact is that you're doing more in-depth reading of the JLS than most of us have - at least, certainly more than most of us have recently.

And those "subtyping" rules follow the ones for numeric promotion, which, I have to be honest, was the reason I assumed why they worked for method calling.

Guess you learn something new every day...

Winston

Ranji Sura
Ranch Hand
Posts: 84
Ah.. Finally.. I have find out what is most specific method is...

Though it was a long journey, I have learnt lot of new things...
It is extremely thank you for Winston Gutkowski and Campbell Ritchie.

They are the ones who showed me the path...

According to the JLS, this is how it works when there are more than one member method is both accessible and applicable for method invocation,
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5
To understand those symbols in this paragraph, I have to study these also...
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10

then, it is able to understand what does it mean by this...

For all j from 1 to n, Tj <: Sj.

it means every T is a proper sub type of S.
it is something like this..

Car is proper sub type of Wheel.
Car is direct sub type of Vehicle.
Vehicle is direct sub type of Wheel.
So,
(Tj <: Sj) is similar to like (Car <: Wheels)
to understand this super sub types for primitive types... >>
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10.1

this how it works...
Case I
Here according to JLS, which method will be choosen ? There are 3 methods, eligible for method invocation here... They are
although there are three methods eligible, it should select one..
to select one, look for this rule
For all j from 1 to n, Tj <: Sj
float is direct sub type of double
long is direct sub type of float
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10.1
so, most suitable one is

because, (long <: double) and (long <1 float).

Case II

(1). new A().myMethod(x,y); == calling myMethod(byte,int) of A
But, there is no such method like myMethod(byte,int) in class A
(2). There are two methods eligible here for method invocation which are,

(3). If we now assign the JLS rules to this,

(long x) of first method and (short x) of second method == short is proper sub type of long, so (short <: long)
(long y) of first method and (int y) of second method == int is direct sub type of long, so (int <1 long)

we can figure out now which method is choosen, it should be (short,int) method which means this,

Case III

In this code, class A compiles OK. but when calling a method.. compiler also in trouble just like us for choosing a method because,
If we assign, JLS rules.. to this...
(int x) of the first method and (long x) of the second method == int is a direct sub type of long so, (int <1 long)
(long y) of the first method and (int y) of the second method == long is a direct super type of int, other words, int is a direct sub type of long so, (long >1 int) means (int <1 long)

so, the method which is selected by compiler is something like,
void myMethod(int x,int y){}
but there is no such method like this in these two methods.. so, compiler in also trouble in this case...

is for some one like me, not to struggle any more...

Once again thank you for these two...
(1) Winston Gutkowski
(2) Campbell Ritchie.

Campbell Ritchie
Sheriff
Posts: 48954
60
You’re welcome

And sorry about my mistake yesterday.