Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

How to convert long to string in Java

In this post, we are going to convert long to String in Java. long is one of the Java data types that is used to store a wide range of numerical values while String is a sequence of characters.

To convert long type to string, we can use the valueOf() method of String class or toString() method of the Long class. Long is a wrapper class in Java that is used to handle long type objects.

The valueOf() method of String class is used to get a string from long. It takes a single argument and returns a string of the specified type.

The toString() method of the Long class returns a string of long type value.

We used plus (+) operator as well to convert long to string because this operator used to concatenate two objects and returns a string.

Time for an Example:

Let's take an example to convert long to a string. Here, we are using the valueOf() method of String class that returns a string.

public class Main {
	public static void main(String[] args){
		long val = 210;
		System.out.println(val);
		// long to String
		String str = String.valueOf(val);
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


210
210
java.lang.String

Example:

Let's take another example to get a string from a long type. Here, we are using the toString() method of the Long class that returns a string by converting long.

public class Main {
	public static void main(String[] args){
		long val = 210;
		System.out.println(val);
		// long to String
		String str = Long.toString(val);
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


210
210
java.lang.String

Example:

In this example, we are using plus(+) operator to concatenate long value with a string value. The plus operator returns a string value after concatenation.

public class Main {
	public static void main(String[] args){
		long val = 210;
		System.out.println(val);
		// long to String
		String str = ""+val;
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


210
210
java.lang.String