Table of contents
1.
Introduction
2.
What are Long And String Datatypes?
3.
Methods To Convert Long To String In Java
3.1.
1. Using the + operator
3.2.
2. Using String.valueOf() method
3.3.
3. Using Long.toString() method
3.3.1.
Syntax:
3.4.
4. Using String.format() method
3.5.
5. Using the new Long Method
3.5.1.
Syntax:
3.6.
6. Using the String Builder Class
3.6.1.
Syntax:
3.6.2.
Code Example:
3.6.2.1.
Output:
4.
The Need for Conversion for Long to String in Java
5.
Frequently Asked Questions
5.1.
How to read long String in Java?
5.2.
How do I compare two Strings in Java?
5.3.
How do I convert a String to a long in Java?
5.4.
What is the default value of a long variable in Java?
6.
Conclusion
Last Updated: Jan 17, 2025
Easy

Converting Long to String in Java

Author Lokesh Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java programming, converting a long datatype to a String is a common task encountered in various scenarios. Java offers multiple straightforward methods for this conversion, each with its unique use cases. This article delves into the significance of converting long to String, the reasons for performing such conversions, and explores different techniques to achieve it efficiently.

converting long to string in java

In this article, we will first understand the reasons why we may need to perform this conversion. We will then look at the ways to convert a long to string in Java.

What are Long And String Datatypes?

In Java, a long is a numeric data type that can hold a 64-bit signed two’s complement integer. It is used to represent a broader range of values than the int data type, which is a 32-bit signed two’s complement integer. 

A 64-bit signed two's complement integer is a type of integer that can store values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 using 64 bits of memory. 

The two's complement representation is a method of storing negative numbers in binary, where the leftmost bit represents the sign (0 for positive, 1 for negative) and the remaining bits represent the magnitude of the number. "Signed" means that the integer can represent negative and positive values.

Similarly, a 32-but signed two’s complement means that it can store values from -2,147,483,648 to 2,147,483,647.

The long data type is useful when working with large numbers or when you need a range larger than what Int can provide.

A String in Java is a class that represents a sequence of characters. Strings are used to represent text and are used for a wide variety of purposes in Java programs. Strings are immutable, which means that once a String object is created, its value cannot be changed.

Here is an example of how long and String variables can be declared and initialized in Java:

long l = 1234567890;
String str = "Hello, World!";
You can also try this code with Online Java Compiler
Run Code

Methods To Convert Long To String In Java

Let us now look at the various methods to convert a long to string in java.

  1. Using + operator
  2. Using String.valueOf()
  3. Using Long.toString()
  4. new Long(long l)
  5. Using String.format()
  6. Using StringBuilder, StringBuffer object

1. Using the operator

We can convert any datatype to a string by adding an empty string to that datatype using the + operator. We represent an empty string using double quotes(“ “).

Let us understand this method with an example. 

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = "" + l;
    System.out.println(str); // prints "1234567890"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output:

1234567890

2. Using String.valueOf() method

You can use the valueOf() method of the String class to convert a long to String in Java. This method returns the string representation of the long argument.

Here's an example that demonstrates how to use the valueOf() method to convert a long value to a String and prints the resulting String:
Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.valueOf(l);
    System.out.println(str); // prints "1234567890"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

1234567890

You can also use the valueOf() method to convert a long value to a hexadecimal String by specifying the radix (base) as 16:

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.valueOf(l, 16); //converts to hexadecimal
    System.out.println(str); // prints "499602d2"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output:

499602d2

3. Using Long.toString() method

You can use the toString() method of the Long class to convert a long to String in Java. This method returns the string representation of the long argument.

Syntax:

String str = Long.toString(LongVar);
You can also try this code with Online Java Compiler
Run Code

Here's an example that demonstrates how to use the toString() method to convert a long value to a String and prints the resulting String:

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = Long.toString(l);
    System.out.println(str); // prints "1234567890"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

1234567890

4. Using String.format() method

You can use the format() method of the String class to convert a long value to a String. This method returns a formatted string using the specified format string and argument.

Here's an example that demonstrates how to use the format() method to convert a long value to a String and prints the resulting String:

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.format("%d", l);
    System.out.println(str); // prints "1234567890"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output:

1234567890

You can also use the format() method to convert a long value to a hexadecimal String by specifying the format string as "%x":

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.format("%x", l); //converts to hexadecimal
    System.out.println(str); // prints "499602d2"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

499602d2

You can also use the format() method to convert a long value to a String with a specific width and alignment. For example, the following code converts a long value to a String with a width of 10 and left-alignment:

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.format("%-10d", l);
    System.out.println(str); // prints "1234567890 " (note the extra space at the end)
  }
}
You can also try this code with Online Java Compiler
Run Code

Output:

1234567890 

5. Using the new Long Method

You can use the toString() method of the Long class by creating a Long object and then calling the toString() method on it to convert a long value to a String. This method returns the string representation of the long value.

Syntax:

String str = new Long(varLong).toString();
You can also try this code with Online Java Compiler
Run Code

Here's an example that demonstrates how to use the new Long() method to convert a long value to a String and prints the resulting String:

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = new Long(l).toString();
    System.out.println(str); // prints "1234567890"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

1234567890

Keep in mind that this method of converting long to String in java is not the most efficient way. It is generally better to use one of the other methods mentioned earlier (e.g. Long.toString(), String.valueOf(), etc.) for converting a long value to a String.

Secondly, this method is now deprecated and is no longer available in Java 9.

You can also read about the topic of Java Destructor

6. Using the String Builder Class

You can use the append() method of the StringBuilder class to convert a long value to a String. The StringBuilder class is a mutable sequence of characters that can be used to build a String.

Syntax:

String str = new StringBuilder().append(LongVar).toString();
You can also try this code with Online Java Compiler
Run Code

Consider the example below.

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    StringBuilder sb = new StringBuilder();
    sb.append(l);
    String str = sb.toString();
    System.out.println(str); // prints "1234567890"
  }
}
You can also try this code with Online Java Compiler
Run Code
Output:
1234567890

You can also use the append() method of the StringBuilder class to convert a long value to a hexadecimal String by specifying the radix (base) as 16. 

Code Example:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    StringBuilder sb = new StringBuilder();
    sb.append(l, 16); //converts to hexadecimal
    String str = sb.toString();
    System.out.println(str); // prints "499602d2"
  }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

499602d2

The Need for Conversion for Long to String in Java

There can be several reasons why you might need to convert a long value to a String in Java:

  1. To display the long value: You may want to display the long value to the user or log it for debugging purposes. Since the output stream expects a String, you must convert the long to String java.
  2. To concatenate with other Strings: You may want to concatenate the long value with other Strings. Since the + operator is used for concatenation, the long value has to be converted to a String.
  3. To pass as an argument: You may want to pass the long value as an argument to a method that expects a String. In such cases, you will have to convert the long value to a String.
  4. To store in a data structure: You may want to store the long value in a data structure that can only hold Strings (e.g., a List). In such cases, you will have to convert the long value to a String.

Frequently Asked Questions

How to read long String in Java?

Long strings can be efficiently read in Java using a BufferedReader. Text lines can be read using a loop, then added to a StringBuilder to create a long string by concatenating them.

How do I compare two Strings in Java?

You can use the equals() method of the String class to compare two Strings in Java. The equals() method compares the characters in the String and returns true if the characters are the same, and false otherwise.

How do I convert a String to a long in Java?

You can use the parseLong() method of the Long class to convert a String to a long in Java. The parseLong() method parses the String argument as a signed decimal long and returns the long value.

What is the default value of a long variable in Java?

A long variable in Java can hold a 64-bit signed two's complement integer. The default value of a long variable in Java is 0.

Conclusion

In conclusion, converting a long to a String in Java is a simple yet essential operation. We explored methods like the + operator, String.valueOf(), Long.toString(), and more, each tailored to specific needs. Mastering these techniques enhances your Java programming efficiency. To deepen your understanding of Java's versatile features, explore our recommended readings and guided paths for coding and interview preparation.

Recommended Readings:

Live masterclass