5
public void getData(Object o[]) {
    System.out.println("In Side Array");
}

public void getData(Object o) {
    System.out.println("In Side Object");
}

public static void main(String[] args) {
    new JavaEx().getData(null);
}

Here it's printing Array block why, Why it's not printing Object block?

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
ramas jani
  • 59
  • 1
  • 6
    possible duplicate of [Method Overloading for NULL parameter](http://stackoverflow.com/questions/5229809/method-overloading-for-null-parameter) – Dragan Bozanovic Sep 11 '15 at 09:08

4 Answers4

4

Both getData methods can handle a null argument. In this situation Java tries to choose the method that handles the more specific type.

Now Object is by definition the superclass of all Java classes, so in this situation Object[] (which is also an Object) is the more specific type and getData(Object o[]) is the more specific method. This is why Java chooses this method.

avik
  • 2,708
  • 17
  • 20
  • Why is Object[] more specific than Object? If you take any other class instead of object the compiler will throw an error. "The method getData(Object) is ambiguous for the type NullTypeMatching" – HopefullyHelpful Sep 11 '15 at 09:24
1

According JLS(Java Language Specification) for determine method signature time

  1. The first phase performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

  2. The second phase performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.

  3. The third phase allows overloading to be combined with variable arity methods, boxing, and unboxing.

In Your example:

when you call method getData() by passing argument null the compiler go to first phase and found method signature without performing any boxing.

So that output is "In Side Array"

Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33
1

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

"If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error."

Joachim
  • 11
  • 1
  • 2
-2

Maybe this will make things a bit clearer:

public void getData(Object o[]) {
    System.out.println("In Side Array");
}

public void getData(Object o) {
    System.out.println("In Side Object");
}
public static void main(String[] args) {
    Object[] array = null;

    new JavaEx().getData(array);
}

This implementation will also print "In Side Array", since an uninitialized array can be null. So it becomes obvious, that the array method is called.

user1438038
  • 5,821
  • 6
  • 60
  • 94
  • 2
    I'm not sure how valid my next statement is, but it's a rule I always use; The compiler will always use the most specified correct method. So in this case, an Object is less specified than a Object[] (because an object array is also an object). So that's why it prints the array over the object. – Robbenu Sep 11 '15 at 08:52
  • "Object is less specified than a Object[] (because an object array is also an object)" I think it's wrong, an int is also an object, but int is less specified than an object. – yelliver Sep 11 '15 at 08:55