HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Convert String to Long in Java

Convert String to Long in Java

3 Java examples to convert String to long value using Long.parseLong(String), Long.valueOf(String) and new Long(String) constructor.

String number = "2018";			//String
		
long value1 = Long.parseLong( number );	//long - method 1

long value2 = Long.valueOf( number );	//long - method 2

long value3 = new Long( number );	//long - method 3

1. Long.valueOf(String) static method

  1. Long.valueOf(String) method parses the string argument as a signed decimal long.
  2. The characters in the string must all be decimal digits, except that the first character may be a minus (-) sign for negative numbers and plus(+) sign for positive numbers.
  3. The result long value is exactly same as string argument in base 10.

1.1. String to long conversion example

In given example, we will convert one positive number and one negative number to long value.

// Positive long

String positiveNumber = "+12001";
long value1 = Long.valueOf(positiveNumber);

// Negative long

String negativeNumber = "-22002";
long value2 = Long.valueOf(negativeNumber);

System.out.println(value1);			//12001
System.out.println(value2);			//-22002

2. Long.parseLong(String) static method

Rules for Long.parseLong(String) method are similar to Long.valueOf(String) method as well.

  1. Long.parseLong(String) method parses the string argument as a signed decimal long.
  2. The characters in the string must all be decimal digits, except that the first character may be a minus (-) sign for negative numbers and plus(+) sign for positive numbers.
  3. The result long value is exactly same as string argument in base 10.

2.1. String to long conversion example

Again, we will convert one positive number and one negative number to long value using Long.parseLong(String) static method.

// Positive long

String positiveNumber = "+1000";
long value1 = Long.parseLong(positiveNumber);

// Negative long

String negativeNumber = "-2000";
long value2 = Long.parseLong(negativeNumber);

System.out.println(value1);			//1000
System.out.println(value2);			//-2000

3. new Long(String) Constructor

Another useful way is to utilize Long class constructor to create new long object. Using unboxing feature, concert long object to long primitive.

// Positive long

String positiveNumber = "+1000";
long value1 = new Long(positiveNumber);

// Negative long

String negativeNumber = "-2000";
long value2 = new Long(negativeNumber);

System.out.println(value1);			//1000
System.out.println(value2);			//-2000

4. NumberFormatException Error

If the String argument does not have only decimal characters (except first character which can be plus or minus sign), you will get NumberFormatException error in runtime.

String number = "12001xyz";

long value = Long.parseLong(number);

//Error

Exception in thread "main" java.lang.NumberFormatException: For input string: "12001xyz"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:589)
	at java.lang.Long.<init>(Long.java:965)
	at com.howtodoinjava.StringExample.main(StringExample.java:9)

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces