Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Long And String Datatype?
3.
Methods To Convert Long To String In Java
3.1.
Using the + operator
3.1.1.
Code:
3.1.1.1.
Output:
3.2.
Using String.valueOf() method
3.2.1.
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:
3.2.1.1.
Output: 
3.3.
Using Long.toString() method
3.3.1.
Syntax:
3.3.2.
Code: 
3.3.2.1.
Output: 
3.4.
Using String.format() method
3.4.1.
Code:
3.4.1.1.
Output:
3.4.2.
Code:
3.4.2.1.
Output: 
3.4.3.
Code: 
3.4.3.1.
Output:
3.5.
Using the new Long Method
3.5.1.
Syntax:
3.5.2.
Code:
3.5.2.1.
Output: 
3.6.
Using the String Builder Class
3.6.1.
Syntax:
3.6.2.
Code:
3.6.2.1.
Output:
3.6.3.
Code:
3.6.3.1.
Output: 
4.
The Need for Conversion
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: Mar 27, 2024
Easy

Converting Long to String in Java

Author Lokesh Sharma
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

When we are writing a program, we might come across a point where we will need to convert a value of a Long datatype to a String datatype. So, Java provides us with various simple methods to perform this operation. It is important to know this conversion because it is useful in many situations.

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 Datatype?

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!";

Also see, Swap Function in Java and Java Ioexception

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = "" + l;
    System.out.println(str); // prints "1234567890"
  }
}

 

Output:
1234567890

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:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.valueOf(l);
    System.out.println(str); // prints "1234567890"
  }
}

 

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:

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"
  }
}

 

Output:

499602d2

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);

 

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: 

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = Long.toString(l);
    System.out.println(str); // prints "1234567890"
  }
}

 

Output: 
1234567890

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:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = String.format("%d", l);
    System.out.println(str); // prints "1234567890"
  }
}

 

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:

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"
  }
}

 

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: 

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)
  }
}

 

Output:
1234567890 

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();

 

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:

public class Main {
  public static void main(String[] args) {
    long l = 1234567890;
    String str = new Long(l).toString();
    System.out.println(str); // prints "1234567890"
  }
}

 

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

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();

 

Consider the example below.

Code:

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"
  }
}
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:

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"
  }
}

 

Output: 
499602d2

The Need for Conversion

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 this article, we learned how to convert Long to String in Java. We understood how to use the + operator to convert long to string in java. Several other methods such as format(), new Long, toString() were covered.

We hope you enjoyed reading this article. If you wish to learn more about data types and return types in java, refer to data types and return types.

Recommended Readings:

Do check out The Interview guide for Product Based Companies as well as some of the Popular interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc.

Refer to our guided paths on Coding Ninjas Studio to learn more about Competitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Previous article
isDigit Java
Next article
Collections in Java
Live masterclass