Tuesday 26 July 2022

Method overloading ambiguity with null values in Java

In this post, Let us see how java handles null values with method overloading, ambiguous situations, and how to handle ambiguous situations?

 

What is null?

null represents nothing or absence of the value. As per the java documentation, null reference can be assigned or cast to any reference type.

 

Let’s try to understand the Java way of handling null values with overloaded methods.

 

PrintUtil.java

package com.sample.app.util;

import java.util.List;

public class PrintUtil {

    public static void print(List<String> list) {
        System.out.println("Printing list content : " + list);
    }
    
    public static void print(Object obj) {
        System.out.println("Printing object : " + obj);
    }
}

In the above example, I defined print method, which can accept a list and object as arguments.

 

PrintUtil.print(null);

Above snippet call the print method with null argument, as both List and Object accepts null values, which method gets called in this case??? In this situation, Java choose the most specific method that accept null. Because null can be of the type List and a List is an Object, the compiler will choose the method that takes a  ‘List<String>’ as argument.

 

App.java

 

package com.sample.app;

import com.sample.app.util.PrintUtil;

public class App {

    public static void main(String[] args) {
        PrintUtil.print(null);
    }
}

Output

Printing list content : null

 

Let’s try with different use case which leads to ambiguity.

 


PrintUtil.java

 

package com.sample.app.util;

import java.util.List;

public class PrintUtil {

    public static void print(String str) {
        System.out.println("Printing string content : " + str);
    }

    public static void print(List<String> list) {
        System.out.println("Printing list content : " + list);
    }

    public static void print(Object obj) {
        System.out.println("Printing object : " + obj);
    }
}

I added one more overloaded version of print method that takes a String as argument. In this case, neither String nor List are more specific than each other, so compiler gets ambiguity to map null to a specific method.

 

App.java

package com.sample.app;

import com.sample.app.util.PrintUtil;

public class App {

    public static void main(String[] args) {
        PrintUtil.print(null);
    }
}

Try to compile App.java, you will get the error message.

App.java:6: error: reference to print is ambiguous
        PrintUtil.print(null);
                 ^
  both method print(String) in PrintUtil and method print(List<String>) in PrintUtil match
1 error

How to address this problem?

Explicitly cast the null to specific type.

 

PrintUtil.print((String)null);

PrintUtil.print((List<String>)null);

 

App.java

package com.sample.app;

import java.util.List;

import com.sample.app.util.PrintUtil;

public class App {

    public static void main(String[] args) {
        PrintUtil.print((String)null);
        PrintUtil.print((List<String>)null);
    }
}

Output

Printing string content : null
Printing list content : null

References

https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1



You may like

Interview Questions

Why to do explicit type casting from double to float conversion?

When is a class or interface is initialized or loaded in Java?

How to check two float values equality?

How to check whether a class is loaded or not in Java?

How to get a random element from Set?

Extract the elements of specific type in a collection

No comments:

Post a Comment