String to long in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

Generally, the data received from a text field or another file is in the form of a string. In order to perform mathematical operations on the string containing numbers, we need to convert strings to long or int values. This can be done using different in-built methods in Java.

How to Convert string to long in Java?

The long data type is usually used when the numerical values are greater than the range of values provided by int. The default value of a long variable is 0. We can convert a string to long in Java using different methods. In this article, we will learn how to convert using the following methods:

  1. Using Long.parseLong
  2. Using Long.valueOf(String)
  3. Using the constructor of the Long Class
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Method 1 - Convert String to long using Long.parseLong()

Long.parseLong() takes a string as its input. All characters in the string must be digits except the first character. The first character could be either a digit or a minus sign ("-"). It does not even allow numbers with decimals in the string.

If the string contains anything except digits (or a minus sign at the beginning), the compiler raises a NumberFormatException.

Syntax:

Here str is a string of digits.

For example:

Output:

In the above example, we used the parseLong() function to convert a string composed of digits into a Long.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Method 2 - Convert String to long using Long.valueOf(String)

The valueOf() method also converts string to Long. Like the parseLong() method, this method also needs a string of digits as its input and it allows a minus sign at the start of the string. If the string contains anything except digits (or a minus sign at the beginning), the compiler raises a NumberFormatException.

Syntax:

For example:

Output:

In the above example, we used the valueOf() method to convert a string of digits to Long.

Method 3 - Convert String to long using the constructor of Long class

The Long class in Java contains a constructor that allows us to convert a string to Long by creating a Long object. The new Long object created by the constructor has the equivalent value of the string containing digits.

The string that is passed as the input, must be composed only of digits and/or a minus sign in the beginning. If the string contains anything else, a NumberFormatException exception will be raised.

For example:

Output:

In the above example, we used a constructor of the Long class to convert a string of digits to a Long value.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • We can convert a string to long in Java using three different methods - Long.parseLong(), Long.valueOf(), and the constructor of the Long class.
  • These methods can only convert those strings that contain positive or negative integer numbers.
  • If the string that will be converted to long contains anything except digits (or a minus sign at the beginning), the compiler throws a NumberFormatException exception.