Monday, August 13, 2018

How to convert String to Long or Long to String in java with examples

Long, String objects

Convert Long to string viceverse in java with examples

Long is an object which holds larger values. It is a wrapper class for primitive type long. if you are initializing long values, you need to add l to its value.
The string is a class to represent a group of characters enclosed in a double quotes. if the string contains non-numeric characters, converting this non-numeric values to long or any numeric data type results in NumberFormatException.

When data retrieved from Database, the application should display the data either in from long to string or string to long. These are all common requirement in developer coding tasks to cast different java objects.

Both are classes defined in java.lang package.


Different ways we can create a long or string object
Long l = 78l;
Long l1 = new Long(3);
String s=new String("23");
String s1="21";
This blog post is about how we can convert Long to String or vice versa.

How to convert Long to String in Java

There are many ways to convert Long to String in Java.
Let us discuss different ways

Using toString() method 

Every class or object has toString() method. Long class also has toString() method which returns string form of long primitive value we can do in two ways using toString method Long.toString(longvalue) method longobject.toString() method
Long l = 1234l;
System.out.println(l.toString()); // outputs 1234
System.out.println(Long.toString(l)); // outputs 1234

using String.valueOf() method 

String class has many static methods. valueOf() is one overloaeded method. It returns value of a long value. T valueOf method has different versions for taking parameters as double, integer and Long pubic static String valueOf(Object value) Object can be of any type
Long longValue=782l;
System.out.println(String.valueOf(longValue)); //outputs 782

Using DecimalFormat class 

java.text.DecimalFormat class can also be used to format a long value and converted into String. format
Long longObject = 78945l;
DecimalFormat df = new DecimalFormat("#");
System.out.println( df.format(longObject));
output is 78945
Using empty String and append
Any numeric value like integer, long, double can be converted to a string using plus operator The process just creates a string object with append empty string and numeric value.
Long longObject = 7845l;
String longString = "" + longObject;

How to Convert String to Long in java?

There are many simple ways to convert it

Using Long constructor 

Every Numeric object has a constructor with a string parameter. if a string with non-numeric characters is passed, NumbericFormatException will be thrown. character l is also not allowed in the string. only numeric characters allowed
Long longObject=new Long("123");

using Long.parseLong() method

 The long method has many static methods, parseLong is one of the static methods public static long parseLong(String s) throws NumberFormatException
String str = "1234";
Long longValue = Long.parseLong(str);
String str1 = "1234as";
Long longValue1 = Long.parseLong(str1);
output is 1234 java.lang.NumberFormatException: For input string: "1234as" "1234as" string is unable to convert to long and thrown NumberFormatException.

using Long.valueOf(String) method

The long object has a valueOf method, returns numeric value and returns a Long object, not primitive type long
String str = "897";
Long longValue = Long.valueOf(str);
String str1 = "87as";
Long longValue1 = Long.valueOf(str1);
That's on casting long to string or string to long. Please like or share my blog on facebook/twitter and also subscribe for latest posts.

Related article