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


Grammarly

More than 40 Lakhs Students and Professionals use Grammarly for their spell and grammar checks. You can register and try for free.

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


Grammarly

More than 40 Lakhs Students and Professionals use Grammarly for their spell and grammar checks. You can register and try for free.
%d bloggers like this: