String class provides us with the method String.valueOf(some stuff) that basically converts "some stuff" into a string..
But we can also concatenate "some stuff" with an empty String ("").
Both these methods result in a String representation of an object(or other data type) so my question is what is the difference between the two, advantages of one over the other, disadvantages of one over the other and which method do you guys usually use when forced to convert something to a String..
darek9576
0
Newbie Poster
Recommended Answers
Jump to PostI've never used the valueOf() method. Just use the '+' operator. The + operator creates a new String in the background anyway. The valueOf() method returns a new String so it does the same. Neither method has a time advantage that anyone cares about; although I don't claim to know …
Jump to PostActually I have written a program that times those 2 methods and the String.valueOf() method is much faster than the other:
Try this code:int N = 1000000; long time = System.currentTimeMillis(); for (int i=0;i<N;i++) { String a = ""+i; } time = System.currentTimeMillis() - …
All 6 Replies
BestJewSinceJC
700
Posting Maven
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
BestJewSinceJC
700
Posting Maven
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
BestJewSinceJC
700
Posting Maven
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.