Java – Long to String conversion in 6 ways

In this article, we will discuss various ways to convert Long to String in Java

Q) What is the need of converting primitive long or Long wrapper-type to String ?

  • Generally, whenever we receive or send data from web application then it is passed in the form of String only
  • After mathematical calculation, all numbers need to be converted into String for displaying
  • This article explains about primitive long or Long wrapper-type to String conversion only, but we can do conversion for other types like int, double, float, etc. to String object
  • Note: Likewise, sometime String to Long conversion is also required
  • Read String class in detail with example

1. Various ways to convert Long to String

  1. Using Long.toString(longValue);
  2. Using String.valueOf(longValue);
  3. Using String.format(type, longValue);
  4. Create Long object and then convert to String using toString() method {new Long(longValue).toString();}
  5. Adding double quotes (“”) to long value {i.e.; “” + longValue;}
  6. Using append() method of StringBuffer & StringBuilder

Let us move forward and discuss all possible ways to convert Long to String in Java

1.1 Using Long.toString(longValue); method

  • This method can be used to convert primitive long data-type or Long wrapper-type to String object
  • Negative long data-type can also be converted to String type, as shown in the below example

Method signature:

public static String toString(long i);

ConvertLongIntoStringUsingToStringMethod.java

package in.bench.resources.longg.to.string.conversion;

public class ConvertLongIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive long data-type
		long longValue1 = 545451236789083488L;

		// 1. converting long to String
		// by using toString() method
		String str1 = Long.toString(longValue1);
		System.out.println("1. Converted"
				+ " primitive long to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		long longValue2 = -5454523943984875L;

		// 2. converting long to String
		// by using toString() method
		String str2 = Long.toString(longValue2);
		System.out.println("\n2. Converted negative"
				+ " primitive long to String value is : "
				+ str2);

		// Long object
		Long longValue3 = new Long(12345678900402498L);

		// 3. converting Long object to String
		// by using toString() method
		String str3 = Long.toString(longValue3);
		System.out.println("\n3. Converted"
				+ " Long object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive long to String value is :
	545451236789083488

2. Converted negative primitive long to String value is :
	-5454523943984875

3. Converted Long object to String value is :
	1234567890

1.2 Using String.valueOf(longValue); method

  • This method can be used to convert primitive long data-type or Long wrapper-type to String object
  • Negative long data-type can also be converted to String type, as shown in the below example

Method signature:

public static String valueOf(long l);

ConvertLongIntoStringUsingValueOfMethod.java

package in.bench.resources.longg.to.string.conversion;

public class ConvertLongIntoStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive long data-type
		long longValue1 = 989891234567890L;

		// 1. converting long to String
		// by using valueOf() method
		String str1 = String.valueOf(longValue1);
		System.out.println("1. Converted"
				+ " primitive long to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		long longValue2 = -989891234567890L;

		// 2. converting negative long to String
		// by using valueOf() method
		String str2 = String.valueOf(longValue2);
		System.out.println("\n2. Converted negative"
				+ " primitive long to String value is : "
				+ str2);

		// Long object
		Long longValue3 = new Long(12345678901234L);

		// 3. converting Long object to String
		// by using valueOf() method
		String str3 = String.valueOf(longValue3);
		System.out.println("\n3. Converted"
				+ " Long object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive long to String value is :
	989891234567890

2. Converted negative primitive long to String value is :
	-989891234567890

3. Converted Long object to String value is :
	12345678901234

1.3 Using String.format(type, number); method

  • This method can be used to convert primitive long data-type or Long wrapper-type to String object
  • Negative long data-type can also be converted to String type, as shown in the below example

Method signature:

public static String format(String format, Object... args);

ConvertLongIntoStringUsingFormatMethod.java

package in.bench.resources.longg.to.string.conversion;

public class ConvertLongIntoStringUsingFormatMethod {

	public static void main(String[] args) {

		// primitive long data-type
		long longValue1 = 664421234567890L;

		// 1. converting long to String
		// by using format() method
		String str1 = String.format("%d", longValue1);
		System.out.println("1. Converted"
				+ " primitive long to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		long longValue2 = -664422344655898L;

		// 2. converting negative long to String
		// by using format() method
		String str2 = String.format("%d", longValue2);
		System.out.println("\n2. Converted negative"
				+ " primitive long to String value is : "
				+ str2);

		// Long object
		Long longValue3 = new Long(54321098765454L);

		// 3. converting Long object to String
		// by using format() method
		String str3 = String.format("%d", longValue3);
		System.out.println("\n3. Converted"
				+ " Long object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive long to String value is :
	664421234567890

2. Converted negative primitive long to String value is :
	-664422344655898

3. Converted Long object to String value is :
	54321098765454

1.4 Create Long object and then convert to String using toString() method {new Long(longValue).toString();}

  • Here, we will create new Long object with primitive long value as constructor-argument
  • After creating new Long object by passing primitive long value
  • then invoke toString() method for converting primitive long data-type to String object

Method signature:

public Long(long value);

public static String toString(int i);

ConvertLongIntoStringUsingObjectCreation.java

package in.bench.resources.longg.to.string.conversion;

public class ConvertLongIntoStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive long data-type
		long longValue1 = 875428243883421L;

		// 1. converting long to String
		// by creating Long object
		Long longObj1 = new Long(longValue1);

		// and then invoke toString(); method
		String str1 = longObj1.toString();
		System.out.println("1. Converted"
				+ " primitive long to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		long longValue2 = -87542123434543635L;

		// 2. converting negative long to String
		// by creating Long object
		Long longObj2 = new Long(longValue2);

		// and then invoke toString(); method
		String str2 = longObj2.toString();
		System.out.println("\n2. Converted negative"
				+ " primitive long to String value is : "
				+ str2);

		// Long object
		Long longValue3 = new Long(147852213432455L);

		// 3. converting Long object to String
		// by creating Long object
		String str3 = longValue3.toString();
		System.out.println("\n3. Converted"
				+ " Long object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive long to String value is :
	875428243883421

2. Converted negative primitive long to String value is :
	-87542123434543635

3. Converted Long object to String value is :
	147852213432455

1.5 Adding double quotes (“”) to long value

  • Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation

Syntax:

String temp = “” + longValue;

ConvertLongIntoStringByAddingDoubleQuotes.java

package in.bench.resources.longg.to.string.conversion;

public class ConvertLongIntoStringByAddingDoubleQuotes {

	public static void main(String[] args) {

		// primitive long data-type
		long longValue1 = 98563234985876956L;

		// 1. converting long to String by adding ""
		String str1 = "" + longValue1;
		System.out.println("1. Converted"
				+ " primitive long to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		long longValue2 = -9856323245946096L;

		// 2. converting negative long to String  by adding ""
		String str2 = "" + longValue2;
		System.out.println("\n2. Converted negative"
				+ " primitive long to String value is : "
				+ str2);

		// Long object
		Long longValue3 = new Long(543459894665786521L);

		// 3. converting Long object to String  by adding ""
		String str3 = "" + longValue3;
		System.out.println("\n3. Converted"
				+ " Long object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive long to String value is :
	98563234985876956

2. Converted negative primitive long to String value is :
	-9856323245946096

3. Converted Long object to String value is :
	543459894665786521

1.6 Using append() method of StringBuffer & StringBuilder

  • This method can be used to convert primitive long data-type to String object
  • Only sign is allowed, like minus (-)

Syntax:

StringBuffer sb = sb.append(longValue);

String temp = sb.toString();

ConvertLongIntoStringByAppending.java

package in.bench.resources.longg.to.string.conversion;

public class ConvertLongIntoStringByAppending {

	public static void main(String[] args) {

		// primitive long data-type
		long longValue1 = 213659234245356L;

		// 1. converting long to String
		// by using append() method
		StringBuffer sb1 = new StringBuffer();
		String str1 = sb1.append(longValue1).toString();
		System.out.println("1. Converted"
				+ " primitive long to String value is : "
				+ str1);

		// primitive long data-type (negative value)
		long longValue2 = -21365909845672L;

		// 2. converting negative long to String
		// by using append() method
		StringBuffer sb2 = new StringBuffer();
		String str2 = sb2.append(longValue2).toString();
		System.out.println("\n2. Converted negative"
				+ " primitive long to String value is : "
				+ str2);

		// Long object
		Long longValue3 = new Long(867514123456L);

		// 3. converting Long object to String
		// by using append() method
		StringBuffer sb3 = new StringBuffer();
		String str3 = sb3.append(longValue3).toString();
		System.out.println("\n3. Converted"
				+ " Long object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive long to String value is :
	213659234245356

2. Converted negative primitive long to String value is :
	-21365909845672

3. Converted Long object to String value is :
	867514123456

Q) What if we want to convert to Long wrapper-type ?

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to respective wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing feature

2. Auto-boxing and un-boxing feature from Java 1.5 version:

  • parseLong() method returns primitive long data-type, but it can be easily used as Long wrapper-type
  • as auto-boxing feature helps to convert primitive data-type to respective wrapper-types
  • similarly, Long.valueOf() method returns Long wrapper-type, but it can also be used as primitive long

AutoBoxingFeatureForLongConversion.java

package in.bench.resources.string.to.longg.conversion;

public class AutoBoxingFeatureForLongConversion {

	public static void main(String[] args) {

		// String with only digits
		String str1 = "12345678901234567";

		// string to long conversion
		long longValue1 = Long.parseLong(str1);

		// 1. Auto-Boxing - converting long to Long
		Long lAutoBoxing = longValue1;
		System.out.println("1. Auto-Boxing : "
				+ lAutoBoxing);

		// String with only digits and a minus sign (-)
		String str2 = "-9876543210987654";

		// string to long conversion
		Long longValue2 = Long.parseLong(str2);

		// 1. Un-Boxing - converting long to Long
		long lUnBoxing = longValue2;
		System.out.println("\n2. Un-Boxing   : "
				+ lUnBoxing);
	}
}

Output:

1. Auto-Boxing : 12345678901234567

2. Un-Boxing   : -9876543210987654

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String to boolean conversion in 3 ways
Java - String to long conversion in 3 ways