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


This content was proof-read by Grammarly. You can Try Grammary 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

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: