Java Long toString() Method

The Long.toString() method in Java is used to convert a Long object or a long primitive to its string representation. This method has multiple overloaded versions to handle different use cases, including converting a long value to a string in a specified radix (base).

Table of Contents

  1. Introduction
  2. toString() Method Syntax
  3. Overloaded Versions
    • toString()
    • toString(long i)
    • toString(long i, int radix)
  4. Examples
    • Converting a Long to a String
    • Converting a long to a String
    • Converting a long to a String with Different Radix
  5. Real-World Use Case
  6. Conclusion

Introduction

The Long.toString() method is a versatile tool for converting Long objects and long primitives to their string representations. This method is useful for displaying long values as strings, performing string operations on long values, and converting long values to different numeral systems.

toString()() Method Syntax

There are three primary overloaded versions of the toString() method in the Long class:

  1. public String toString()
  2. public static String toString(long i)
  3. public static String toString(long i, int radix)

Overloaded Versions

1. toString()

This method converts the Long object to a string in base 10.

public String toString()

The method returns:

  • A string representing the value of the Long object in base 10.

2. toString(long i)

This method converts the long value i to a string in base 10.

public static String toString(long i)
  • i: The long value to be converted to a string.

The method returns:

  • A string representing the specified long value in base 10.

3. toString(long i, int radix)

This method converts the long value i to a string in the specified radix (base).

public static String toString(long i, int radix)
  • i: The long value to be converted to a string.
  • radix: The base to use for the conversion (e.g., 2 for binary, 8 for octal, 16 for hexadecimal).

The method returns:

  • A string representing the specified long value in the specified radix.

Examples

Converting a Long to a String

The toString() method can be used to convert a Long object to its string representation in base 10.

Example

public class LongObjectToStringExample {
    public static void main(String[] args) {
        Long longObject = 123456789L;
        String result = longObject.toString();

        System.out.println("String representation of Long object: " + result);
    }
}

Output:

String representation of Long object: 123456789

In this example, the Long object 123456789L is converted to the string "123456789".

Converting a long to a String

The toString(long i) method can be used to convert a long primitive to its string representation in base 10.

Example

public class LongToStringExample {
    public static void main(String[] args) {
        long number = 987654321L;
        String result = Long.toString(number);

        System.out.println("String representation of long: " + result);
    }
}

Output:

String representation of long: 987654321

In this example, the long value 987654321L is converted to the string "987654321".

Converting a long to a String with Different Radix

The toString(long i, int radix) method can be used to convert a long value to its string representation in the specified radix.

Example

public class LongToStringWithRadixExample {
    public static void main(String[] args) {
        long number = 255L;

        String binaryString = Long.toString(number, 2);
        String octalString = Long.toString(number, 8);
        String hexString = Long.toString(number, 16);

        System.out.println("Binary representation of " + number + ": " + binaryString);
        System.out.println("Octal representation of " + number + ": " + octalString);
        System.out.println("Hexadecimal representation of " + number + ": " + hexString);
    }
}

Output:

Binary representation of 255: 11111111
Octal representation of 255: 377
Hexadecimal representation of 255: ff

In this example, the long value 255L is converted to its binary, octal, and hexadecimal string representations.

Real-World Use Case

Logging and Debugging

In a real-world application, you might use the Long.toString() method to convert long values to strings for logging and debugging purposes.

Example

import java.util.logging.Logger;

public class LoggingExample {
    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) {
        long userId = 1024L;
        long orderId = 2048L;

        logger.info("User ID: " + Long.toString(userId));
        logger.info("Order ID: " + Long.toString(orderId, 16)); // Logging in hexadecimal format
    }
}

Output:

INFO: User ID: 1024
INFO: Order ID: 800

In this example, the Long.toString() method is used to convert long values to strings for logging purposes, including logging in different numeral systems.

Conclusion

The Long.toString() method in Java is a versatile tool for converting Long objects and long primitives to their string representations. By understanding how to use the different overloaded versions of this method, you can efficiently handle tasks that involve displaying and manipulating string representations of long values in your Java applications. Whether you are dealing with base 10, different numeral systems, or logging and debugging, the toString() method provides a reliable solution for these tasks.

Comments