A method is returning a Object
or Object[]
of type String
but if I am casting with String[]
, it is giving class cast exception when it contains single string. How can i resolve this?
Is there any way to check whether it contains String
or String[]
?
Sure, use the instanceof
operator:
if (x instanceof String) {
...
}
if (x instanceof String[]) {
...
}
etc. It's not ideal to have to do this, mind you... is there any way you could redesign your API to avoid this?
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