Which of these is better casting or toString()

K

keshawra

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
 
P

Paul Hamaker

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.
 
K

keshawra

Paul said:
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.
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() .??
 
S

Stefan Schulz

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.
 
C

Chris Uppal

keshawra said:
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.

Definitely use the cast (or let generics do it for you). Using toString()
would be unforgivably perverse.

-- chris
 
A

Andrea Desole

keshawra said:
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
 
R

Roedy Green

// more add statements here..

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

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

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
 
J

James McGill

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.

Ensure homogenous collections:

ArrayList<String> lst = new ArrayList<String>();

Then you don't have to worry about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top