Method Overloading for null argument





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







118















I have added three methods with parameters:



public static  void doSomething(Object obj) {
System.out.println("Object called");
}

public static void doSomething(char obj) {
System.out.println("Array called");
}

public static void doSomething(Integer obj) {
System.out.println("Integer called");
}


When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char methods or Integer and Object methods?










share|improve this question




















  • 2





    Just change the Integer to int.

    – Mudassir
    Mar 8 '11 at 7:59






  • 2





    @Mudassir: and what exactly would that solve?

    – Joachim Sauer
    Mar 8 '11 at 8:02






  • 2





    @Joachim Sauer: If changed from Integer to int, then null isn't referred to primitive types in Java, so the compiler will not throw error.

    – Phani
    Mar 8 '11 at 8:05











  • Oh right, I missed that.

    – Joachim Sauer
    Mar 8 '11 at 8:06











  • @Joachim Sauer: It'll not throw the reference to doSomething is ambiguous error.

    – Mudassir
    Mar 8 '11 at 8:09


















118















I have added three methods with parameters:



public static  void doSomething(Object obj) {
System.out.println("Object called");
}

public static void doSomething(char obj) {
System.out.println("Array called");
}

public static void doSomething(Integer obj) {
System.out.println("Integer called");
}


When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char methods or Integer and Object methods?










share|improve this question




















  • 2





    Just change the Integer to int.

    – Mudassir
    Mar 8 '11 at 7:59






  • 2





    @Mudassir: and what exactly would that solve?

    – Joachim Sauer
    Mar 8 '11 at 8:02






  • 2





    @Joachim Sauer: If changed from Integer to int, then null isn't referred to primitive types in Java, so the compiler will not throw error.

    – Phani
    Mar 8 '11 at 8:05











  • Oh right, I missed that.

    – Joachim Sauer
    Mar 8 '11 at 8:06











  • @Joachim Sauer: It'll not throw the reference to doSomething is ambiguous error.

    – Mudassir
    Mar 8 '11 at 8:09














118












118








118


35






I have added three methods with parameters:



public static  void doSomething(Object obj) {
System.out.println("Object called");
}

public static void doSomething(char obj) {
System.out.println("Array called");
}

public static void doSomething(Integer obj) {
System.out.println("Integer called");
}


When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char methods or Integer and Object methods?










share|improve this question
















I have added three methods with parameters:



public static  void doSomething(Object obj) {
System.out.println("Object called");
}

public static void doSomething(char obj) {
System.out.println("Array called");
}

public static void doSomething(Integer obj) {
System.out.println("Integer called");
}


When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char methods or Integer and Object methods?







java oop null overloading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 13 '15 at 17:50









Jon Skeet

1098k69779968477




1098k69779968477










asked Mar 8 '11 at 7:55









PhaniPhani

3,35062941




3,35062941








  • 2





    Just change the Integer to int.

    – Mudassir
    Mar 8 '11 at 7:59






  • 2





    @Mudassir: and what exactly would that solve?

    – Joachim Sauer
    Mar 8 '11 at 8:02






  • 2





    @Joachim Sauer: If changed from Integer to int, then null isn't referred to primitive types in Java, so the compiler will not throw error.

    – Phani
    Mar 8 '11 at 8:05











  • Oh right, I missed that.

    – Joachim Sauer
    Mar 8 '11 at 8:06











  • @Joachim Sauer: It'll not throw the reference to doSomething is ambiguous error.

    – Mudassir
    Mar 8 '11 at 8:09














  • 2





    Just change the Integer to int.

    – Mudassir
    Mar 8 '11 at 7:59






  • 2





    @Mudassir: and what exactly would that solve?

    – Joachim Sauer
    Mar 8 '11 at 8:02






  • 2





    @Joachim Sauer: If changed from Integer to int, then null isn't referred to primitive types in Java, so the compiler will not throw error.

    – Phani
    Mar 8 '11 at 8:05











  • Oh right, I missed that.

    – Joachim Sauer
    Mar 8 '11 at 8:06











  • @Joachim Sauer: It'll not throw the reference to doSomething is ambiguous error.

    – Mudassir
    Mar 8 '11 at 8:09








2




2





Just change the Integer to int.

– Mudassir
Mar 8 '11 at 7:59





Just change the Integer to int.

– Mudassir
Mar 8 '11 at 7:59




2




2





@Mudassir: and what exactly would that solve?

– Joachim Sauer
Mar 8 '11 at 8:02





@Mudassir: and what exactly would that solve?

– Joachim Sauer
Mar 8 '11 at 8:02




2




2





@Joachim Sauer: If changed from Integer to int, then null isn't referred to primitive types in Java, so the compiler will not throw error.

– Phani
Mar 8 '11 at 8:05





@Joachim Sauer: If changed from Integer to int, then null isn't referred to primitive types in Java, so the compiler will not throw error.

– Phani
Mar 8 '11 at 8:05













Oh right, I missed that.

– Joachim Sauer
Mar 8 '11 at 8:06





Oh right, I missed that.

– Joachim Sauer
Mar 8 '11 at 8:06













@Joachim Sauer: It'll not throw the reference to doSomething is ambiguous error.

– Mudassir
Mar 8 '11 at 8:09





@Joachim Sauer: It'll not throw the reference to doSomething is ambiguous error.

– Mudassir
Mar 8 '11 at 8:09












7 Answers
7






active

oldest

votes


















196














Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).



Object, char and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.



Since Object is the super-type of char, the array version is more specific than the Object-version. So if only those two methods exist, the char version will be chosen.



When both the char and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.



Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).



On the contrary, the following invocation would be perfectly unambiguous:



char x = null;
doSomething(x);


Although you're still passing the value null, Java knows exactly which method to call, since it will take the type of the variable into account.






share|improve this answer





















  • 1





    This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

    – Christian Gosch
    Feb 13 '15 at 12:29






  • 2





    I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

    – Joachim Sauer
    Feb 13 '15 at 15:04











  • Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

    – Sameer
    Nov 10 '16 at 9:22











  • Thanks a lot, this saved my interview :)

    – roottraveller
    Sep 25 '17 at 10:34



















38














Each pair of these three methods is ambiguous by itself when called with a null argument. Because each parameter type is a reference type.



The following are the three ways to call one specific method of yours with null.



doSomething( (Object) null);
doSomething( (Integer) null);
doSomething( (char) null);


May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments. Such a design invites errors in the future.






share|improve this answer





















  • 1





    Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

    – kajacx
    Aug 11 '15 at 6:54






  • 1





    @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

    – jmg
    Sep 15 '15 at 5:51











  • what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

    – Sameer
    Nov 7 '16 at 15:13













  • Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

    – peterk
    Jul 18 '17 at 21:43



















4














null is a valid value for any of the three types; so the compiler cannot decide which function to use. Use something like doSomething((Object)null) or doSomething((Integer)null) instead.






share|improve this answer
























  • I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

    – Phani
    Mar 8 '11 at 8:01






  • 2





    Java arrays are also Objects.

    – Zds
    Mar 8 '11 at 8:53



















2














Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.






share|improve this answer































    1














    I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.



    Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.






    share|improve this answer

































      0














      there is an ambiguity because of doSomething(char obj) and doSomething(Integer obj) .



      char and Integer both are same superior for null thats why they are in ambiguous .






      share|improve this answer































        0














        class Sample{
        public static void main (String args) {
        Sample s = new Sample();
        s.printVal(null);

        }
        public static void printVal(Object i){
        System.out.println("obj called "+i);
        }

        public static void printVal(Integer i){
        System.out.println("Int called "+i);
        }
        }


        The output is Int called null and so ambiguity is with char and Integer






        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5229809%2fmethod-overloading-for-null-argument%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          196














          Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).



          Object, char and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.



          Since Object is the super-type of char, the array version is more specific than the Object-version. So if only those two methods exist, the char version will be chosen.



          When both the char and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.



          Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).



          On the contrary, the following invocation would be perfectly unambiguous:



          char x = null;
          doSomething(x);


          Although you're still passing the value null, Java knows exactly which method to call, since it will take the type of the variable into account.






          share|improve this answer





















          • 1





            This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

            – Christian Gosch
            Feb 13 '15 at 12:29






          • 2





            I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

            – Joachim Sauer
            Feb 13 '15 at 15:04











          • Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

            – Sameer
            Nov 10 '16 at 9:22











          • Thanks a lot, this saved my interview :)

            – roottraveller
            Sep 25 '17 at 10:34
















          196














          Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).



          Object, char and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.



          Since Object is the super-type of char, the array version is more specific than the Object-version. So if only those two methods exist, the char version will be chosen.



          When both the char and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.



          Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).



          On the contrary, the following invocation would be perfectly unambiguous:



          char x = null;
          doSomething(x);


          Although you're still passing the value null, Java knows exactly which method to call, since it will take the type of the variable into account.






          share|improve this answer





















          • 1





            This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

            – Christian Gosch
            Feb 13 '15 at 12:29






          • 2





            I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

            – Joachim Sauer
            Feb 13 '15 at 15:04











          • Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

            – Sameer
            Nov 10 '16 at 9:22











          • Thanks a lot, this saved my interview :)

            – roottraveller
            Sep 25 '17 at 10:34














          196












          196








          196







          Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).



          Object, char and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.



          Since Object is the super-type of char, the array version is more specific than the Object-version. So if only those two methods exist, the char version will be chosen.



          When both the char and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.



          Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).



          On the contrary, the following invocation would be perfectly unambiguous:



          char x = null;
          doSomething(x);


          Although you're still passing the value null, Java knows exactly which method to call, since it will take the type of the variable into account.






          share|improve this answer















          Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).



          Object, char and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.



          Since Object is the super-type of char, the array version is more specific than the Object-version. So if only those two methods exist, the char version will be chosen.



          When both the char and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.



          Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).



          On the contrary, the following invocation would be perfectly unambiguous:



          char x = null;
          doSomething(x);


          Although you're still passing the value null, Java knows exactly which method to call, since it will take the type of the variable into account.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 1 '17 at 3:40









          Jake Millington

          90111




          90111










          answered Mar 8 '11 at 8:06









          Joachim SauerJoachim Sauer

          239k50486562




          239k50486562








          • 1





            This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

            – Christian Gosch
            Feb 13 '15 at 12:29






          • 2





            I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

            – Joachim Sauer
            Feb 13 '15 at 15:04











          • Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

            – Sameer
            Nov 10 '16 at 9:22











          • Thanks a lot, this saved my interview :)

            – roottraveller
            Sep 25 '17 at 10:34














          • 1





            This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

            – Christian Gosch
            Feb 13 '15 at 12:29






          • 2





            I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

            – Joachim Sauer
            Feb 13 '15 at 15:04











          • Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

            – Sameer
            Nov 10 '16 at 9:22











          • Thanks a lot, this saved my interview :)

            – roottraveller
            Sep 25 '17 at 10:34








          1




          1





          This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

          – Christian Gosch
          Feb 13 '15 at 12:29





          This is stated for Java 7. Does that also apply to previous Java versions? I mean: If you have several method signatures with parameters only along a type hierarchy, than you are on the save side with null as actual value? And if you have built a "hierarchy for" like in the example here, then you are not?

          – Christian Gosch
          Feb 13 '15 at 12:29




          2




          2





          I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

          – Joachim Sauer
          Feb 13 '15 at 15:04





          I'm pretty sure those rules are the same for at least everything since Java 1.1 (except for the addition of generics, obviously).

          – Joachim Sauer
          Feb 13 '15 at 15:04













          Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

          – Sameer
          Nov 10 '16 at 9:22





          Does this mean that if compiler were to choose in between doSomething(String str) and doSomething(Object obj) during runtime with doSomething(null), doSomething(String str) will be called.

          – Sameer
          Nov 10 '16 at 9:22













          Thanks a lot, this saved my interview :)

          – roottraveller
          Sep 25 '17 at 10:34





          Thanks a lot, this saved my interview :)

          – roottraveller
          Sep 25 '17 at 10:34













          38














          Each pair of these three methods is ambiguous by itself when called with a null argument. Because each parameter type is a reference type.



          The following are the three ways to call one specific method of yours with null.



          doSomething( (Object) null);
          doSomething( (Integer) null);
          doSomething( (char) null);


          May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments. Such a design invites errors in the future.






          share|improve this answer





















          • 1





            Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

            – kajacx
            Aug 11 '15 at 6:54






          • 1





            @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

            – jmg
            Sep 15 '15 at 5:51











          • what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

            – Sameer
            Nov 7 '16 at 15:13













          • Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

            – peterk
            Jul 18 '17 at 21:43
















          38














          Each pair of these three methods is ambiguous by itself when called with a null argument. Because each parameter type is a reference type.



          The following are the three ways to call one specific method of yours with null.



          doSomething( (Object) null);
          doSomething( (Integer) null);
          doSomething( (char) null);


          May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments. Such a design invites errors in the future.






          share|improve this answer





















          • 1





            Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

            – kajacx
            Aug 11 '15 at 6:54






          • 1





            @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

            – jmg
            Sep 15 '15 at 5:51











          • what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

            – Sameer
            Nov 7 '16 at 15:13













          • Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

            – peterk
            Jul 18 '17 at 21:43














          38












          38








          38







          Each pair of these three methods is ambiguous by itself when called with a null argument. Because each parameter type is a reference type.



          The following are the three ways to call one specific method of yours with null.



          doSomething( (Object) null);
          doSomething( (Integer) null);
          doSomething( (char) null);


          May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments. Such a design invites errors in the future.






          share|improve this answer















          Each pair of these three methods is ambiguous by itself when called with a null argument. Because each parameter type is a reference type.



          The following are the three ways to call one specific method of yours with null.



          doSomething( (Object) null);
          doSomething( (Integer) null);
          doSomething( (char) null);


          May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments. Such a design invites errors in the future.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 '11 at 8:23

























          answered Mar 8 '11 at 7:58









          jmgjmg

          6,19611421




          6,19611421








          • 1





            Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

            – kajacx
            Aug 11 '15 at 6:54






          • 1





            @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

            – jmg
            Sep 15 '15 at 5:51











          • what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

            – Sameer
            Nov 7 '16 at 15:13













          • Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

            – peterk
            Jul 18 '17 at 21:43














          • 1





            Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

            – kajacx
            Aug 11 '15 at 6:54






          • 1





            @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

            – jmg
            Sep 15 '15 at 5:51











          • what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

            – Sameer
            Nov 7 '16 at 15:13













          • Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

            – peterk
            Jul 18 '17 at 21:43








          1




          1





          Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

          – kajacx
          Aug 11 '15 at 6:54





          Only Integer-char pair is ambiguous, because in other two cases, Java compiler can choose most specific choice, like @JoachimSauer described.

          – kajacx
          Aug 11 '15 at 6:54




          1




          1





          @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

          – jmg
          Sep 15 '15 at 5:51





          @kajacx: OPs original question was about calling these methods with null as parameter. Under that precondition, all three pairs are ambiguous. For the general case, I agree that only Integer - char pair is ambiguous.

          – jmg
          Sep 15 '15 at 5:51













          what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

          – Sameer
          Nov 7 '16 at 15:13







          what about doSomething(null) for public static void doSomething(String str) { System.out.println("String called"); } This will return string called.

          – Sameer
          Nov 7 '16 at 15:13















          Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

          – peterk
          Jul 18 '17 at 21:43





          Is it possible to use an anotation like "nullable" or "not nullable" and set up the declarations so only the one method you want to get a null with so that an explicit "null" (yet implicit null type) argument always unambiguously selects a specific overload ??

          – peterk
          Jul 18 '17 at 21:43











          4














          null is a valid value for any of the three types; so the compiler cannot decide which function to use. Use something like doSomething((Object)null) or doSomething((Integer)null) instead.






          share|improve this answer
























          • I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

            – Phani
            Mar 8 '11 at 8:01






          • 2





            Java arrays are also Objects.

            – Zds
            Mar 8 '11 at 8:53
















          4














          null is a valid value for any of the three types; so the compiler cannot decide which function to use. Use something like doSomething((Object)null) or doSomething((Integer)null) instead.






          share|improve this answer
























          • I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

            – Phani
            Mar 8 '11 at 8:01






          • 2





            Java arrays are also Objects.

            – Zds
            Mar 8 '11 at 8:53














          4












          4








          4







          null is a valid value for any of the three types; so the compiler cannot decide which function to use. Use something like doSomething((Object)null) or doSomething((Integer)null) instead.






          share|improve this answer













          null is a valid value for any of the three types; so the compiler cannot decide which function to use. Use something like doSomething((Object)null) or doSomething((Integer)null) instead.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 '11 at 7:58









          Lars NoschinskiLars Noschinski

          3,4521229




          3,4521229













          • I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

            – Phani
            Mar 8 '11 at 8:01






          • 2





            Java arrays are also Objects.

            – Zds
            Mar 8 '11 at 8:53



















          • I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

            – Phani
            Mar 8 '11 at 8:01






          • 2





            Java arrays are also Objects.

            – Zds
            Mar 8 '11 at 8:53

















          I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

          – Phani
          Mar 8 '11 at 8:01





          I removed the method with Integer parameter, it's invoking the function and returning output as "Array Called", so what's the contract between Array and Object?

          – Phani
          Mar 8 '11 at 8:01




          2




          2





          Java arrays are also Objects.

          – Zds
          Mar 8 '11 at 8:53





          Java arrays are also Objects.

          – Zds
          Mar 8 '11 at 8:53











          2














          Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.






          share|improve this answer




























            2














            Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.






            share|improve this answer


























              2












              2








              2







              Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.






              share|improve this answer













              Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 8 '11 at 8:05









              AnkitAnkit

              2,50211525




              2,50211525























                  1














                  I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.



                  Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.






                  share|improve this answer






























                    1














                    I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.



                    Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.






                    share|improve this answer




























                      1












                      1








                      1







                      I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.



                      Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.






                      share|improve this answer















                      I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.



                      Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 30 '17 at 6:30









                      ItamarG3

                      3,48262039




                      3,48262039










                      answered Jul 30 '14 at 15:57









                      Jafar AliJafar Ali

                      677830




                      677830























                          0














                          there is an ambiguity because of doSomething(char obj) and doSomething(Integer obj) .



                          char and Integer both are same superior for null thats why they are in ambiguous .






                          share|improve this answer




























                            0














                            there is an ambiguity because of doSomething(char obj) and doSomething(Integer obj) .



                            char and Integer both are same superior for null thats why they are in ambiguous .






                            share|improve this answer


























                              0












                              0








                              0







                              there is an ambiguity because of doSomething(char obj) and doSomething(Integer obj) .



                              char and Integer both are same superior for null thats why they are in ambiguous .






                              share|improve this answer













                              there is an ambiguity because of doSomething(char obj) and doSomething(Integer obj) .



                              char and Integer both are same superior for null thats why they are in ambiguous .







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 4 '17 at 0:41









                              chitrendra chaudharychitrendra chaudhary

                              1




                              1























                                  0














                                  class Sample{
                                  public static void main (String args) {
                                  Sample s = new Sample();
                                  s.printVal(null);

                                  }
                                  public static void printVal(Object i){
                                  System.out.println("obj called "+i);
                                  }

                                  public static void printVal(Integer i){
                                  System.out.println("Int called "+i);
                                  }
                                  }


                                  The output is Int called null and so ambiguity is with char and Integer






                                  share|improve this answer




























                                    0














                                    class Sample{
                                    public static void main (String args) {
                                    Sample s = new Sample();
                                    s.printVal(null);

                                    }
                                    public static void printVal(Object i){
                                    System.out.println("obj called "+i);
                                    }

                                    public static void printVal(Integer i){
                                    System.out.println("Int called "+i);
                                    }
                                    }


                                    The output is Int called null and so ambiguity is with char and Integer






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      class Sample{
                                      public static void main (String args) {
                                      Sample s = new Sample();
                                      s.printVal(null);

                                      }
                                      public static void printVal(Object i){
                                      System.out.println("obj called "+i);
                                      }

                                      public static void printVal(Integer i){
                                      System.out.println("Int called "+i);
                                      }
                                      }


                                      The output is Int called null and so ambiguity is with char and Integer






                                      share|improve this answer













                                      class Sample{
                                      public static void main (String args) {
                                      Sample s = new Sample();
                                      s.printVal(null);

                                      }
                                      public static void printVal(Object i){
                                      System.out.println("obj called "+i);
                                      }

                                      public static void printVal(Integer i){
                                      System.out.println("Int called "+i);
                                      }
                                      }


                                      The output is Int called null and so ambiguity is with char and Integer







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 4 '18 at 13:14









                                      nagarjuna chimakurthinagarjuna chimakurthi

                                      1




                                      1






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5229809%2fmethod-overloading-for-null-argument%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Bressuire

                                          Vorschmack

                                          Quarantine