I have a method that takes a parameter of type Object:
public static void test(Object foo)
{
System.out.println(foo);
}
I can pass foo: Strings, integers, booleans, etc...pretty much anything. I think this is possible because Object is the base class from which everything else inherits from, and therefore it accepts any data type as valid. I could understand this being the case with objects like Strings, and Arrays. But why is this the case with primitive types? Do integer, char, and boolean also inherit from Object?
No, primitive types do not inherit from Object since they're not classes. What happens when you pass a primitive type to this method from Java 5+ is called autoboxing, and the compiler will convert your primitive into one of the wrapper classes. For example, an int will be automatically converted into an Integer (using temporary variables behind the scenes) which will make the code compilable.
in this boxing then widening occurs ....for eg: if long then it First box in to Long then widen to Object Class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With