In Java, we often need to convert a String to a long value and vice versa. A String is a sequence of characters, while long is a primitive data type used to store 64-bit integer values. If a String contains a number, we may need to convert it to a long value for calculations or other operations.
Java provides simple methods to convert a String to a long, such as Long.parseLong() and Long.valueOf(). These methods read the numeric value from the String and convert it into a long value.

It is generally used if we have to perform mathematical operations on the string that contains long number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is long, we need to convert string to long.
The Long.parseLong() method is a basic utility that the Long class provides for converting a string representation of a numeric value into its corresponding matching long primitive data type. This approach is mostly applied where user input, configuration files, and, generally speaking, any situation where numbers are transferred or stored as strings.
The parselong () method takes a string argument which is representing a numeric value and returns the equivalent long value. It does this by disassembling the characters of the string and translating them into a numeric value, complying with the rules of Java. The signature of the parseLong() method is given below:
Let's see the simple example of converting a String to a long.
StringToLongExample.java
Output:
Converted long value: 123456789012345
The Long.valueOf() method is a way to conveniently convert a string representation to a Long object of a long primitive data type. This approach is the part of the Long class that is used extensively, and it is convenient in the process of string to long conversions in Java applications. The valueOf() method takes a string argument containing a numeric value and return a Long object that represents this value. On the other hand, it can also return a long primitive data type for any specific purpose.
The signature of the valueOf() method is given below:
Let's see the simple example of converting a String to a long.
StringToLongExample.java
Output:
Converted long value: 123456789012345
The long.decode() method converts a string of numeric value to a Long object or a long primitive data type. This technique belongs to the Long class, which gives string-to-long conversions much flexibility and convenience in Java applications.
decode() accepts a string with a numeric value optionally prefixed with a radix indicator, such as 0x for hexadecimal or 0 for octal notation. It then returns a Long object that will represent the string's value. When needed, it can also return a long primitive data type.
The signature of the decode() method is given below:
Let's see the simple example of converting a String to a long.
Output:
Converted long value: 123456789012345
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India