To convert a long
value to a String
in Java, you can use the Long.toString()
method. Here’s an example:
long num = 123456789; String str = Long.toString(num); System.out.println(str); // prints "123456789"
Alternatively, you can also use the String.valueOf()
method to convert a long
to a String
:
long num = 123456789; String str = String.valueOf(num); System.out.println(str); // prints "123456789"
Both of these methods will convert a long
value to a String
representation.
1) String.valueOf():
String.valueOf()
is a method in Java that returns a String
representation of the given argument. It is an overloaded method, which means it can take different types of arguments and return their respective String
representations.
Here are a few examples of how String.valueOf()
can be used:
// Convert an int to a String int num1 = 123; String str1 = String.valueOf(num1); // str1 = "123" // Convert a double to a String double num2 = 3.14; String str2 = String.valueOf(num2); // str2 = "3.14" // Convert a boolean to a String boolean flag = true; String str3 = String.valueOf(flag); // str3 = "true"
In all of these examples, String.valueOf()
is used to convert a value to its String
representation. Note that for primitive types like int
, double
, and boolean
, the method returns the equivalent String
representation of the value. For objects, String.valueOf()
calls the object’s toString()
method to get its String
representation.
Java long to String Example using String.valueOf():
Sure, here’s an example of how to convert a long
value to a String
using String.valueOf()
method:
long num = 123456789; String str = String.valueOf(num); System.out.println(str); // prints "123456789"
In this example, the long
value 123456789
is converted to a String
using String.valueOf()
method and assigned to the str
variable. The println()
method is then used to print the str
variable, which outputs the String
representation of the long
value as "123456789"
.
2) Long.toString():
Long.toString()
is a method in Java that returns a String
representation of a long
value. It is a static method of the Long
class, which means it can be called directly on the class without needing to create an instance of it.
Here’s an example of how to use Long.toString()
to convert a long
value to a String
:
long num = 123456789; String str = Long.toString(num); System.out.println(str); // prints "123456789"
In this example, the long
value 123456789
is converted to a String
using Long.toString()
method and assigned to the str
variable. The println()
method is then used to print the str
variable, which outputs the String
representation of the long
value as "123456789"
.
Long.toString()
also has an overloaded version that allows you to specify the radix (base) of the resulting String
. For example, if you want to convert a long
value to its hexadecimal representation, you can call Long.toString(num, 16)
:
long num = 255; String hex = Long.toString(num, 16); System.out.println(hex); // prints "ff"
In this example, the long
value 255
is converted to its hexadecimal representation using Long.toString(num, 16)
method and assigned to the hex
variable. The println()
method is then used to print the hex
variable, which outputs the String
representation of the long
value in hexadecimal as "ff"
.
Java long to String Example using Long.toString():
Here’s an example of how to convert a long
value to a String
using Long.toString()
method:
long num = 123456789; String str = Long.toString(num); System.out.println(str); // prints "123456789"
In this example, the long
value 123456789
is converted to a String
using Long.toString()
method and assigned to the str
variable. The println()
method is then used to print the str
variable, which outputs the String
representation of the long
value as "123456789"
.
If you want to convert a long
value to its hexadecimal representation, you can call Long.toString()
with a radix (base) of 16:
long num = 255; String hex = Long.toString(num, 16); System.out.println(hex); // prints "ff"
In this example, the long
value 255
is converted to its hexadecimal representation using Long.toString(num, 16)
method and assigned to the hex
variable. The println()
method is then used to print the hex
variable, which outputs the String
representation of the long
value in hexadecimal as "ff"
.