Saved Bookmarks
| 1. |
What is the result of the below code and Why? |
|
Answer» public class TestClass { public static void main(String[] args) { someMethod(null); } public static void someMethod(Object o) { System.out.println("Object method Invoked"); } public static void someMethod(String s) { System.out.println("String method Invoked"); } } The output of this code is “String method Invoked”. We know that null is a value that can be assigned to any kind of object reference type in Java. It is not an object in Java. Secondly, the Java compiler chooses the method with the most specific parameters in method overloading. this means that since the String class is more specific, the method with String input parameter is called. |
|