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.

Output

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.

Output

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-

We can also write as: 

or

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.

Output