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
Paul Hamaker - 24 Feb 2006 09:04 GMT
An ArrayList as used here can contain any type element, including ones
that cannot be cast to String, so that's unsafe . toString can be
called on any type, however, but this would indicate a design flaw,
since you probably expect your AL to contain strings, right ?
So, to make your code robust, you could check the element with
instanceof and then cast it or use 1.5 generics instead.
keshawra - 24 Feb 2006 09:10 GMT
> An ArrayList as used here can contain any type element, including ones
> that cannot be cast to String, so that's unsafe . toString can be
[quoted text clipped - 4 lines]
>
>
thanks for that..
Assuming that the AL contains only strings are there any performance
gain using one over other..
I mean is casting faster that tostring() .??
Stefan Schulz - 24 Feb 2006 11:27 GMT
You really should not worry about this, the cost of either is
absolutely miniscule. However, you really should take a look at
generics, which will take care of all the casting for you.
Also, be aware that toString() can be arbitrarily expensive.
Chris Uppal - 24 Feb 2006 12:40 GMT
> List lst = new ArrayList();
> lst.add("a");
[quoted text clipped - 5 lines]
>
> casting or toString() ?? which one is better to use.
Definitely use the cast (or let generics do it for you). Using toString()
would be unforgivably perverse.
-- chris
Andrea Desole - 24 Feb 2006 12:52 GMT
> List lst = new ArrayList();
> lst.add("a");
[quoted text clipped - 7 lines]
>
> 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
Roedy Green - 24 Feb 2006 13:34 GMT
>// more add statements here..
>
[quoted text clipped - 3 lines]
>
>casting or toString() ?? which one is better to use.
toString and (String) do quite different things.
You would rarely use (String) today. All it can do is reassure Java
that some Object is indeed ALREADY a String. It cannot change anything
else into a String. It is not like (int) that can cause a
transformation. Unfortunately, the (cast) syntax has two completely
different meanings, conversion and reassurance. Today you use
generics instead of most reassurance casting. You no longer need
(String).
toString on the other hand in a worker method that does its best to
display the Object in some reasonable way to make sense to humans.
String.toString just returns the String. All it does is waste time.
So the answer to you question is NEITHER. Use generics. If they
frighten you, use (String). You are reassuring, no converting.
See http://mindprod.com/jgloss/generics.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
James McGill - 24 Feb 2006 21:49 GMT
> List lst = new ArrayList();
> lst.add("a");
[quoted text clipped - 5 lines]
>
> casting or toString() ?? which one is better to use.
Ensure homogenous collections:
ArrayList<String> lst = new ArrayList<String>();
Then you don't have to worry about it.