Home>

I'm studying Java and sometimes I don't understand.
In a book called Self-study Java, there are the following samples.

package to.msn.wings.selflearn.chap07.method;
public class OverloadAnti {
        public void show (String value) {
            System.out.println ("String:" + value);
        }
        public void show (StringBuilder builder) {
            System.out.println ("StringBuilder:" + builder);
        }
        public void show (StringBuffer buf) {
            System.out.println ("StringBuffer:" + buf);
        }
        public void show (CharSequence cs) {
            System.out.println ("CharSequence:" + cs);
        }
}
package to.msn.wings.selflearn.chap07.method;
public class OverloadAntiClient {
    public static void main (String [] args) {
        var c = new OverloadAnti ();
        var list = new CharSequence [] {
                "Spring is Akebono",
                new String Builder ("Summer is night"),
                new String Buffer ("Autumn is dusk"),
        };
        for (var cs: list) {
            c.show (cs);
        }
    }
}


Result is,
Cher Sequence: Spring is Akebono
CherSequence: Summer is night
Cher Sequence: Dusk in Autumn
It is written that it will be.
As an explanation, since the array of CharSequence is used, the method that takes CharSequence as an argument is selected at compile time. Was written. (P290)

Then I tried it instead of the Char Sequence array.

package to.msn.wings.selflearn.chap07.method;
import java.util.List;
public class OverloadAntiClient {
    public static void main (String [] args) {
        var c = new OverloadAnti ();
        var list = List.of (
                "Spring is Akebono",
                new String Builder ("Summer is night"),
                new StringBuffer ("Autumn is dusk")
        );
        for (var cs: list) {
            c.show (cs);
        }
    }
}


The result did not change.
CharSequence: Spring is Akebono
CharSequence: Summer is night
CharSequence: Autumn is dusk
I think that the method is decided at compile time, but is it different that it is decided based on the array type for the time being? I think.
What are your criteria?