Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null is of reference type, is it a String reference or Object reference? [duplicate]

Tags:

java

object

oop

null is a reference and it is of null type only, i.e. null is not object type. But, when I run the following code snippet I was surprised when I pass null to method method(null); it calls method(String s) not method(Object o).

If null is itself a type defined by Java and not object type, then why does it call method(String s) not method(Object o)?

public class Test {
    public static void main(String[] args) {
        method(null);
    }
    public static void method(Object o) {
        System.out.println("Object impl");
    }
    public static void method(String s) {
        System.out.println("String impl");
    }
}

Edit

I have added one more method:

public static void method(Integer s) {
   System.out.println("String impl11");
}

Now the compiler gives the error The method method(Object) is ambiguous for the type Test.

Integer a = null;

If it is legal, then why do I see a compile-time exception?

like image 339
Priya Avatar asked Feb 12 '15 09:02

Priya


People also ask

Can an object reference be null?

So, a reference is what a variable of a reference type contains. These variables can point to “nothing”, though, and that's what we call a null reference: a reference that doesn't point to any object. When you try to call a method or another member on the said variable, you got the NullReferenceException.

Is String reference type?

String is a reference type, but it is immutable. It means once we assigned a value, it cannot be changed. If we change a string value, then the compiler creates a new string object in the memory and point a variable to the new memory location.

What is an object reference?

An object reference is information on how to find a particular object. The object is a chunk of main memory; a reference to the object is a way to get to that chunk of memory. The variable str does not actually contain the object, but contains information about where the object is.

What is null reference exception?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.


1 Answers

null is assignable to any reference type. When it comes to method overloading, the compiler always prefers the method with the more specific argument types. Therefore the method with the String argument is preferred over the method with the Object argument, since String is a sub-class of Object.

If, on the other hand, the choice was between methods with unrelated argument types - for example method(String s) and method (Integer i) - the code wouldn't pass compilation, since none of the options would have precedence over the other.

like image 146
Eran Avatar answered Oct 14 '22 07:10

Eran