➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method In Java
Convert String To Long Java | Here, we will see how to convert string to long in Java. There are many ways to do this:-
- By using the parseLong() method of the Long class.
- By using the valueOf() method of the Long class.
- By using the constructor of the Long class.
For example:
1. String:- “123”
Long = 123
2.String = “abc”
Long = Invalid String
Convert String To Long Java using parseLong()
Let us see change string to long in Java using the parseLong() method. This parseLong() method is used to parse the given string as signed long. In the long class, the parseLong() method is defined as follows:-
public static long parseLong(String s) throws NumberFormatException
It returns the long represented by the argument in decimal. And throws NumberFormatException if the string does not contain a parsable long value.
public class Main {
public static void main(String args[]) {
String string = "12356789";
System.out.println("String = " + string);
long number = Long.parseLong(string);
System.out.println("Long = " + number);
}
}
Output:-
String = 12356789
Long = 12356789
Convert String To Long Java using valueOf()
Let us see how to convert string to long Java using the valueOf() method. There are two more variations of the valueOf() method but only the valueOf() method accepting string value will be helpful for us. The method details of the valueOf() method are as follows:-
public static Long valueOf(String s) throws NumberFormatException
public class Main{
public static void main(String args[]) {
String string = "32659874";
System.out.println("String = " + string);
long number = Long.valueOf(string);
System.out.println("Long = " + number);
}
}
Output:
String = 32659874
Long = 32659874
Java String To Long Using Constructor
We can also use constructor of Long class to convert String to Long value. the constructor of the Long class allows the string argument to create a new object of the long. The constructor is given as follows:-
public Long(String s) throws NumberFormatException
Note:- Similar to other wrapper classes, constructors are deprecated in Long class also since Java 9 version onwards, and will be removed in the upcoming versions. Therefore it is not recommended to use constructors, instead use valueOf() or parseLong() method to convert string to long value.
public class Main{
public static void main(String args[]) {
String string = "32659874";
System.out.println("String = " + string);
long number = new Long(string);
System.out.println("Long = " + number);
}
}
Output:-
String = 32659874
Long = 32659874
Example to Parse String To Long Java
The below code demonstrates valueOf() and parseLong() methods.
public class Main{
public static void main(String args[]) {
String string1 = "2223349494943933";
String string2 = "+45523349494943933";
String string3 = "-745523349494943933";
long val1 = Long.parseLong(string1);
long val2 = Long.parseLong(string2);
long val3 = Long.parseLong(string3);
System.out.println(string1 + " converted to long value =" + val1 );
System.out.println(string2 + " converted to long value =" + val2 );
System.out.println(string3 + " converted to long value =" + val3 );
long val4 = Long.valueOf(string1);
long val5 = Long.valueOf(string2);
long val6 = Long.valueOf(string3);
System.out.println("String : " + string1 + " long : " + val4);
System.out.println("String : " + string2 + " long : " + val5);
System.out.println("String : " + string3 + " long : " + val6);
}
}
Output:-
2223349494943933 converted to long value = 2223349494943933
+45523349494943933 converted to long value = 45523349494943933
-745523349494943933 converted to long value = -745523349494943933
String : 2223349494943933 long : 2223349494943933
String : +45523349494943933 long : 45523349494943933
String : -745523349494943933 long : -745523349494943933
Also see:- Check if a String is a Number Java
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!