98

I came across this question in a quiz,

public class MoneyCalc {

   public void method(Object o) {
      System.out.println("Object Verion");
   }

   public void method(String s) {
      System.out.println("String Version");
   }

   public static void main(String args[]) {
      MoneyCalc question = new MoneyCalc();
      question.method(null);
   }
}

The output of this program is "String Version". But I was not able to understand why passing a null to an overloaded method chose the string version. Is null a String variable pointing to nothing ?

However when the code is changed to,

public class MoneyCalc {

   public void method(StringBuffer sb) {
      System.out.println("StringBuffer Verion");
   }

   public void method(String s) {
      System.out.println("String Version");
   }

   public static void main(String args[]) {
      MoneyCalc question = new MoneyCalc();
      question.method(null);
   }
}

it gives a compile error saying "The method method(StringBuffer) is ambiguous for the type MoneyCalc"

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
zakSyed
  • 1,364
  • 1
  • 13
  • 30
  • You can assign a string to a null value so it is valid and the order for java and most programming languages is fit to the closest type and then to object. – JonH Oct 23 '12 at 14:44
  • http://stackoverflow.com/a/1572499/231290 – Lukasz Oct 23 '12 at 14:45
  • 6
    Apparently this was closed as duplicate by people who only read the title. The actual question here is about why a specific overload was chosen, not "what is null". – interjay Oct 24 '12 at 10:06

8 Answers8

102

Is null a String variable pointing to nothing ?

A null reference can be converted to an expression of any class type. So in the case of String, this is fine:

String x = null;

The String overload here is chosen because the Java compiler picks the most specific overload, as per section 15.12.2.5 of the JLS. In particular:

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

In your second case, both methods are still applicable, but neither String nor StringBuffer is more specific than the other, therefore neither method is more specific than the other, hence the compiler error.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    And how is the String overload more specific than the Object overload? – user1610015 Oct 23 '12 at 14:48
  • 12
    Because an `Object` can take any type and wrap it in an `Object`, whereas a `String` can only take a `String`. In this case, `String` is more specific of a type compared to the `Object` type. – JonH Oct 23 '12 at 14:48
  • @user1610015: See the JLS for details - I've included the "informal" version in the answer. – Jon Skeet Oct 23 '12 at 14:49
  • Thanks for the answer. However in my second scenario why didn't it choose the string method again as it didn't give any compile error earlier. – zakSyed Oct 23 '12 at 14:56
  • @zakSyed: What do you mean "as it didn't give any compile error earlier"? In the second case, it's ambiguous - neither `String` nor `StringBuffer` is more specific than the other. – Jon Skeet Oct 23 '12 at 14:58
  • 10
    @zakSyed If you were asked what is more specialized "String" or "Object", what would you say? Evidently "String", right? If you were asked: what is more specialized "String" or "StringBuffer"? There is no answer, they are both orthogonal specializations, how can you choose between them? Then you must be explicit regarding which one you want (i.e. by casting your null reference `question.method((String)null)`) – Edwin Dalorzo Oct 23 '12 at 15:01
  • @zakSyed - You may want to snatch a book on understanding data types. You have to understand why your second case is ambiguous. It's two different types namely, `String` and `StringBuffer` its the same if you would of overloaded this method with `String` and `Int`. – JonH Oct 23 '12 at 15:01
  • 1
    @JonSkeet: That makes sense. So basically it looks for a method according to the most specific rule and if it not able to decide which is more specific then it would throw a compile-time error. – zakSyed Oct 23 '12 at 15:03
  • 1
    @JonH: In the case of string and int, the code compiles and returns the output "String version". – zakSyed Oct 23 '12 at 15:06
  • 1
    @zakSyed - right, in your specific case, my point was `String` and `Int` are just two different types that specialize in just that their types. If you pass an int the compiler looks at the most specific method (given you are dealing with overloaded functions) and uses that one. – JonH Oct 23 '12 at 15:08
  • 3
    @JonH "null" is reference type, if one of the methods receives a primitive type as parameter (i.e. int) it will not even be considered by the compiler when choosing the right method to invoke for a reference of type null. It is confusing if by "Int" you meant java.lang.Integer or if you meant the primitive type int. – Edwin Dalorzo Oct 23 '12 at 15:12
  • @EdwinDalorzo - lost my mind you are right int was a very bad example. Use a reference type zak. – JonH Oct 23 '12 at 15:13
9

Additionally, the JLS 3.10.7 also declares that "null" is a literal value of the "null type". Therefore there exists a type called "null".

Later, the JLS 4.1 states that there exists a null type of which is impossible to declare variables, but you can use it through the null literal only. Later it says:

The null reference can always undergo a widening reference conversion to any reference type.

Why the compiler chooses to widen it to String might well be explained in Jon's answer.

Community
  • 1
  • 1
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
2

You can assign a string to a null value so it is valid and the order for java and most programming languages is fit to the closest type and then to object.

JonH
  • 32,732
  • 12
  • 87
  • 145
2

To answer the question in the title: null is neither a String nor an Object, but a reference to either can be assigned to null.

I'm actually surprised this code even compiles. I tried something similar previously and I got a compiler error saying that the call was ambiguous.

However, in this case, it seems like the compiler is choosing the method which is lowest on the food chain. It's assuming that you want the least generic version of the method in order to help you out.

I'll have to see if I can dig up the example where I got a compiler error in this (seemingly) exact same scenario, though...]

EDIT: I see. In the version I made, I had two overloaded methods accepting a String and an Integer. In this scenario, there is no "most specific" parameter (as in Object and String), so it can't choose between them, unlike in your code.

Very cool question!

asteri
  • 11,402
  • 13
  • 60
  • 84
  • null is neither but it can be assigned to both, that is the difference and it will compile why wouldn't it - it's absolutely valid code. – JonH Oct 23 '12 at 14:46
0

As String type is more specific than Object type. Let's say you add one more method that takes an Integer type.

public void method(Integer i) {
      System.out.println("Integer Version");
   }

Then you will get a compiler error saying that the call is ambiguous. As now we two equally specific methods with same precedence.

BSingh
  • 29
  • 3
0

Java compiler gives most derived class type to assign null.

Here is the example to understand it :

class A{

    public void methodA(){
        System.out.println("Hello methodA");
    }
}

class B extends A{
    public void methodB(){
        System.out.println("Hello methodB");
    }
}

class C{
    public void methodC(){
        System.out.println("Hello methodC");
    }
}

public class MyTest {

     public static void fun(B Obj){
         System.out.println("B Class.");
     }
     public static void fun(A Obj){
         System.out.println("A Class.");
     }

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

output: B Class.

on the other hand:

public class MyTest {

     public static void fun(C Obj){
         System.out.println("B Class.");
     }
     public static void fun(A Obj){
         System.out.println("A Class.");
     }

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

Result : The method fun(C) is ambiguous for the type MyTest

Hope it will help to understand this case better.

Ravi
  • 41
  • 1
  • 4
0

Source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.5
Concept: Most specific method
Explanation: If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. Try casting the null on to specific type and the method you wish will be automatically called.

M.P
  • 11
  • 3
  • In the first example, String extends Object, so "most specific" is the method taking String, but in the second example, String and StringBuffer **both** extend Object, so they are equally specific and therefore the compiler cannot make a choice – David Kerr Mar 12 '19 at 13:47
-1

I would say neither. NULL is a state not a value. Check out this link for more info on this (the article applies to SQL, but I think it helps with your question as well).

northpole
  • 10,244
  • 7
  • 35
  • 58