Null interview questions - Java

String x = (String) null;
Why there is no exception in this statement?
String x = null;
System.out.println(x);
It prints null. But .toString() method should throw a null pointer exception.
Ans: You can cast null to any reference type without getting any exception.
println method does not throw null pointer because it first checks whether the object is null or not. If null then it simply prints the string "null". Otherwise it will call the toString method of that object.
Adding more details: Internally print methods call String.valueOf(object) method on the input object. And in valueOf method, this check helps to avoid null pointer excpeiton:
return (obj == null) ? "null" : obj.toString();
For rest of your confusion, calling any method on a null object should throw a null pointer exception, if not a special case.
Source: Stackoverflow

-------------------------------------------------------------------------------------------------------------------------------

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?
Ans:  Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).
null is a valid value for the types Objectchar[] and Integer. 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.
Source: Stackoverflow
--------------------------------------------------------------------------------------------------------------------------

Interesting question: 

In the code below the output is
String
and if I remove the method with the parameter of type String then the output is
Object
I know how overloading of methods acts when the parameter types don't match exactly but I can not understand how null can be treated as an Object and/or a String parameter.
What is the explanation for this?
class C {

    static void m1(Object x) {
        System.out.print("Object");
    }
    static void m1(String x) {
        System.out.print("String");
    }

    public static void main(String[] args) {
        m1(null);
    }
}
Source: http://geekexplains.blogspot.in/2009/06/choosing-most-specific-method-tricky.html

Comments

Popular posts from this blog

public vs protected vs default access modifiers - Java

Class, Reference and Object