JavaDevNotes.com

Java String To Long Examples

This post will show examples on how to convert Java String To Long. It is a commonly encountered case to convert a String value to long when programming with the Java language.
Here are some examples for performing the conversion:

Convert using Long.parseLong()

The Long.parseLong() static method parses the string argument as a signed decimal long and returns a long value.

Syntax
public static long parseLong(String s) throws NumberFormatException

The parameter s will be converted to a primitive long value. Note that the method will throw a NumberFormatException if the parameter is not a valid long.

Example
String numberAsString = "1234";
long number = Long.parseLong(numberAsString);
System.out.println("The number is: " + number);
When you run the code above, the output will show this:
The number is: 1234
This is the most common and popular solution when you need to convert a String to long. Note that the resulting value is not an instance of the Long class but just a plain primitive long value.


Convert using Long.valueOf()

The Long.valueOf() static method will return an Long object holding the value of the specified String.

Syntax
public static Long valueOf(String s) throws NumberFormatException

Note that the method will throw a NumberFormatException if the parameter is not a valid long.

String numberAsString = "1234";
long number = Long.valueOf(numberAsString);
System.out.println("The number is: " + number);
This will output:
The number is: 1234
This is the most common method when you wish to convert a String to Long. Note that the resulting value is an instance of the Long class and not a primitive long value.


Convert using new Long(String).longValue()

Another alternative method is to create an instance of Long class and then invoke it's longValue() method.

Example

String numberAsString = "1234";
Long longObject = new Long(numberAsString);
long number = longObject.longValue();

We can shorten to:

String numberAsString = "1234";
long number = new Long(numberAsString).longValue();

or just:
long number = new Long("1234").longValue();


Convert using DecimalFormat

The class java.text.DecimalFormat is a class that can be used to convert a number to it's String representation. It can also be used the other way around - it can parse a String into it's numerical representation.
Example
String numberAsString = "1234";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   long number = decimalFormat.parse(numberAsString).longValue();
   System.out.println("The number is: " + number);
} catch (ParseException e) {
   System.out.println(numberAsString + " is not a valid number.");
}
Will output
The number is: 1234
Note that the parse() method will throw a ParseException when there are problems encountered with the String.


Convert with special radix

All of the examples above uses the base (radix) 10. There are cases when we wish to convert a Java String to Long but using another base. Both Long.parseLong() and Long.valueOf() can receive a custom radix to be used in the conversion

Examples

  • Binary
    String numberAsString = "11111111";
    long number1 = Long.valueOf(numberAsString, 2);
    long number2 = Long.parseLong(numberAsString, 2);
    System.out.println(number1);
    System.out.println(number2);
    
    Will output:
    255
    255
    
  • Octal
    String numberAsString = "377";
    long number1 = Long.valueOf(numberAsString, 8);
    long number2 = Long.parseLong(numberAsString, 8);
    System.out.println(number1);
    System.out.println(number2);
    
    Will output:
    255
    255
    
  • Hexadecimal
    String numberAsString = "ff";
    long number1 = Long.valueOf(numberAsString, 16);
    long number2 = Long.parseLong(numberAsString, 16);
    System.out.println(number1);
    System.out.println(number2);
    
    Will output:
    255
    255
    

String Tutorials And Examples