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?

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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"
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"

Comments

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."

Comments

-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.

2 Comments

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.
"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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.