Convert Long to String in Java

Introduction

In Java, converting a long value to a String is a common task that can be useful in various scenarios, such as formatting numbers for display, logging, and manipulating numerical data. This blog post will explore different methods to convert a long to a String in Java.

Table of Contents

  1. Using Long.toString()
  2. Using String.valueOf()
  3. Using String.format()
  4. Using DecimalFormat
  5. Using String Concatenation
  6. Complete Example Program
  7. Conclusion

1. Using Long.toString()

The Long.toString() method is a static method in the Long class that converts a long value to its string representation.

Example:

public class LongToStringUsingToString {
    public static void main(String[] args) {
        long longValue = 123456789L;

        // Convert long to string using Long.toString()
        String strValue = Long.toString(longValue);

        System.out.println("Long value: " + longValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Long value: 123456789
String value: 123456789

Explanation:

  • Long.toString(longValue) converts the long value to its string representation.

2. Using String.valueOf()

The String.valueOf() method is a static method in the String class that can convert various data types to their string representation, including long values.

Example:

public class LongToStringUsingValueOf {
    public static void main(String[] args) {
        long longValue = 987654321L;

        // Convert long to string using String.valueOf()
        String strValue = String.valueOf(longValue);

        System.out.println("Long value: " + longValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Long value: 987654321
String value: 987654321

Explanation:

  • String.valueOf(longValue) converts the long value to its string representation.

3. Using String.format()

The String.format() method allows you to format a string using format specifiers.

Example:

public class LongToStringUsingFormat {
    public static void main(String[] args) {
        long longValue = 1122334455L;

        // Convert long to string using String.format()
        String strValue = String.format("%d", longValue);

        System.out.println("Long value: " + longValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Long value: 1122334455
String value: 1122334455

Explanation:

  • String.format("%d", longValue) formats the long value to a string using the decimal format specifier %d.

4. Using DecimalFormat

The DecimalFormat class allows you to format numbers to a specific pattern, which can then be converted to a string.

Example:

import java.text.DecimalFormat;

public class LongToStringUsingDecimalFormat {
    public static void main(String[] args) {
        long longValue = 5566778899L;

        // Convert long to string using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#");
        String strValue = decimalFormat.format(longValue);

        System.out.println("Long value: " + longValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Long value: 5566778899
String value: 5566778899

Explanation:

  • DecimalFormat("#") creates a pattern to format the long value.
  • decimalFormat.format(longValue) converts the long value to its string representation with the specified pattern.

5. Using String Concatenation

Another way to convert a long to a String is by concatenating it with an empty string (""). This implicitly calls the toString() method on the long value.

Example:

public class LongToStringUsingConcatenation {
    public static void main(String[] args) {
        long longValue = 3344556677L;

        // Convert long to string using concatenation
        String strValue = longValue + "";

        System.out.println("Long value: " + longValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Long value: 3344556677
String value: 3344556677

Explanation:

  • Concatenating the long value with an empty string converts it to its string representation.

6. Complete Example Program

Here is a complete program that demonstrates all the methods discussed above to convert a long to a String.

Example Code:

import java.text.DecimalFormat;

public class LongToStringExample {
    public static void main(String[] args) {
        long longValue1 = 123456789L;
        long longValue2 = 987654321L;
        long longValue3 = 1122334455L;
        long longValue4 = 5566778899L;
        long longValue5 = 3344556677L;

        // Using Long.toString() Method
        String strValue1 = Long.toString(longValue1);
        System.out.println("Using Long.toString():");
        System.out.println("Long value: " + longValue1 + " -> String value: " + strValue1);

        // Using String.valueOf() Method
        String strValue2 = String.valueOf(longValue2);
        System.out.println("\nUsing String.valueOf():");
        System.out.println("Long value: " + longValue2 + " -> String value: " + strValue2);

        // Using String.format() Method
        String strValue3 = String.format("%d", longValue3);
        System.out.println("\nUsing String.format():");
        System.out.println("Long value: " + longValue3 + " -> String value: " + strValue3);

        // Using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#");
        String strValue4 = decimalFormat.format(longValue4);
        System.out.println("\nUsing DecimalFormat:");
        System.out.println("Long value: " + longValue4 + " -> String value: " + strValue4);

        // Using String Concatenation
        String strValue5 = longValue5 + "";
        System.out.println("\nUsing String Concatenation:");
        System.out.println("Long value: " + longValue5 + " -> String value: " + strValue5);
    }
}

Output:

Using Long.toString():
Long value: 123456789 -> String value: 123456789

Using String.valueOf():
Long value: 987654321 -> String value: 987654321

Using String.format():
Long value: 1122334455 -> String value: 1122334455

Using DecimalFormat:
Long value: 5566778899 -> String value: 5566778899

Using String Concatenation:
Long value: 3344556677 -> String value: 3344556677

7. Conclusion

Converting a long to a String in Java can be accomplished in several ways. The Long.toString() method and String.valueOf() method are both straightforward and widely used. Using String.format() and DecimalFormat provides additional control over the formatting of the long value. String concatenation offers an alternative approach. By understanding these different methods, you can choose the one that best fits your needs and coding style.

Happy coding!

Comments