- 8 ways to convert long to String in Java [Practical Examples]
- Different methods to convert long to String in Java
- Method-1: Using + operator
- Method-2: Using String.valueOf()
- Method-3: Using Long.toString()
- Method-4: Using DecimalFormat class
- Method-5: Using String.format()
- Method-6: Using StringBuilder class
- Method-7: Using StringBuffer class
- Method-8: Using Long Constructor
- Summary
- References
- Leave a Comment Cancel reply
- Java Tutorial
- Java long to String
- Java Long to String
- Using + operator
- Long.toString()
- String.valueOf()
- new Long(long l)
- String.format()
- DecimalFormat
- StringBuilder, StringBuffer
- Java Long to String Example
- Convert Long to String with Radix
8 ways to convert long to String in Java [Practical Examples]
Different methods to convert long to String in Java
In java, long is a primitive data type whereas Long is a wrapper class. As we know that java supports auto-boxing, so we can use long and Long interchangeably in most of the cases.
In several applications, we may need to convert a long number to a string because we need to operate on the value in its string form. In many GUI based application also, if we have to display long value in text field we may need to convert it before displaying. There are several ways to convert long type data to a string. The list below shows eight different ways in which we can convert long to string.
- Using + operator
- Using String.valueOf()
- Using Long.toString()
- Using DecimalFormat class
- Using String.format()
- Using StringBuilder class
- Using StringBuffer class
- Using Long constructor
Method-1: Using + operator
This is the simplest approach for converting long to a string. Here, we are using a concatenation operator + to store the long value as a string.
Example : In this example, we concatenate blank string «» with long variable l.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 8584157; // Converting long to string String s = l + ""; // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 8584157 The value of String variable is 8584157
Method-2: Using String.valueOf()
In this approach, we are using valueof method of String class that converts long values passed as a parameter into a string.
Example : In this example, long variable l is converted to string using valueOf method.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1587421; // Converting long to string String s = String.valueOf(l); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 1587421 The value of String variable is 1587421
Method-3: Using Long.toString()
In this approach, we are using Long wrapper class to convert long value to a string. Each of the Number subclasses includes a class method, toString() , that will convert its primitive type to a string.
Example :In this example, we are using toString method of Long class for conversion.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1598521; // Converting long to string String s = Long.toString(l); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 1598521 The value of String variable is 1598521
Method-4: Using DecimalFormat class
In this approach, we are using the DecimalFormat class to convert long to String. Moreover, when we do not specify the format it takes the grouping separator comma(,) to separate the long numbers.
Example :In this example, we are using format method of DecimalFormat class for conversion. Here, for one of the string we are not giving any specific format. So, by default it will take comma as a separator.
import java.text.DecimalFormat; // Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1598753; // Converting long to string DecimalFormat df = new DecimalFormat(); String s1 = df.format(l); df = new DecimalFormat("#"); String s2 = df.format(l); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable s1 is " + s1); System.out.println("The value of String variable s2 is " + s2); >>
The value of long variable is 1598753 The value of String variable s1 is 1,598,753 The value of String variable s2 is 1598753
Method-5: Using String.format()
In this approach, we are using format method of a String class to convert long to string.
Example : In this example, we will use the access specifier » %d » as the value to format is a long type.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1597532; // Converting long to string String s = String.format("%d", l); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 1597532 The value of String variable is 1597532
Method-6: Using StringBuilder class
In this approach, we are using append method of StringBuilder class. The append() method is used to append the string representation of the long argument to this sequence.
Example : In this example, we will use the append() method to add long value and toString() method to convert it to string.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1598753; // Converting long to string StringBuilder b = new StringBuilder(); String s = b.append(l).toString(); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 1598753 The value of String variable is 1598753
Method-7: Using StringBuffer class
In this approach, we are using append method of StringBuffer class. The append() method is used to append the string representation of the long argument to this sequence.
Example : In this example, we will use the append() method to add long value and toString() method to convert it to string.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1598753; // Converting long to string StringBuffer b = new StringBuffer(); String s = b.append(l).toString(); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 1598753 The value of String variable is 1598753
Method-8: Using Long Constructor
In this approach, we are using constructor of Long class to initialize with long value and then use toString() method to convert Long object to the string. Although, this approach is deprecated from Java version 9, it can be used in the older versions.
Example : In this example, we will use the constructor to initialize the object with value and toString() method to convert it to string.
// Program to convert long to string in Java public class Main < public static void main(String[] args) < // Initializing the variable long l = 1598753; // Converting long to string Long o = new Long(l); String s = o.toString(); // Printing both the variables System.out.println("The value of long variable is " + l); System.out.println("The value of String variable is " + s); >>
The value of long variable is 1598753 The value of String variable is 1598753
Summary
The knowledge of converting long value to a String in Java is very useful while working on real time applications. In this tutorial, we covered eight different approaches to convert long to String in Java. As per the requirement of an application, we can choose an appropriate approach for conversion. We learned in detail about this approaches with an example.
All in all, this tutorial, covers everything that you need to know in order to have a clear view on conversion of long value to a string object in Java.
References
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Leave a Comment Cancel reply
Java Tutorial
- Set Up Java Environment
- Set Up Java on Linux
- Set up Java with BlueJ IDE
- Set up Java with VSC IDE
- Set up Java with Eclipse IDE
- Java Multiline Comments
- Java Variables
- Java Global Variables
- Java Date & Time Format
- Different Java Data Types
- Java Booleans
- Java Strings
- Java Array
- Java Byte
- Java convert list to map
- Java convert double to string
- Java convert String to Date
- Java convert Set to List
- Java convert char to int
- Java convert long to string
- Java Operators Introduction
- Java Boolean Operators
- Java Relational Operators
- Java Arithmetic Operators
- Java Bitwise Operators
- Java Unary Operators
- Java Logical Operators
- Java XOR (^) Operator
- Java Switch Statement
- Java If Else Statement
- Java While Loop
- Java For / For Each Loop
- Java Break Continue
- Java Nested Loops
- Java throw exception
- Java Try Catch
- Java Accessor and Mutator Methods
- Java main() Method
- IndexOf() Java Method
- Java ListIterator() Method
- Java create & write to file
- Java read file
- Java Parameter
- Java Argument
- Java Optional Parameters
- Java Arguments vs Parameters
- Java Arrays.asList
- Java HashSet
- Java Math
- Java HashMap vs Hashtable vs HashSet
- Java LinkedList
- Linked List Cycle
- Java List vs LinkedList
- Java ArrayList vs LinkedList
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
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
long l = 123L; String str = l+""; // str is '123'
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'
String.valueOf()
long l = 12345L; String str = String.valueOf(l); // str is '12345'
new Long(long l)
long l = 12345L; //deprecated from Java 9, use valueOf for better performance String str = new Long(l).toString(); // str is '12345'
String.format()
long l = 369L; String s = String.format("%d", l);
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'
StringBuilder, StringBuffer
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.