Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, November 15, 2021

Java String to Long - How to Parse String Into Long?

1. Overview

In this tutorial, We'll learn how to convert String to Long in java with examples programs.

This problem can be solved in many ways but we will look at the most commonly used in real-time applications.

Sometimes you get the number in string format. So, you need to parse the string to long value to perform the operations on the long number.


Let us look at each approach and understand its usage.
Java String to Long - How to Parse String Long?


2. Java Parse String to Long using Long.parseLong()


Long.parseLong() method takes the string object and parses the string into the long value.

This method works for positive and negative values.

The first character of the string is used for the sign of the number either plus(+) or minus(-) but this is optional.

Look at the below example program.
package com.javaprogramto.programs.strings.tolong;

public class StringToLongExample {

	public static void main(String[] args) {

		String positiveString = "12345";

		long longvalue1 = Long.parseLong(positiveString);
		System.out.println("Positive Long value 1 : " + longvalue1);

		String positiveString2 = "+12345";

		long longvalue2 = Long.parseLong(positiveString2);
		System.out.println("Positive Long value 2 : " + longvalue2);
		
		String negativeValue1 = "-12345";

		long longvalue3 = Long.parseLong(negativeValue1);
		System.out.println("Negative Long value 3 : " + longvalue3);

	}

}

Output:
Positive Long value 1 : 12345
Positive Long value 2 : 12345
Negative Long value 3 : -12345

3. Java Convert String to Long using Long.valueOf()


Long.valueOf() method is also from the Long class and it is a static method.

Long.valueOf() method takes the string value and converts it into the Long wrapper object. But whereas parseLong() method converts into primitive long value.

parseLong() method rules applies to valueOf() method.

Look at the below example.

package com.javaprogramto.programs.strings.tolong;

public class StringToLongExample {

	public static void main(String[] args) {

		String positiveString = "12345";

		long longvalue1 = Long.parseLong(positiveString);
		System.out.println("Positive Long value 1 : " + longvalue1);

		String positiveString2 = "+12345";

		long longvalue2 = Long.parseLong(positiveString2);
		System.out.println("Positive Long value 2 : " + longvalue2);
		
		String negativeValue1 = "-12345";

		long longvalue3 = Long.parseLong(negativeValue1);
		System.out.println("Negative Long value 3 : " + longvalue3);

	}

}

This program also produces the same output.


4. Java Convert String to Long using Long Constructor


Last way is using new Long(String) constructor.  But this way is deprecated.

package com.javaprogramto.programs.strings.tolong;

public class StringToLongExample3 {

	public static void main(String[] args) {

		String positiveString = "12345";

		Long longvalue1 = new Long(positiveString);
		System.out.println("Positive Long value 1 : " + longvalue1);

		String positiveString2 = "+12345";

		Long longvalue2 = new Long(positiveString2);
		System.out.println("Positive Long value 2 : " + longvalue2);
		
		String negativeValue1 = "-12345";

		Long longvalue3 = new Long(negativeValue1);
		System.out.println("Negative Long value 3 : " + longvalue3);

	}

}


5. Java String to Long - String is with non-digits


What happens if the string is having non-digits in it?

All of the above approaches will produce the runtime exception "NumberFormatException".

package com.javaprogramto.programs.strings.tolong;

public class StringToLongExample4 {

	public static void main(String[] args) {

		long one = Long.parseLong("abc133");
		
		Long two = Long.valueOf("abc133");
		
		Long three = new Long("abc133");
		
		
	}

}

The program fails at the runtime because string is having alphabets that are other than numbers.
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc133"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
	at java.base/java.lang.Long.parseLong(Long.java:707)
	at java.base/java.lang.Long.parseLong(Long.java:832)
	at com.javaprogramto.programs.strings.tolong.StringToLongExample4.main(StringToLongExample4.java:7)

6. Conclusion


In this article, we have seen how to parse String to Long in java and what are the possible error if the string is with non digits values.

But among all of these 3 ways, Long,parseLong() method is suggested to use because valueOf() and Long constructor uses internally parseLong() method. 





No comments:

Post a Comment

Please do not add any spam links in the comments section.