How to convert long to String in java


In this core java tutorial we will learn how to convert long primitive type to String in java and convert long object to String with program and examples in java. We will  also understand  When to use convert String to long primitive type and when when to convert String to long object in java.




Programs to convert long to String in java -
  • Program/Example 1 to How to convert long primitive type to String  in java.
  • Program/Example 2 to How to convert long object to String in java


Program/Example 1 to How to convert long primitive type to String  in java.

String.valueOf(long) method converts long primitive type to String  in java.

package convertLongToStringPkg;
public class ConvertLongToStringExample1 {
   public static void main(String[] args) {
          long l = 12L;
          String string = String.valueOf(l);
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12

*/



Program/Example 2 to How to convert long object to String in java

toString() method of java.lang.Long class convert long object to String in java.

package convertLongToStringPkg;
public class ConvertLongObjectToStringExample2 {
   public static void main(String[] args) {
          Long longObject = 12L;
          String string = longObject.toString();
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12
*/


Summary -

In this core java tutorial we learned conversion of long primitive type to String in java and convert long object to String with program and examples in java. We also understood  when to use convert String to long primitive type and when when to convert String to long object in java.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Collection - List, Set and Map all properties in tabular form


How to convert String to long and long to String in java

How to check string contains special characters in Java


No comments:

Post a Comment