In case you look Java code for valueOf() methodology from java.lang.String class, you’ll notice that it really calls the toString() methodology of java.lang.Lengthy class for conversion, which implies there is just one methodology the place the logic of changing primitive lengthy to String is written.
Find out how to convert lengthy to String in Java?
Listed below are the 3 ways to transform an extended primitive to String in Java :
- Utilizing Lengthy.toString() methodology
- Utilizing String.valueOf() methodology
- Utilizing String concatenation with empty String
You should use any of those strategies to transform an extended worth to a String in Java, however String.valueOf() is the really helpful method in Java.
Java Program to transform alongside worth to String in Java
Right here is our pattern Java program for this information sort conversion. On this program, you’ll study all these three strategies of changing primitive lengthy to String in Java.
/** * Java Program to transform lengthy to String in Java * * @writer java67 */ public class LongToStringInJava { public static void most important(String args[]) { // let's first create an extended variable for conversion lengthy massive = 3344344323434343L; // changing lengthy to String utilizing String concatenation // you simply want so as to add quantity with empty String // as proven beneath String s1 = "" + massive; System.out.println("String worth created from lengthy by concatenation : " + s1); // lengthy to String conversion utilizing Lengthy.toString() methodology String s2 = Lengthy.toString(massive); System.out.println("String to lengthy utilizing Lengthy.toString() : " + s2); // lengthy worth to String utilizing String.valueOf() String s3 = String.valueOf(massive); System.out.println("String to lengthy utilizing String.valueOf() : " + s3); } } Output String worth created from lengthy by concatenation : 3344344323434343 String to lengthy utilizing Lengthy.toString() : 3344344323434343 String to lengthy utilizing String.valueOf() : 3344344323434343
From the output, it is fairly clear that we had been capable of convert the large lengthy worth into String with none drawback. Each Lengthy.toString() and String.valueOf() methodology can also be capable of convert an extended worth with a constructive and unfavourable signal to corresponding String literal as proven beneath :
lengthy longWithPlusSign = +300L; String str1 = Lengthy.toString(longWithPlusSign); lengthy longWithMinusSign = -333L; String str2 = String.valueOf(longWithMinusSign); System.out.println("lengthy to String with plus signal : " + str1); System.out.println("lengthy to String with minus signal : " + str2); Output lengthy to String with plus signal : 300 lengthy to String with minus signal : -333
Issues to recollect
1) String.valueOf(lengthy) methodology internally calls Lengthy.toString() methodology.
2) In case you go an indication together with a quantity e.g. constructive or unfavourable signal then transformed String can even comprise that signal however just for unfavourable numbers, not for positives.
That is all about how one can convert lengthy values to String in Java. It is higher to make use of Lengthy.toString() methodology as a result of all different strategies internally name this one solely. If you know the way to transform int to String or double to String, you possibly can comply with the identical steps to transform different information varieties to String in Java as effectively.
In case you like this information sort conversion tutorial in Java, you may additionally get pleasure from following tutorials about changing String to different varieties e.g. Date, Enum, and so on
- Find out how to convert ByteBuffer to String in Java? (program)
- Find out how to convert String to Double in Java? (instance)
- Find out how to convert float to String in Java? (instance)
- Find out how to convert byte array to String in Java? (program)
- Find out how to convert Double to Lengthy in Java? (program)
- Find out how to convert String to Date in a thread-safe method? (instance)
- Find out how to convert String to int in Java? (instance)
- Find out how to convert Decimal to Binary in Java? (instance)
- Find out how to convert Enum to String in Java? (instance)