Integer To Long in Java

Java Integer To Long | The Integer object of the wrapper class can be converted to the primitive data types are you wondering how? Yes, Java provides various methods to convert objects into primitive ones. In other blogs, we have converted Integer objects to int and int arrays as well.

“Long” is a wrapper class whereas “long” is a primitive data type. Similarly, “Integer” is a wrapper class whereas “int” is a primitive data type. Here, we will convert the Integer object to the long primitive variable and Long wrapper class Object.

Integer To Long In Java

Let us first see how to convert an Integer object to a Long object In Java. We can do it in the following ways:-

  1. Using Long.valueOf() method
  2. Using autoboxing & auto unboxing

How To Convert Integer To Long In Java Using Long.valueOf() method

Java Long class contains the valueOf() method to convert the primitive long value to the Long object. The method is given as follows:- public static Long valueOf(long l)

public class Main {
    public static void main(String[] args) {
        Integer numInteger = 99;
        System.out.println("Integer value: " + numInteger);
        System.out.println(numInteger.getClass().getName());

        Long numLong = Long.valueOf(numInteger);
        System.out.println("Long value: " + numLong);
        System.out.println(numLong.getClass().getName());
    }
}

Output:-

Integer value: 99
java.lang.Integer
Long value: 99
java.lang.Long

When the valueOf() method takes a long primitive then how it accepts the Integer value in the argument? Due to the auto unboxing feature, Integer is internally converted to an int value, see more:- Integer to int in Java. If the method argument is long then it can accept the int value.

The return type of the valueOf() method is the Long object. In the above program, we are passing the Integer object as a parameter and it returns the Long object.

How To Convert Integer To Long In Java Using Autoboxing & Auto unboxing

Now we use the direct assignment method in order to convert the Integer object of the wrapper class to a long primitive type. The code works as follows, first, create an Integer object. Create a long primitive variable and assign it to the Integer object. By this line, the Integer object will be explicitly converted to the long primitive data type.

public class Main {
    public static void main(String[] args) {
        Integer numInteger = 777;
        System.out.println("Integer value: " + numInteger);
        System.out.println(numInteger.getClass().getName());

        // Integer to long
        long value = numInteger;

        // long to Long
        Long numLong = value; // autoboxing
        System.out.println("Long value: " + numLong);
        System.out.println(numLong.getClass().getName());
    }
}

Output:-

Integer value: 777
java.lang.Integer
Long value: 777
java.lang.Long

When we convert Integer to a long variable using assignment then internally first, the Integer value will be converted to an int and then the int value is getting converted to a long. Now, we can take the help of the autoboxing feature to convert long primitive to Long objects through a simple assignment.

Integer Object To long Primitive In Java

You may have figured out from the above programs how to convert an Integer object to a long primitive in Java. But for convenience, let us see them individually.

Integer Object To long Primitive In Java using Long.valueOf() method

We can use the valueOf() method to convert the Integer object to a long primitive value as we have converted the Integer object to a Long object. We only have to change to variable type which stores the return value.

public class Main {
    public static void main(String[] args) {
        Integer numInteger = 500;
        System.out.println("Integer value: " + numInteger);
        System.out.println(numInteger.getClass().getName());

        long numLong = Long.valueOf(numInteger);
        System.out.println("long primitive value: " + numLong);
    }
}

Output:-

Integer value: 500
java.lang.Integer
long primitive value: 500

The return type of the valueOf() method is the Long object, but due to auto unboxing Long object is internally converted into the long primitive value.

Integer Object To long Primitive In Java using Assignment

public class Main {
    public static void main(String[] args) {
        Integer numInteger = 888;
        System.out.println("Integer value: " + numInteger);
        System.out.println(numInteger.getClass().getName());

        // Integer to long
        long value = numInteger;
        System.out.println("long primitive value: " + value);
    }
}

Output:-

Integer value: 888
java.lang.Integer
long primitive value: 888

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *