Java Long to String

Java Long to String | The Long is a data type supported by Java that can hold up to 64-bit two’s complement, the default value of long is 0L. The long contains a minimum value of -263 and a maximum value of 263-1. The default size of a long is 8 bytes and it is used when we need to hold a higher range of integer values. Whereas string is a sequence of characters. This blog, helps you to know how to convert long to string in Java.

Example:-
1. Long = 1010L
String = “1010”
2. Long = 00000000L
String = “00000000”.

We will see three different ways of converting long to string in Java:-
1. Using Concatenation Operator (+)
2. Using String.valueOf()
3. Using Long.toString()

Converting Long To String Java using “+”

In this code, we convert the long to a string by using the “+” operator. This operator is also known as the concatenation operator.

public class Main{
   public static void main(String args[]) {
      Long number = 45897612354L;
      String string = number + " ";

      System.out.println("Original type: " 
          + number.getClass().getName());
      System.out.println("String: " + string);
      System.out.println("Converted type: " 
          + string.getClass().getName());
   }
}

Output:-

Original type: java.lang.Long
String: 45897612354
Converted type: java.lang.String

In the above example, we have taken the Long type which is a wrapper class but we can also take a primitive type value. Below example demonstrating it:-

public class Main{
   public static void main(String args[]) {
      long number = 123;
      String string = number + " ";

      System.out.println("String: " + string);
      System.out.println("Converted type: " 
           + string.getClass().getName());
   }
}

Output:-

String: 123
Converted type: java.lang.String

Since the number variable is long type (primitive), but not the Long (wrapper class) type therefore we can’t call any method on it, and hence calling getClass() on the number variable will produce a compile-time error.

Java Long to String using String.valueOf()

Here we are using the String.valueOf() method. The valueOf() method converts the data from the internal form to the human-readable form. The valueOf() method is a static method that is overloaded within a string for all the built-in types in Java to convert into the string properly.

The method detail of the valueOf() method is as follows:-
1. public static String valueOf(long l)
2. public static String valueOf(Object obj)

public class Main{
   public static void main(String args[]) {
      Long var = 1234569813231L;
      String string = String.valueOf(var);

      System.out.println("String: " + string);
      System.out.println("Converted type: " 
             + string.getClass().getName());
   }
}

Output:-

String: 1234569813231
Converted type: java.lang.String

Long to String Java using Long.toString()

The third way is by using the Long.toString() method. We use the toString() method to get a string representation of an object.

The method details of toString() are as follows:-
1. public static String toString(long i)
2. public String toString()

public class Main{
   public static void main(String args[]) {
      Long var = 1234569813231L;
      String string = Long.toString(var);
      // Or,
      // String string = var.toString();

      System.out.println("String: " + string);
      System.out.println("Converted type: " 
             + string.getClass().getName());
   }
}

Output:-

String: 1234569813231
Converted type: java.lang.String

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *