简体   繁体   中英

in java null value in String

I get a compilation error at prt(null) in the following:

 public class Abc {

    public static void prt(String b)
    {
        System.out.println("I m static Method with String");
    }
    public static void prt(Abc n)
    {
        System.out.println("I m static Method with Object");
    }


    public static void main(String[] args) {
        prt(null);

    }

}

but this class is fine:

public class Abc {

    public static void prt(String b)
    {
        System.out.println("I m static Method with String");
    }
    public static void prt(Object n)
    {
        System.out.println("I m static Method with Object");
    }


    public static void main(String[] args) {
        prt(null);

    }

}

Please help me understand null value in String, in any class object and in Object class object.

You should read carefully full compiler message. Its probably complains that the call prt(null) is ambiguous. In fact you should typecast it: prt((String)null) or prt((Abc)null) to let compiler know what parameter overloaded method to call.

Quote to explore:

It will call the method whose parameter type is a subclass of the parameter type of the other method. Beware! The parameter type is defined by the declared type of an object, not its instantiated type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM