Methods To Convert Long To String In Java
Let us now look at the various methods to convert a long to string in java.
- Using + operator
- Using String.valueOf()
- Using Long.toString()
- new Long(long l)
- Using String.format()
- Using StringBuilder, StringBuffer object
1. 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 Example:
public class Main {
public static void main(String[] args) {
long l = 1234567890;
String str = "" + l;
System.out.println(str); // prints "1234567890"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
1234567890
2. 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 Example:
public class Main {
public static void main(String[] args) {
long l = 1234567890;
String str = String.valueOf(l);
System.out.println(str); // prints "1234567890"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
1234567890
You can also use the valueOf() method to convert a long value to a hexadecimal String by specifying the radix (base) as 16:
Code Example:
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"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
499602d2
3. 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);
You can also try this code with Online Java Compiler
Run CodeHere'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 Example:
public class Main {
public static void main(String[] args) {
long l = 1234567890;
String str = Long.toString(l);
System.out.println(str); // prints "1234567890"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
1234567890
4. 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 Example:
public class Main {
public static void main(String[] args) {
long l = 1234567890;
String str = String.format("%d", l);
System.out.println(str); // prints "1234567890"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
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 Example:
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"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
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 Example:
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)
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
1234567890
5. 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();
You can also try this code with Online Java Compiler
Run CodeHere'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 Example:
public class Main {
public static void main(String[] args) {
long l = 1234567890;
String str = new Long(l).toString();
System.out.println(str); // prints "1234567890"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
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.
6. 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();
You can also try this code with Online Java Compiler
Run CodeConsider the example below.
Code Example:
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"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
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 Example:
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"
}
}
You can also try this code with Online Java Compiler
Run CodeOutput:
499602d2
The Need for Conversion for Long to String in Java
There can be several reasons why you might need to convert a long value to a String in Java:
- 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.
- 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.
- 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.
- 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 conclusion, converting a long to a String in Java is a simple yet essential operation. We explored methods like the + operator, String.valueOf(), Long.toString(), and more, each tailored to specific needs. Mastering these techniques enhances your Java programming efficiency. To deepen your understanding of Java's versatile features, explore our recommended readings and guided paths for coding and interview preparation.
Recommended Readings: