esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I convert a primitive to a String?
There are several ways you can do this. In Java programs, you will often see that a primitive is converted by using the concatenation operator on Strings ("" + primitive).
A more performant way is to use the method toString on the Object that represents the primitive or to use the static function valueOf in the String class.
Here's an example:



public class TestProg
{
public static void main(String args[])
{
short p_short = 4;
int p_int = 10;
long p_long = 2349945821828L;
float p_float = 3.1415F;
double p_double = 0.00000000004D;
boolean p_boolean = false;
char p_char = 'E';
char p_array[] = { 'e', 's', 'u', 's' };


System.out.println(""+p_short); // 4
System.out.println(Short.toString(p_short)); // 4
System.out.println(String.valueOf(p_short)); // 4 (casted to an int!)

System.out.println(""+p_int); // 10
System.out.println(Integer.toString(p_int)); // 10
System.out.println(String.valueOf(p_int)); // 10

System.out.println(""+p_long); // 2349945821828
System.out.println(Long.toString(p_long)); // 2349945821828
System.out.println(String.valueOf(p_long)); // 2349945821828

System.out.println(""+p_float); // 3.1415
System.out.println(Float.toString(p_float)); // 3.1415
System.out.println(String.valueOf(p_float)); // 3.1415

System.out.println(""+p_double); // 4.0E-11
System.out.println(Double.toString(p_double)); // 4.0E-11
System.out.println(String.valueOf(p_double)); // 4.0E-11

System.out.println(""+p_boolean); // false
System.out.println(new Boolean(p_boolean).toString()); // false
System.out.println(String.valueOf(p_boolean)); // false

System.out.println(""+p_char); // E
System.out.println(new Character(p_char).toString()); // E
System.out.println(String.valueOf(p_char)); // E

System.out.println(""+p_array); // esus
System.out.println(p_array.toString()); // [C@1a
System.out.println(String.valueOf(p_array)); // esus
}
}


(Notice that the Boolean and Character classes do not have a static method toString(). In that case, you have to instantiate an object and call the toString method on it.)


To test out the difference in performance of using toString() or valueOf() over ""+p, I ran the following test:



public class TestPerformance
{
public static void main(String args[])
{
double p_double = 0.00000000004D;
long start, end;

System.out.println("Converting to String using concatenation");
start = System.currentTimeMillis();
for (int i=0; i<100000; i++)
String s = "" + p_double;
end = System.currentTimeMillis();
System.out.println("\tElapsed time: " + (end - start));


System.out.println("Converting to String using valueOf");
start = System.currentTimeMillis();
for (int i=0; i<100000; i++)
String s = String.valueOf(p_double);
end = System.currentTimeMillis();
System.out.println("\tElapsed time: " + (end - start));


System.out.println("Converting to String using toString");
start = System.currentTimeMillis();
for (int i=0; i<100000; i++)
String s = Double.toString(p_double);
end = System.currentTimeMillis();
System.out.println("\tElapsed time: " + (end - start));
}
}


The output (times may vary!) shows a performance gain of almost 20%!



Converting to String using concatenation
Elapsed time: 5270
Converting to String using valueOf
Elapsed time: 4230
Converting to String using toString
Elapsed time: 4230




Further Information
Author of answer: unknown

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.