Java Convert String To Long Example

In this article, we will show you how to convert Java String to wrapper Long class or primitive type long code examples. There are different ways we can convert Java String to wrapper Long class or primitive type long.
This is a common scenario to convert String to wrapper Long class or primitive type long in java programming language.
In this post, we will demonstrate converting String to wrapper Long class or primitive type long in 4 ways.
  1. Convert using Long.parseLong()
  2. Convert using Long.valueOf()
  3. Convert using new Long(String).longValue()
  4. Convert using DecimalFormat

1. Convert using Long.parseLong()

Long.parseLong() - Parses the string argument as a signed decimal long.
//Convert using Long.parseLong()
String str = "123456";
long number = Long.parseLong(str);
System.out.println("Convert using Long.parseLong() :: " + number);
Output:
Convert using Long.parseLong() :: 123456

2. Convert using Long.valueOf()

//Convert using Long.valueOf()
String str = "123456";
long number1 = Long.valueOf(str);
System.out.println("Convert using Long.valueOf() :: " + number1);
Output:
Convert using Long.valueOf() :: 123456

3. Convert using new Long(String).longValue()

// Convert using new Long(String).longValue()
String str = "123456";
Long longObject = new Long(str);
long number2 = longObject.longValue();
System.out.println("Convert using new Long(String).longValue() :: " + number2);
Output:
Convert using new Long(String).longValue() :: 123456

4. Convert using DecimalFormat

// Convert using DecimalFormat
String str = "123456";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   long number3 = decimalFormat.parse(str).longValue();
   System.out.println("Convert using DecimalFormat: " + number3);
} catch (ParseException e) {
   System.out.println(str + " is not a valid number.");
}
Output:
Convert using DecimalFormat: 123456

Complete Program for Reference

import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * ConvertStringToLongExamples
 * @author javaguides.net
 *
 */
public class ConvertStringToLongExamples {
 public static void main(String[] args) {
  
  //Convert using Long.parseLong()
  String str = "123456";
  long number = Long.parseLong(str);
  System.out.println("Convert using Long.parseLong() :: " + number);
  
  //Convert using Long.valueOf()
  long number1 = Long.valueOf(str);
  System.out.println("Convert using Long.valueOf() :: " + number1);
  
  // Convert using new Long(String).longValue()
  Long longObject = new Long(str);
  long number2 = longObject.longValue();
  System.out.println("Convert using new Long(String).longValue() :: " + number2);
  
  // Convert using DecimalFormat
  DecimalFormat decimalFormat = new DecimalFormat("#");
  try {
     long number3 = decimalFormat.parse(str).longValue();
     System.out.println("Convert using DecimalFormat: " + number3);
  } catch (ParseException e) {
     System.out.println(str + " is not a valid number.");
  }
  
 }
}
Output:
Convert using Long.parseLong() :: 123456
Convert using Long.valueOf() :: 123456
Convert using new Long(String).longValue() :: 123456
Convert using DecimalFormat: 123456


Comments