For example I have an Object “pretending” to be String:
Object o = new String("dsadsa");
if I want to use the String functions of that object, first I have to convert it to a string like this:
((String)o).indexOf("sad");
Which becomes really annoying and unreadable when there are so many brackets! Especially when that has to go in an IF statement or in a function!
if (((String)o).equals("dsadsa")) {}
What is the best way to avoid this kind of situation?
Cast the object in one line.
Use the casted object in another line.