Wednesday, December 11, 2019

How To Convert Long To String Inwards Java? Example

There are iii master copy ways to convert a long value to String inward 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 purpose whatsoever 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 inward Java. Same method applies hither every bit 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 course of teaching y'all volition realize that it genuinely calls the toString() method of java.lang.Long course of teaching for conversion, which agency at that spot is alone i method where logic of converting primitive long to String is written. Nevertheless, if y'all are non converting long inward loop all method volition perform same, but if y'all accept to convert large release of long values to String thus straight using Long.toString() method tin hold upwardly quite useful. 



How to convert long to String inward Java? 

Here are the iii ways to convert a long primitive to String inward Java :
  • Using Long.toString() method 
  • Using String.valueOf() method
  • Using String concatenation alongside empty String



Sample Program :
Here is our sample Java programme for this information type conversion. In this programme y'all volition larn all these iii methods of converting primitive long to String inward Java.

/**  * Java Program to convert long to String inward Java  *   * @author java67  */  public class LongToStringInJava {      public static void main(String args[]) {          // let's start practise a long variable for conversion         long large = 3344344323434343L;          // converting long to String using String concatenation         // y'all simply take away to add together release alongside empty String         // every bit 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 whatsoever problem. Both Long.toString() as well as String.valueOf() method is every bit good able to convert a long value alongside positive as well as negative sign to corresponding String literal every bit 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 addition sign : " + str1); System.out.println("long to String alongside minus sign : " + str2);  Output long to String alongside addition 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 transcend sign along alongside release e.g. positive or negative sign thus converted String volition every bit good comprise that sign but alone for negative numbers, non for positives.
 There are iii master copy ways to convert a long value to String inward Java e How to convert long to String inward Java? Example


That's all nearly how to convert long values to String inward Java. It's ameliorate to purpose Long.toString() method because all other method internally calls this i only. If y'all know how to convert int to String or double to String, y'all tin follow same steps to convert other information types to String inward Java every bit well.

Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
program)
  • How to convert String to Double inward Java? (example)
  • How to convert float to String inward Java? (example)
  • How to convert byte array to String inward Java? (program)
  • How to convert Double to Long inward Java? (program)
  • How to convert String to Date inward thread-safe manner? (example)
  • How to convert String to int inward Java? (example)
  • How to convert Decimal to Binary inward Java? (example)
  • How to convert Enum to String inward Java? (example)

  • No comments:

    Post a Comment