Report this

What is the reason for this report?

Java long to String

Published on August 3, 2022
Java long to String

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 our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:
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.

Still looking for an answer?

Was this helpful?
Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.