Monday, 25 April 2016

Scenarios when calling method by passing null value


Calling method by passing null value

Scenario 1

public class Test1 {
  public static void main(String[] args) {
       show(null); // The method show(String) is ambiguous for the type Test1
  }
  private static void show(String str) {
       System.out.println("String str");
  }
      
  private static void show(int[] str) {
       System.out.println("int[] str");
  }
}


Output : Compilation error
The method show(String) is ambiguous for the type Test1



Scenario 2

public class Test2 {
  public static void main(String[] args) {
       show(null);
  }
  private static void show(String str) {
       System.out.println("String str");
  }
      
  private static void show(Object str) {
       System.out.println("int[] str");
  }
}

Output : String str

No comments:

Post a Comment

Note: only a member of this blog may post a comment.