How to convert String to Long or Long to String in java with examples

Long and String Object Conversion in Java

Long is an object that holds larger values, serving as a wrapper class in Java for the primitive type long. If you initialize long values, you need to append l to its value.

The String class represents a group of characters enclosed in double-quotes. If the string contains non-numeric characters, attempting to convert these non-numeric values to long or any numeric data type results in a NumberFormatException.

When data is retrieved from the database, the application may need to display the data either from long to string or string to long. These are common requirements in developer coding tasks to cast different Java objects. Both Long and String are classes defined in the java.lang package.

First, let’s see different ways we can create a Long or String object:

Long l = 78l;
Long l1 = new Long(3);
String s=new String("23");
String s1="21";

This blog post is about how we can convert Long to String or vice versa.

How to Convert Long to String in Java?

There are several ways to convert Long to String in Java. Let’s discuss different ways.

  • Using toString() Method

    Every class or object has a toString() method. The Long class also has the toString() method that returns the string form of the long primitive value.

    You can use it in two ways:

    • using toString method Long.toString(longvalue) method
    • longobject.toString() method
    Long l = 1234l;
    System.out.println(l.toString()); // outputs 1234
    System.out.println(Long.toString(l)); // outputs 1234
    
  • Using String.valueOf() Method

    The String class has many static methods. valueOf() is one overloaded method. It returns the value of a long value. The valueOf method has different versions for taking parameters as double, integer, and Long. public static String valueOf(Object value) where the object can be of any type.

    Long longValue=782l;
    System.out.println(String.valueOf(longValue)); //outputs 782
    
  • Using DecimalFormat Class

    The java.text.DecimalFormat class is used to format a long value and convert it into a String.

    Long longObject = 78945l;
    DecimalFormat df = new DecimalFormat("#");
    System.out.println( df.format(longObject));
    

    output is 78945

  • Using empty String and append

    Any numeric value like integer, long, or double can be converted to a string using the plus operator. This process creates a string object by appending an empty string and the numeric value.

    Long longObject = 7845l;
    String longString = "" + longObject;
    System.out.println(longString);
    
    

How to Convert String to Long in Java?

There are several simple ways to convert it.

  • Using Long Constructor

    Every Numeric object has a constructor with a string parameter. If a string with non-numeric characters is passed, a NumberFormatException will be thrown. The character l is also not allowed in the string; only numeric characters are allowed.

    Long longObject=new Long("123");
    System.out.println(longObject);
    
    
  • Using Long.parseLong() Method

    The Long class has many static methods, and parseLong is one of them. It throws a NumberFormatException if the string cannot be converted to a long.

    String str = "1234";
    Long longValue = Long.parseLong(str);
    System.out.println(longValue);
    String str1 = "1234as";
    try {
        Long longValue1 = Long.parseLong(str1);
        System.out.println(longValue1);
    } catch (NumberFormatException e) {
        System.out.println("NumberFormatException: " + e.getMessage());
    }
    
    

    output is

    1234
    java.lang.NumberFormatException: For input string: "1234as"
    

    ”1234as” string is unable to convert to long and thrown NumberFormatException.

  • Using Long.valueOf(String) Method

    The Long object has a valueOf method that returns a numeric value and returns a Long object, not a primitive type long.

    String str = "897";
    Long longValue = Long.valueOf(str);
    System.out.println(longValue);
    String str1 = "87as";
    Long longValue1 = Long.valueOf(str1);
    System.out.println(longValue1);
    
    

That concludes the process of casting long to string or string to long.