There are 3 primary ways to convert a long value to String inwards Java e.g. past times using Long.toString(long value) method, past times using String.valueOf(long) and past times concatenating alongside an empty String. You tin plough over the sack role whatever of these method to convert a long information type into String object. It's non real dissimilar than from how y'all convert an int to String inwards Java. Same method applies hither equally well. String concatenation seems the easiest way of converting a long variable to String, but others are also convenient. If y'all await Java code for valueOf() method from java.lang.String bird y'all volition realize that it genuinely calls the toString() method of java.lang.Long bird for conversion, which agency at that spot is exclusively 1 method where logic of converting primitive long to String is written. Nevertheless, if y'all are non converting long inwards loop all method volition perform same, but if y'all convey to convert large give away of long values to String as well as then straight using Long.toString() method tin plough over the sack endure quite useful.
Sample Program :
Here is our sample Java computer program for this information type conversion. In this computer program y'all volition larn all these 3 methods of converting primitive long to String inwards Java.
From the output its quite clear that nosotros were able to convert large long value into String without whatever problem. Both Long.toString() as well as String.valueOf() method is likewise able to convert a long value alongside positive as well as negative sign to corresponding String literal equally shown below :
Things to remember
1) String.valueOf(long) method internally calls Long.toString() method.
2) If y'all top sign along alongside give away e.g. positive or negative sign as well as then converted String volition likewise comprise that sign but exclusively for negative numbers, non for positives.
That's all most how to convert long values to String inwards Java. It's improve to role Long.toString() method because all other method internally calls this 1 only. If y'all know how to convert int to String or double to String, y'all tin plough over the sack follow same steps to convert other information types to String inwards Java equally well.
If y'all similar this information type conversion tutorial inwards Java, y'all may likewise relish next tutorials most converting String to other types e.g. Date, Enum etc
How to convert long to String inwards Java?
Here are the 3 ways to convert a long primitive to String inwards Java :- Using Long.toString() method
- Using String.valueOf() method
- Using String concatenation alongside empty String
Sample Program :
Here is our sample Java computer program for this information type conversion. In this computer program y'all volition larn all these 3 methods of converting primitive long to String inwards Java.
/** * Java Program to convert long to String inwards Java * * @author java67 */ public class LongToStringInJava { public static void main(String args[]) { // let's kickoff practise a long variable for conversion long large = 3344344323434343L; // converting long to String using String concatenation // y'all simply require to add together give away alongside empty String // equally shown below String s1 = "" + big; System.out.println("String value created from long past times concatenation : " + s1); // long to String conversion using Long.toString() method String s2 = Long.toString(big); System.out.println("String to long using Long.toString() : " + s2); // long value to String using String.valueOf() String s3 = String.valueOf(big); System.out.println("String to long using String.valueOf() : " + s3); } } Output String value created from long past times concatenation : 3344344323434343 String to long using Long.toString() : 3344344323434343 String to long using String.valueOf() : 3344344323434343
From the output its quite clear that nosotros were able to convert large long value into String without whatever problem. Both Long.toString() as well as String.valueOf() method is likewise able to convert a long value alongside positive as well as negative sign to corresponding String literal equally shown below :
long longWithPlusSign = +300L; String str1 = Long.toString(longWithPlusSign); long longWithMinusSign = -333L; String str2 = String.valueOf(longWithMinusSign); System.out.println("long to String alongside summation sign : " + str1); System.out.println("long to String alongside minus sign : " + str2); Output long to String alongside summation sign : 300 long to String alongside minus sign : -333
Things to remember
1) String.valueOf(long) method internally calls Long.toString() method.
2) If y'all top sign along alongside give away e.g. positive or negative sign as well as then converted String volition likewise comprise that sign but exclusively for negative numbers, non for positives.
That's all most how to convert long values to String inwards Java. It's improve to role Long.toString() method because all other method internally calls this 1 only. If y'all know how to convert int to String or double to String, y'all tin plough over the sack follow same steps to convert other information types to String inwards Java equally well.
If y'all similar this information type conversion tutorial inwards Java, y'all may likewise relish next tutorials most converting String to other types e.g. Date, Enum etc
- How to convert ByteBuffer to String inwards Java? (program)
- How to convert String to Double inwards Java? (example)
- How to convert float to String inwards Java? (example)
- How to convert byte array to String inwards Java? (program)
- How to convert Double to Long inwards Java? (program)
- How to convert String to Date inwards thread-safe manner? (example)
- How to convert String to int inwards Java? (example)
- How to convert Decimal to Binary inwards Java? (example)
- How to convert Enum to String inwards Java? (example)
0 Response to "How to convert long to String inwards Java? Example"
Posting Komentar