Mudassir
Mudassir

Reputation: 13174

Efficient Method of Converting int to String in Java

Integer.toString(int);

and

String.valueOf(int);

Which one among the above two methods is the efficient way of converting an int to String?

Thanks in advance.

Upvotes: 2

Views: 3739

Answers (3)

Zaid
Zaid

Reputation: 21

I think that the String.valueOf() method was just provided for the purpose of flexibility, Since the purpose is closely related with String class(in fact for the conversion to String). While the (Integer/Float/etc).toString() method is the authentic method for the purpose of String conversion. You can refer them as slightly overloaded method.

Upvotes: 2

Cameron Skinner
Cameron Skinner

Reputation: 54276

String.valueOf calls Integer.toString, so I guess you could argue that Integer.toString is marginally more efficient.

EDIT: With a modern compiler the calls will be inlined so there should be no difference at all between the two. With an ancient compiler the difference should still be negligible.

Upvotes: 8

Chromium
Chromium

Reputation: 1073

Actually it doesn't matter which method you use. But I think Integer.toString(int); is more efficient because, String.toString(int); is internally calling the same method.

Upvotes: 1

Related Questions