This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to string conversions threadsafe?

 
Stephen Peterson
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a need in a servlet to convert an integer to a string,
I know of these 2 options:
1) String s = Integer.toString(i)
2) String s = (new Integer(i)).toString()
It seems that the 1st method would be more memory-efficient.
But is the 1st method not threadsafe if it is not synchronized,
and thus would I be better off using the 2nd method?
------------------
United Health Group
Hartford, CT, USA
 
Phil Hanna
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you think Integer.toString(i) is not threadsafe? It does not access anything but its own local variables.
Most often, I use String.valueOf(i).
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
 
Stephen Peterson
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,
The point I am not clear on, is whether local variables work in static methods the same way they work in non-static methods.
I've read that local variables are created on a stack that is assigned by the JVM to each thread. You seem to be saying that this is true regardless of whether the method is static or non-static. I could not find a statement that explicitly said that in the Java 2.0 language spec document at
http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html
But I may not know where to look.
Is there a way I prove to myself that local variables are always threadsafe in a static method? Does the language spec spell this behavior out somewhere?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13046
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are always Thread safe because the only reference is in the stack of a single Thread. There is no difference between static and instance methods in the way they are called. Probably the reason you didn't find a statement to that effect is that it never occurred to anybody to doubt it.
When in doubt about any method - look at the source code for the class.
Bill
 
Stephen Peterson
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Bill. I believe you.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic