Java string to long
In this post, you will learn how to convert string to long using the Java programming language.
In the Java programming language, strings are objects that represent a sequence of characters. Java strings are created and manipulated through the string class. In Java, a string is immutable, which means its value cannot be changed once it is created.
The Java long keyword is a primitive data type. It is used to declare variables. It can hold a 64-bit two's complement integer. It is used when you need a higher range integer value.
These are the following ways to convert a string to a long value in Java.
- Long.parseLong() Method
- Long.valueOf() Method
- Long(String s) Constructor of Long class
Convert String to long using Long.parseLong(String) method
The Long.parseLong(String) method converts a string to long value. It accepts a string that can be parsed to a valid long value.
public class ConvertStrLong {
public static void main(String[] args)
{
String str1 = "234222";
String str2 = "-332333";
//Conversion using parseLong(String) method
long num1 = Long.parseLong(str1);
long num2 = Long.parseLong(str2);
System.out.println(num1);
System.out.println(num2);
}
}
Output of the above code:
234222
-332333
If you do not pass a valid string value that is parsable long, the Long.parseLong() method throws NumberFormatException.
import java.util.Scanner;
public class ConvertStrLong {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
// getting input from user
System.out.print("Enter a sentence: ");
String str = sc.nextLine();
long n = 0;
try {
//convert string to long
n = Long.parseLong(str);
} catch (NumberFormatException e) {
System.out.println("Not a valid long value.");
} catch (NullPointerException e) {
System.out.println("String is null.");
}
System.out.print("Long value : "+n);
}
}
Output of the above code:
Enter a sentence: 3424242344
Long value : 3424242344
Enter a sentence:
Not a valid long value.
Long value : 0
Enter a sentence: Null
Not a valid long value.
Long value : 0
Convert String to long using Long.valueOf() method
We can use the Long.valueOf() function to convert a string to long. Just like Long.parseLong() method, the function Long.valueOf() can also throws NullPointerException, if the string argument is null, or a NumberFormatException, if we do not pass a valid string value that is parsable long.
import java.util.Scanner;
public class ConvertStrLong {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
// getting input from user
System.out.print("Enter a sentence: ");
String str = sc.nextLine();
long n = 0;
try {
n = Long.valueOf(str);
} catch (NumberFormatException e) {
System.out.println("Not a valid long value.");
} catch (NullPointerException e) {
System.out.println("String is null.");
}
System.out.print("Long value : "+n);
}
}
Output of the above code:
Enter a sentence: 342342354
Long value : 342342354
Enter a sentence: -7677
Long value : -7677
Convert String to long using Long(String s) Constructor of Long class
We can use the Long class constructor to create new long object. As the new Long() constructor is depreciated. So, you may get warning when we are using this process.
import java.util.Scanner;
public class ConvertStrLong {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
// getting input from user
System.out.print("Enter a sentence: ");
String str = sc.nextLine();
long n = 0;
try {
n = new Long(str);
} catch (NumberFormatException e) {
System.out.println("Not a valid long value.");
} catch (NullPointerException e) {
System.out.println("String is null.");
}
System.out.print("Long value : "+n);
}
}
Output of the above code:
Enter a sentence: 23443243
Long value : 23443243
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number