Tutorial

Java long to String

Published on August 3, 2022
Default avatar

By Pankaj

Java long to String

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Today we will see different ways to convert long to string in java. Java long to string conversion can be done in many ways, we will go through them one by one with example code snippets.

Java Long to String

java long to string Let’s look at different code snippets for java long to string conversion. Note that long is a primitive data type whereas Long is an Object. However java supports autoboxing, so they both can be used interchangeably in most of the cases.

  1. Using + operator

    This is the easiest way to convert long to string in java.

    long l = 123L;
    String str = l+""; // str is '123'
    
  2. Long.toString()

    We can use Long class toString method to get the string representation of long in decimal points. Below code snippet shows you how to use it to convert long to string in java.

    long l = 12345L;
    String str = Long.toString(l);
    System.out.println(str); //prints '12345'
    

    We can write long in other formats too such as Octal and Hexadecimal, let’s see what happens when we convert long to string in those scenarios.

    long l = 0x11L;
    String str = Long.toString(l);
    System.out.println(str); //prints '17'
    l = 011L;
    str = Long.toString(l);
    System.out.println(str); //prints '9'
    

    So the string is always being returned in decimal format. Let’s look into other ways to convert long to string too.

  3. String.valueOf()

    long l = 12345L;
    String str = String.valueOf(l); // str is '12345'
    
  4. new Long(long l)

    Long constructor with long argument has been deprecated in Java 9, but you should know it.

    long l = 12345L;
    //deprecated from Java 9, use valueOf for better performance
    String str = new Long(l).toString(); // str is '12345'
    
  5. String.format()

    long l = 369L;
    String s = String.format("%d", l);
    
  6. DecimalFormat

    long l = 12345L;
    String str = DecimalFormat.getNumberInstance().format(l);
    System.out.println(str); //str is '12,345'
    //if you don't want formatting
    str = new DecimalFormat("#").format(l);
    System.out.println(str); //str is '12345'
    
  7. StringBuilder, StringBuffer

    We can use StringBuilder and StringBuffer append function to convert long to string.

    long l = 12345L;
    String str = new StringBuilder().append(l).toString();
    

Java Long to String Example

Here is a simple program where we will convert long to string and print it using all the different methods we saw above.

package com.journaldev.string;

import java.text.DecimalFormat;

public class JavaLongToString {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		long l = 12345L;
		String str = Long.toString(l);
		System.out.println(str);

		str = String.valueOf(l);
		System.out.println(str);

		// deprecated from Java 9, use valueOf for better performance
		str = new Long(l).toString();
		System.out.println(str);

		str = String.format("%d", l);
		System.out.println(str);

		str = l + "";
		System.out.println(str);

		str = DecimalFormat.getNumberInstance().format(l);
		System.out.println(str);

		str = new DecimalFormat("#").format(l);
		System.out.println(str);

		str = new StringBuilder().append(l).toString();
		System.out.println(str);
	}
}

Change the long value in above program to Octal or Hexadecimal format, you will notice that string representation is always on decimal format.

Convert Long to String with Radix

What if we want to convert long to string with different radix, not the default decimal point. We can use Long class methods for these cases. Below code snippet shows how to get the string representation of a long variable in different bases such as Octal, Hex or even custom one.

long number = 45;
System.out.println(Long.toBinaryString(number)); //101101
System.out.println(Long.toOctalString(number)); //55
System.out.println(Long.toHexString(number)); //2d
System.out.println(Long.toString(number, 5)); //140

That’s all for converting long to string in java program. Reference: Long API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel