This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Performance and the fly likes toString() and typecast to String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "toString() and typecast to String" Watch "toString() and typecast to String" New topic
Author

toString() and typecast to String

Roopa Bharani
Greenhorn

Joined: Mar 18, 2008
Posts: 1
Hi,

I have a problem in which I can either use the toString() method to get the String version of an object or typecast it to String. But i want to know which is the more efficient way of doing this??
In terms of memory and cpu utilization which approach would be better?

Thanks,
Roopa.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Roopa Bharani,
Welcome to JavaRanch.

The cast version : (String) myObject is faster than the myObject.toString()

Actually, as *I* know that myObject.toString() is transformed to
((String) myObject.toString()). So, this in turn requires an extra call to toString() thus, it's a bit slower.

Well, in a normal application you would hardly find the difference as the difference actually accounts when there are very huge say millions of such operrations.

Another point to consider when you use either ways is that if the object you are working on is null then toString() will throw exception. Moreover, you have to be very careful that the object can actually be reffred as String.

If you are using collections, Maps etc. then it is always better to use Generics(JDK5.0).

Hope that helps. Good Luck..!!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Er, what?! Casting and toString() are not alternatives to one another.

Casting is something you do to an object reference. If you have a reference of type java.lang.Object, but the object referred-to is really a String, you can cast it to String. However, if the object referred-to is not a String, this will not make it into a String.

The toString() method is entirely different. For a start, a method is something that an object has, not an object reference has. All objects have a toString() method, which will return a String that somehow represents the object. The version of toString() in java.lang.Object is only suitable for debugging purposes, but some objects override toString() to give something more readable.

If you're still confused about this sort of thing, then it's far too early in your Java learning curve to be worrying about what's faster than what.
[ March 18, 2008: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
As previous posters have mentioned this is more of a design question than a performance question. And rarely if ever will this design choice impact the overall performance of your application.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: toString() and typecast to String
 
Similar Threads
Why is this output ??
Question on copying objects.
how to store datecombobox in an arraylist of type combobox
Performance issue
Typecasting to a dynamic class