In java virtual machine, methods are determined with their names, argument types, and and return type. This is why two or more methods with the same name can coexist - because their "full names" are different.
Java compiler selects most appropriate method to call so that minimal argument type conversion required (and disregards the return type). So it selects doSomething(String s)
for the first case anddoSomething(Object o)
for the second. At run time, methods selected at compile time are invoked, so the actual type of Object o
does not matter.
If you box simple types into Integer and Boolean, there is no other way to determine the actual type except for instanceof
. Consider boxing into your own types which can be extended.