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.

πŸ’» Awesome Tech Resources:
  • Looking for βš’οΈ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events πŸ—“οΈ Calendar.️
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 Date to LocalDate in java
    12 January

    Java Date to LocalDate

    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 also: Convert LocalDate to Date in java πŸ’» Awesome Tech Resources: Looking for βš’οΈ […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to 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. πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go to our job portal. Looking for tech events? Go […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    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; πŸ’» Awesome Tech Resources: Looking for […]

  • Java Stream List to Map
    26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go […]

  • Convert LocalDateTime to Date in Java
    15 May

    Java LocalDateTime to Date

    In this post, we will see how to convert LocalDateTime 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. πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go to our job portal. Looking for tech events? Go […]

  • 20 October

    Java Array to Set

    In this post, we will learn java array to set conversion. There are many ways to convert array to set. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using this method. πŸ’» Awesome Tech Resources: Looking for βš’οΈ tech jobs? Go to our job portal. Looking for tech events? […]

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.