How to convert String to Long or Long to String in java with examples
- Admin
- Sep 25, 2023
- Java-convert Java
Long, String objects conversion in java
Long
is an object which holds larger values.
It is a wrapper class in java for primitive type long
.
if you initialize long values, you need to add l
to its value.
The string
is a class to represent a group of characters enclosed in double-quotes.
if the string
contains non-numeric characters, converting these non-numeric values to long or any numeric data type results in NumberFormatException.
When data is retrieved from the database, the application should display the data either from long to string or string to long.
These are all common requirements in developer coding tasks to cast different java objects.
Both are classes defined in java.lang package.
First, Let’s see 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 the toString()
method that returns the 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
The string class has many static methods. valueOf() is one overloaded method. It returns the 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 uses to format a long value and convert it into a 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
, or double
can be converted to a string
using the plus operator.
The process just creates a string object with an append empty string and numeric value.
Long longObject = 7845l;
String longString = "" + longObject;
System.out.println(longString);
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, NumericFormatException will be thrown. character l is also not allowed in the string. only numeric characters allowed
Long longObject=new Long("123");
System.out.println(longObject);
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);
System.out.println(longValue);
String str1 = "1234as";
Long longValue1 = Long.parseLong(str1);
System.out.println(longValue1);
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 a numeric value, and returns a Long object, not primitive type long.
String str = "897";
Long longValue = Long.valueOf(str);
System.out.println(longValue);
String str1 = "87as";
Long longValue1 = Long.valueOf(str1);
System.out.println(longValue1);
That’s on casting long to string or string too long. Please like or share my blog on Facebook/Twitter and also subscribe for the latest posts.