How to convert long to String in Java

How to convert long to String in Java

Example:

Input:  num=1320
Output: "1320"

Approach: 1: Using toString()

Java


public class LongToString {
    public static void main(String[] args) {
        long num = 1320;
        String numLong = longToString(num);
        System.out.println("String is " + numLong);
    }

    private static String longToString(long num) {
        return Long.toString(num);
    }

}

Approach : 2: Using valueOf()

Java


public class LongToString {
    public static void main(String[] args) {
        long num = 1320;
        String numLong = longToString(num);
        System.out.println("String is " + numLong);
    }

    private static String longToString(long num) {
        return String.valueOf(num);
    }

}


No comments:

Post a Comment