Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why null cast a parameter? [duplicate]

Tags:

java

null

When and why would somebody do the following:

doSomething( (MyClass) null ); 

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

like image 390
pek Avatar asked Nov 24 '08 23:11

pek


People also ask

What happens if you cast a null?

You can cast null to any reference type without getting any exception. The 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.

Can null be cast to Boolean?

Unfortunately the compiler is not capable of deducing that the statement boolean var1=(var=null); would always lead to the invalid assignment boolean var1=null .


2 Answers

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

like image 169
Johannes Schaub - litb Avatar answered Oct 22 '22 08:10

Johannes Schaub - litb


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);

like image 22
Eren Aygunes Avatar answered Oct 22 '22 09:10

Eren Aygunes