Java – How to convert Long to other primitive numeric data types ?

Problem Statement

You need convert a long value to other numeric data types in java.

Solution

Use the Long wrapper class which exposes methods like byteValue , shortValue , intValue etc.

Below is a sample code snipper demonstrating the usage of the same.

public class Main {

    public static void main(String[] args) {

        System.out.println("Abundantcode.com Java Tutorials");

        Long input = new Long("76");
        
        // Convert Long to short value
        short output1 = input.shortValue();
        System.out.println(output1);
        
        // Convert Long to int
        int output2 = input.intValue();
        System.out.println(output2);
        
        // Convert long to double
        double output3 = input.doubleValue();
        System.out.println(output3);
        

    }
}

The output for the above program is

Abundantcode.com Java Tutorials

76

76

76.0

2 Comments

Leave Your Comment

Liked this content?. Please spread the word :)