File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes converting a double to a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "converting a double to a String" Watch "converting a double to a String" New topic

converting a double to a String

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
hello all

I have a ResultSet (rs) from a jdbc query that contains 500 rows
I'm looping over it and for each I need to get a double out of it, manipulate it, and convert it into a String
what's the most efficient way to do it?
currently I use rs.getDouble()
do the manipulation
use the static method Double.valueOf() to convert to a Double
then us the Dougles toString() method
it's to slow, is there a faster way?

while on the subject what's a good way to profile code in java if I'm using Netbeans for development?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The static method String.valueOf(double) is more efficient than creating the unneeded Double object. But it's very unlikely that this tiny inefficiency will matter at all. JDBC drivers have to do a lot of work behind the scenes, and it's likely that any performance issues have more to do with database communications than with creating a small number of simple objects.

[Jess in Action][AskingGoodQuestions]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63857

You could try String.valueOf()

Originally posted by Dave Robbins:
while on the subject ...

It's best to not gang up multiple questions in one topic. Either one question will end up being ignored or you could end up with an entwined thread that's just confusing.

Especially when one of the questions would be better asked in a completely separate forum.

[Asking smart questions] [About Bear] [Books by Bear]
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
The loop is below
This is happening after calling the database so I would think jdbc issues are not the problem. I declared all my variables outside the loop so they don't constantly get recreated. The only potential problem I see is the repeated type conversions from double to String
Does the ResultSet have to do any work when I call getDouble()? The value was a double in the database.
What the code is doing is creating javascript code to place markers on the map thing google gives away so it's dealing with latitude/longitude. If multiple point lay at the same lat/lon I want to offset the numbers a little so the markers don't lie on top of each other
This takes about 10 seconds to run in a debugger and is similarly slow when deployed on Tomcat (it's in a servlet)

Bear, your point is well taken, I'll make another post on that topic

[ May 05, 2007: Message edited by: Dave Robbins ]
Dhaval Shah
Ranch Hand

Joined: Jun 10, 2005
Posts: 56
As you have mentioned that there are approx 500 rows in result set and each time the loop is executed 3 String objects are getting created on the heap. I am not so sure. . . but just try replacing the String vars by StringBuffer vars as String objects generally hit performance while manipulating them, which is not the case with StringBuffer

Thanks and Regards,<br />Dhaval Shah<br />SCJP 1.4<br />SCWCD 1.4
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131

I'm not sure what you're referring to
Do you mean where I keep appending stuff onto str?
I'm going the try changing str to a StringBuffer, will let you know the outcome

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
that took it from 10 seconds down to less than 1
thanks, I just learned a new trick

I agree. Here's the link:
subject: converting a double to a String
It's not a secret anymore!