How to convert long to String in Java? Example

There are three main ways to convert a long value to a String in Java e.g. by using Long.toString(long value) method, by using String.valueOf(long), and by concatenating with an empty String. You can use any of these methods to convert a long data type into a String object. It's not very different from how you convert an int to String in Java. The same method applies here as well. String concatenation seems the easiest way of converting a long variable to a String, but others are also convenient.  

If you look Java code for valueOf() method from java.lang.String class, you will realize that it actually calls the toString() method of java.lang.Long class for conversion, which means there is only one method where the logic of converting primitive long to String is written.

Nevertheless, if you are not converting long in the loop all methods will perform the same, but if you have to convert a large number of long values to String then directly using Long.toString() method can be quite useful. 


How to convert long to String in Java? 

Here are the three ways to convert a long primitive to String in Java :
  • Using Long.toString() method 
  • Using String.valueOf() method
  • Using String concatenation with empty String
You can use any of these methods to convert a long value to a String in Java, but String.valueOf() is the recommended way in Java.


How to convert long to String in Java? Example



Java Program to convert along value to String in Java

Here is our sample Java program for this data type conversion. In this program, you will learn all these three methods of converting primitive long to String in Java.

/**
 * Java Program to convert long to String in Java
 * 
 * @author java67
 */

public class LongToStringInJava {

    public static void main(String args[]) {

        // let's first create a long variable for conversion
        long big = 3344344323434343L;

        // converting long to String using String concatenation
        // you just need to add number with empty String
        // as shown below
        String s1 = "" + big;
        System.out.println("String value created from long by concatenation : "
                + s1);

        // long to String conversion using Long.toString() method
        String s2 = Long.toString(big);
        System.out.println("String to long using Long.toString() : " + s2);

        // long value to String using String.valueOf()
        String s3 = String.valueOf(big);
        System.out.println("String to long using String.valueOf() : " + s3);

    }

}

Output
String value created from long by concatenation : 3344344323434343
String to long using Long.toString() : 3344344323434343
String to long using String.valueOf() : 3344344323434343

From the output, it's quite clear that we were able to convert the big long value into String without any problem. Both Long.toString() and String.valueOf() method is also able to convert a long value with a positive and negative sign to corresponding String literal as shown below :

long longWithPlusSign = +300L;
String str1 = Long.toString(longWithPlusSign);
        
long longWithMinusSign = -333L;
String str2 = String.valueOf(longWithMinusSign);
        
System.out.println("long to String with plus sign : " + str1);
System.out.println("long to String with minus sign : " + str2);

Output
long to String with plus sign : 300
long to String with minus sign : -333



Things to remember

1) String.valueOf(long) method internally calls Long.toString() method.
2) If you pass a sign along with a number e.g. positive or negative sign then converted String will also contain that sign but only for negative numbers, not for positives.

That's all about how to convert long values to String in Java. It's better to use Long.toString() method because all other methods internally call this one only. If you know how to convert int to String or double to String, you can follow the same steps to convert other data types to String in Java as well.

If you like this data type conversion tutorial in Java, you may also enjoy following tutorials about converting String to other types e.g. Date, Enum, etc
  • How to convert ByteBuffer to String in Java? (program)
  • How to convert String to Double in Java? (example)
  • How to convert float to String in Java? (example)
  • How to convert byte array to String in Java? (program)
  • How to convert Double to Long in Java? (program)
  • How to convert String to Date in a thread-safe manner? (example)
  • How to convert String to int in Java? (example)
  • How to convert Decimal to Binary in Java? (example)
  • How to convert Enum to String in Java? (example)

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.