BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java long to String Conversion

Last Updated: November 20, 2022 by Chaitanya Singh | Filed Under: java

In this guide, we will discuss the following ways to convert a long value to a String:

  • String.valueOf(long l) Method
  • Long.toString(long l) Method
  • String.format() Method
  • Using DecimalFormat
  • Using StringBuilder and StringBuffer

Note: Out of all these ways, the String.valueOf() is the preferred method for conversion as it is null safe. If the long value is null, the String contains "null" after conversion, instead of throwing NullPointerException.

1. Using String.valueOf() Method

We can use String.valueOf(long l) method to convert long to String. This method takes a long value as an argument and returns a string representation of it.

Method Syntax:

public static String valueOf(long l)

Parameters:
l – long value that needs to be converted into a String

Returns:
String representation of long argument l.

long lvar = 123;
String str = String.valueOf(lvar);

Java long to String Example using String.valueOf()

public class JavaExample{
  public static void main(String args[]){
    long l = 100000001L;
    //long to String Conversion
    String str = String.valueOf(l);
    //Print String value
    System.out.println("String after conversion: "+str);
  }
}

Output:

long to String String.valueOf() Example Output

2. Using Long.toString() Method

This is the another way of long to String conversion. The method Long.toString(long l) works same as String.valueOf(long) method. It returns the string representation of the long value passed as an argument to this method. For example, if the passed long value is 1202 then the returned string would be “1202”.

Method Syntax:

public static String toString(long l)

Parameters:
l – long value whose String representation is to be determined.

Returns:
String representation of the given long value l.

long lvar2 = 200;
String str2 = Long.toString(lvar2);

Note: If the long value is null then it throws NullPointerException, it is better to use String.valueOf() method in this case.

Java long to String Example using Long.toString()

public class JavaExample{
  public static void main(String args[]){
    //long values are suffixed with 'L'
    long l = 123456789L;
    //passing the long value to the toString() method
    String str = Long.toString(l);
    //Print the string value returned by Long.toString() method
    System.out.println("String after conversion: "+str);
  }
}

Output:

long to String using Long.toString() Output

3. Using String.format() Method

String.format() was introduced in java 5. We can use this method to convert a long value to String as shown in the following program.

public class JavaExample{
  public static void main(String args[]){
    long l = 1234567L;
    String str = String.format("%d",l);

    //Output: "1234567"
    System.out.println("long to String: "+str);
  }
}

This method is especially useful, if you want to add leading zeroes in the output. Let’s see how can we add leading zeroes in the output String.

public class JavaExample {
  public static void main(String[] args) {
    long l = 12345L;

    // In %09:
    // 0: pad string with zeroes
    // 9: set the length of output string to 9
    String str = String.format("%09d", l);
    System.out.println("long to String with leading zeroes: "+str);
  }
}

Output:

long to String with leading zeroes

4. Using DecimalFormat

You can use DecimalFormat to convert the given long value to String with or without commas.

With Commas:

import java.text.*;
public class JavaExample{
  public static void main(String args[]) {
    long l = 12345678L;
    String str = DecimalFormat.getNumberInstance().format(l);
    System.out.println("long to String: "+str);
  }
}

Output:

long to String conversion with commas

Without Commas:

import java.text.*;
public class JavaExample{
  public static void main(String args[]) {
    long l = 12345678L;
    //Specify the pattern '#' to avoid commas in the output
    String str = new DecimalFormat("#").format(l);;
    System.out.println("long to String: "+str);
  }
}

Output:

long to String without the commas

5. Using StringBuffer and StringBuilder

We can also use StringBuffer and StringBuilder class to convert the given long primitive value or Long object to a String.

public class JavaExample{
  public static void main(String args[]) {
    long l = 1000001L;
    Long obj = 1000005L;
    String str = new StringBuilder().append(l).toString();
    String str2 = new StringBuilder().append(obj).toString();
    String str3 = new StringBuffer().append(l).toString();
    String str4 = new StringBuffer().append(obj).toString();
    System.out.println(str); //Output: "1000001"
    System.out.println(str2); //Output: "1000005"
    System.out.println(str3); //Output: "1000001"
    System.out.println(str4); //Output: "1000005"
  }
}

Recommended Posts

  • Java String to long Conversion
  • Java int to String Conversion
  • Java double to String Conversion
  • Java char to String Conversion
  • Java float to String Conversion
  • Java Date to String Conversion
  • Java boolean to String Conversion
  • Java long to int Conversion
  • Java long Keyword
❮ PreviousNext ❯

Top Related Articles:

  1. Java – String regionMatches() Method example
  2. Java – boolean to String conversion
  3. Java String join() method
  4. Java Integer byteValue() Method
  5. Java String contains() method

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap