Java: why null might be used as an input parameter (aka argument)

The tutor looks into passing null as an argument.

Sometimes, reading Java code, you encounter null being passed as an argument aka parameter:

method0(null);

Apparently, when a method expects an object parameter, but doesn’t (necessarily) use it, you can pass it null to satisfy its need for a parameter. A very simple example might be as follows:

method0(Object obj0){
System.out.println(“Hello! I’m here!”);
}

Above, the method method0 requires a parameter of type Object. Although method0 doesn’t use that parameter, the compiler will protest if method0 is called without it. Calling method0 with null as the parameter will apparently satisfy the compiler, which is why you might see

method0(null);

in Java code examples.

I’ll talk more about this in future posts.

Source:

coderanch.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply