-1

So let's have I have the long 12321431 , how would I store that as a String variable inside of Java?

Or let's say 10.15, how would I store it inside a String?

I'm still a newbie at this and I need to do this for file-io purposes.

X1XX
  • 141
  • 2
  • 13
  • 1
    possible duplicate of [Java:Convert / Cast long to String ?](http://stackoverflow.com/questions/1854924/javaconvert-cast-long-to-string) – cchapman Mar 19 '15 at 00:33

3 Answers3

1

Simply use String.valueOf to convert it into a String.

Dici
  • 25,226
  • 7
  • 41
  • 82
1

As Robert and Dici mentioned, you can use either of the two variations below:

num.toString(); // string representation

String.valueOf(num); // calls toString

String.valueOf calls the toString method of an object. It is now convention to cast using static methods rather than instance methods derived from the most basic Object class. I felt like the underlying code and reasoning was lacking in the other responses.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

String strLong = Long.toString(longNumber);

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • `String.valueOf` calls the `toString` method of an object. It is now convention to cast using static methods rather than instance methods derived from the most basic Object class. – Malik Brahimi Mar 19 '15 at 00:43