Java long to String

In this post, we will see how to convert long to String in java.

There are lot of ways to convert long to String.Let’s see each one by one.


Using Long.toString()

You can use Long class toString() method to convert long to String.

In case, Long can be null and you don’t want to have 4 characters null String, then you should use Objects.toString(i,null) to convert long to String.
Please note that java.util.Objects class is available from JDK 7.

Using String.valueOf()

You can use String’s valueOf method to convert long to String.


Using + operator

You can use + operator to convert long to String.


Using String.format method()

You can also use String’s format() method to convert Long to String.
Here is the simple example.


Using StringBuffer and StringBuilder

You can use StringBuffer and StringBuilder class to convert Long to String.


Using DecimalFormat

You can use DecimalFormat class to convert Long to String.

In case, you need to format String with ,, you can use below code.


Long to String with different Radix

We have seen all the examples with base 10. In case, you want to convert Long to String in different radix, you can use difference Long.toXXXString() method.

Binary

We can use Long’s toBinaryString() to convert Long to String in binary format.

Octal

We can use Long’s toOctalString() to convert Long to String in Octal format.

Hexadecimal

We can use Long’s toHexString() to convert Long to String in HexaDecimal format.

Java program for Long to String Conversion

Output:

Using Long’s toString(): 22
Using Object’s toString(): null
Using String’s valueOf(): 11
Using + operator: 12
Using String’s format(): 13
Using DecimalFormat: 132987654
Using DecimalFormat: 132,987,654
Using StringBuilder: 14
Binary: 1100
Octal: 14
Hex: c

That’s all about how to convert Long to String in java.

Related Posts

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.Date In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    Table of ContentsLocalDateTimeTimestampConvert LocalDateTime to Timestamp In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.