Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the output of this Core java Code?

Tags:

java

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?

like image 218
ramas jani Avatar asked Sep 26 '22 18:09

ramas jani


1 Answers

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.

like image 64
avik Avatar answered Sep 30 '22 07:09

avik