Tutorials  /  JavaScript

Java long to String - Tutorial

Ccentron Redaktion · March 2024 ·4 min read ·JavaScript, 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.

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.

Using + operator

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

Code
long l = 123L;
String str = l+""; // str is '123'
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

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.

Code
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.

Code
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.

String.valueOf()

Code
long l = 12345L;
String str = String.valueOf(l); // str is '12345'

new Long(long l)

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

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

String.format()

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

DecimalFormat

Code
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'

StringBuilder, StringBuffer

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

Code
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.

Go
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.

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren