Java – Convert String to Long

You can typecast or convert a String to Long in Java in many ways. Some of them are using Long.parseLong(), Long.valueOf(), new Long().

In this tutorial, we shall learn some of the ways of how to convert a string value to a long value with examples.

1. Convert string to long using Long.parseLong()

Long.parseLong(str) parses any parsable long value from string to long value.

In this example, we shall use Long.parseLong() method and pass a string that can be parsed to a valid long value.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Long
 */

public class StringToLong {

	public static void main(String[] args) {
		//a string
		String str = "18965426354";
		
		//convert string to long
		long n = Long.parseLong(str);
		
		System.out.print(n);
	}
}

Run the above program and the String is converted to Long.

18965426354

If you do not provide a valid string that is parsable long, Long.parseLong() throws NumberFormatException.

In the following example program, we shall take a string which does not contain a valid long value.

Some of the scenarios that could throw this error are:

  • If the string contains invalid characters that does not parse to a long value. Like decimal point, alphabets, etc.
  • A number that is out of range for a long value. Any value that is outside the range [-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807] will make parseLong() to throw this error.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Long
 */

public class StringToLong {

	public static void main(String[] args) {
		//a string
		String str = "5.354"; //"as 9w0", "1as52"
		
		//convert string to long
		long n = Long.parseLong(str);
		
		System.out.print(n);
	}
}

Run the above program and parseLong() throws NumberFormatException.

Exception in thread "main" java.lang.NumberFormatException: For input string: "5.354"
	at java.base/java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.base/java.lang.Long.parseLong(Unknown Source)
	at java.base/java.lang.Long.parseLong(Unknown Source)
	at StringToLong.main(StringToLong.java:12)

Also, if null is passed to Long.parseLong(), the function throws NullPointerException.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Long
 */

public class StringToLong {

	public static void main(String[] args) {
		//a string
		String str = null;
		
		//convert string to long
		long n = Long.parseLong(str);
		
		System.out.print(n);
	}
}

Run the above program and parseLong() throws NullPointerException.

Exception in thread "main" java.lang.NumberFormatException: null
	at java.base/java.lang.Long.parseLong(Unknown Source)
	at java.base/java.lang.Long.parseLong(Unknown Source)
	at StringToLong.main(StringToLong.java:12)

As Long.parseLong() can throw NullPointerException or NumberFormatException, it is a good practice to surround the function parseLong() with Java Try Catch and handle the exceptions accordingly.

</>
Copy
/**
 * Java Program - Convert String to Long
 */

public class StringToLong {

	public static void main(String[] args) {
		//a string
		String str = "85612536";
		
		long n = 0;
 
		try {
			//convert string to long
			n = Long.parseLong(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid long value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(n);
	}
}

2. Convert string to long using Long.valueOf()

You can also use Long.valueOf() function to convert a string to long.

In the following example, we shall use the method valueOf() to get long value from string.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Long
 */

public class StringToLong {

	public static void main(String[] args) {
		//a string
		String str = "-632916325";
		
		long n = 0;

		//convert string to long
		try {
			n = Long.valueOf(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid long value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(n);
	}
}

Just like Long.parseLong(), the function Long.valueOf() also throws NullPointerException if the string argument is null, or a NumberFormatException if the string does not parse to a valid long value.

3. Convert string to long using Long() constructor

Note: new Long() constructor is depreciated. So, you may get warning when you are following this process. Long.valueOf() is recommended in the place of new Long().

You can also use the constructor of Long class, to convert a string to long.

In the following example, we shall use the constructor of Long class to convert from string to long.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Long
 */

public class StringToLong {

	public static void main(String[] args) {

		try {
			Long f = new Long("9223372036854775807");
			System.out.print(f);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid long value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
	}
}

Conclusion

In this Java Tutorial, we learned how to Convert a String to Long value in Java using Long.parseLong() and Long.valueOf() methods.