83

When and why would somebody do the following:

doSomething( (MyClass) null );

Have you ever done this? Could you please share your experience?

pek
  • 17,847
  • 28
  • 86
  • 99
  • Maybe just an inexperienced coder? Or someone who changed the code from doSomething((cast)something); and wanted to change it to null, and just didn't bother to remove the cast? Is it in more than one place or just one instance? – Ari Ronen Nov 24 '08 at 23:20
  • I can't remember where I've read it, but I saw this and learned that you can cast a null to an object. The problem is, I can't find a reasonable scenario! :P – pek Nov 24 '08 at 23:22
  • This is not a null cast. There is no such thing. It is a cast of a null. Totally the opposite. Your original title was better. – user207421 Sep 29 '19 at 01:04

2 Answers2

129

If doSomething is overloaded, you need to cast the null explicitly to MyClass so the right overload is chosen:

public void doSomething(MyClass c) {
    // ...
}

public void doSomething(MyOtherClass c) {
    // ...
}

A non-contrived situation where you need to cast is when you call a varargs function:

class Example {
    static void test(String code, String... s) {
        System.out.println("code: " + code);
        if(s == null) {
            System.out.println("array is null");
            return;
        }
        for(String str: s) {
            if(str != null) {
                System.out.println(str);
            } else {
                System.out.println("element is null");
            }
        }
        System.out.println("---");
    }

    public static void main(String... args) {
        /* the array will contain two elements */
        test("numbers", "one", "two");
        /* the array will contain zero elements */
        test("nothing");
        /* the array will be null in test */
        test("null-array", (String[])null); 
        /* first argument of the array is null */
        test("one-null-element", (String)null); 
        /* will produce a warning. passes a null array */
        test("warning", null);
    }
}

The last line will produce the following warning:

Example.java:26: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.String for a varargs call
cast to java.lang.String[] for a non-varargs call and to suppress this warning

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • What about NullPointerException? I did find this answer, but I didn't hind a real world scenario for this! Have you done this? Why? – pek Nov 24 '08 at 23:23
  • 2
    Often in unit tests you might pass null to a function – Jim Burger Nov 24 '08 at 23:25
  • 3
    This is not related to NullPointerException. You don't dereference the null. You just change the type of the reference to MyClass so that overload resultion can find the right method. aygunes provided a good example where it could be of use – Johannes Schaub - litb Nov 24 '08 at 23:30
34

Let's say you have these two functions, and assume that they accept null as a valid value for the second parameters.

void ShowMessage(String msg, Control parent);
void ShowMessage(String msg, MyDelegate callBack);

These two methods differ only by the type of their second parameters. If you want to use one of them with a null as the second parameter, you must cast the null to the type of second argument of the corresponding function, so that compiler can decide which function to call.

To call the first function: ShowMessage("Test", (Control) null);
For the second: ShowMessage("Test2", (MyDelegate) null);

wchargin
  • 15,589
  • 12
  • 71
  • 110
Eren Aygunes
  • 987
  • 9
  • 16