I’m a fairly new to Java and to programming, so excuse me if this is a stupid question.
I want to create a method exceptionchecker that does this
public void exceptionchecker(Object check){
if(check.size() == 0 && some_other_stuff) throw new IllegalArgumentException();
}
The problem is that the object passed might be a list or a string. So depending on the case I need to either use .length or .size().
Is there any easy way to do this, or am I better off creating two seperate methods?
EDIT: If it’s relevant, the list would be of type String as well.
You could use
instanceofto do some checks, and it is reliable, but it doesn’t leave it self as open to extension, or clarity.The reason for this is
Using two methods is most appropriate.