How to Convert String to long in Java
How to Convert String to long in java
It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String to long is necessary. When you get the data from the text field, it received as a string. You can convert String to long in three ways:
- Using Long.parseLong() method
- Using Long.valueOf() method
- Using the constructor of Long class
Using Long.parseLong() method
The Long.parseLong() is a built-in, static method of java.lang package. It parses the String argument as a signed decimal long and returns a long value. The Syntax of the method is-
public static long parseLong(String s)
The parameter s will be converted to a primitive long value. It throws a NumberFormatException if the parameter is not a valid long. It is the most common method to convert String to long.
Remember
- The first character must be a digit or a minus sign (-).
- The value it returns is not an instance of the Long class.
Valid Conversion
- String number=”-10123”;
- String number=”10123”;
Invalid Conversion
- String number=”10123a”;
- String number=”a10123”;
- String number=”abcde”;
If you are declaring a String as above, Long.parseLong () throws “NumberFormatException,” because it is not a valid conversion.
Example
In this example, we have taken a String num having value “10000”. val is a variable of type long having value 100001. rslt is a variable that stores the value of num after conversion. Long is a class under java.lang package. parseLong() is a static method parses a String num as an argument. Now the string has been converted into long type, So we can perform mathematical operation. The first println statement concatenates two variables i.e. num and val. In the second println statement we are performing addition operation between rslt and val variable.
public class StringToLongExample { public static void main(String[] args) { String num = "10000"; long val=100001; long rslt = Long.parseLong(num); //Conversion using parseLong() method System.out.println("Before Conversion: " +num+val); //concatenation of num and val System.out.println("After Conversion:" +(rslt+val)); //performing addition } }
Output
Before Converion:10000100001 After Conversion: 110001
Using Long.valueOf() method
Its working is similar to Long.parseLong() method. Long.valueOf() is a built-in and static method in java.lang package. It returns a Long object holding the value of the specified String. The value it returns is an instance of the Long class. The signature of the method is-
public static Long.valueOf(String s)
Example
Consider the following example, here we have taken two variables of type String str1 and str2 having value “11111” and “22222” respectively. num1 and num2 are two variables of type long store the value of str1 and str2 after conversion. The first println statement concatenates the two strings str1 and str2. Now we have the converted value of String, so we can perform addition. The second println statement prints the addition of num1 and num2 variable.
public class StringToLongExample1 { public static void main(String[] args) { String str1 = "11111"; String str2 = "22222"; long num1 = Long.valueOf(str1); //Conversion using valueOf(String) method long num2 = Long.valueOf(str2); System.out.println("Before Conversion: " +str1+str2); //concatenation of Str1 and str2 System.out.println("After Conversion: " +(num1+num2)); //performing addition } }
Output
Before Conversion: 1111122222 After Conversion: 33333
Using the constructor of Long class
Another method to convert String to long is; to create the constructor of Long class and then invoke its longValue() method. It has a constructor that allows String argument and creates a new Long object. We can create constructor in the following forms-
String avg = "11111"; Long longObject = new Long(avg); long number = longObject.longValue();
We can also write as:
String avg = "11111"; long number = new Long(avg).longValue();
or
long number = new Long("11111").longValue();
Example
Consider the following example; we have taken two variables val1 and val2 of type String having values “10000” and “22222” respectively. num1 and num2 are two variables of type long which stores the long value of val1 and val2. new is a keyword that creates the object of Long class and having arguments val1 and val2 passing to it. The first println statement prints the concatenation of two strings val1 and val2. The second println statement prints the addition of variable num1 and num2.
public class StringToLongExample2 { public static void main(String[] args) { String val1 = "10000"; String val2 = "22222"; long num1 = new Long(val1); //Conversion using Long(String s) constructor long num2 = new Long(val2); System.out.println("Before Conversion: " +val1+val2); //concatenation of num1 and num2 System.out.println("After Conversion: " +(num1+num2)); //performing addition } }
Output
Before Conversion: 1000022222 After Conversion: 32222