Menu

How to Convert long to String in Java?

How to convert Java long to String

In Java, we can convert long into a String in two ways either by using the valueOf() method or toString() method.

1. By Using String.valueOf() Method

The valueOf() method is a part of String class. It is a static method that converts a long value into a String value.

Example 1:

Here, a long value is passed in the method and converted into a String by using the valueOf() method.

public class StudyTonight
{  
    public static void main(String args[])
    {  
        long n = 500L;  
        String s = String.valueOf(n); 
        System.out.println("The string value is " +s);  
    }
}

Output:

The string value is 500

2. By Using Long.toString() Method

The toString() method is a part of Long class. It is a static method that can also be used to convert a long value to String.

Example 2:

Here, a long value is passed in the toString() method to get converted into a String.

public class StudyTonight
{  
    public static void main(String args[])
    {  
        long n = 500L;  

        String s = Long.toString(n); 
        System.out.println("The string value is " +s);
    }
}

Output:

The string value is 500

3. By Using Concatenation Process

We can use + operator to concatenate long value with string object and then get string as result. The + operator returns a string object after concatenation.

public class StudyTonight
{  
    public static void main(String args[])
    {  
        long n = 500L;  

        String longString = ""+n; //concatenation

        System.out.println("String is : "+longString);
    }
}

Output:

String is : 500