Dan Ciborowski - MSFT
Dan Ciborowski - MSFT

Reputation: 7217

Java: Returning a String of a primitive type

I have a variable of type "long"

 long time = System.currntTimeMillis();

I would like to pass it to a method that requires a string. If this wasn't a primative type I would call time.toString(): but that is not a valid method.

What I am doing is

 method("" + time);

And this creates a string, but is there a better way or more optimal way to do this?

Upvotes: 1

Views: 105

Answers (4)

buc
buc

Reputation: 6358

Or

method(String.valueof(time));

Upvotes: 1

rocketboy
rocketboy

Reputation: 9741

 method("" + time);

is inefficient. String API exposes lots of static utility overloaded methods for different types:

String.valueOf(time)

Upvotes: 1

BobTheBuilder
BobTheBuilder

Reputation: 19294

You can use:

Long.toString(time) or String.valueOf(time).

Take a look at this answer

Upvotes: 5

Reimeus
Reimeus

Reputation: 159844

Perhaps

method(Long.toString(time));

Upvotes: 4

Related Questions