Re: Which of these is better casting or toString()



keshawra wrote:
List lst = new ArrayList();
lst.add("a");
// more add statements here..

((String) lst.get(j)).toUpperCase()

lst.get(j).toString().toUpperCase();

casting or toString() ?? which one is better to use.

kiri

I would cast, because it makes it clear that you are expecting a string. With a cast it's easier to detect an error, with toString() it isn't.
As to performance I'm not sure there is a big difference. I wouldn't be surprised if it depended on the implementation. It shouldn't have a great impact anyway: probably the execution of toUpperCase() takes much longer
.



Relevant Pages


Loading