Convert long primitive to Long object in Java


To convert long primitive to Long object, follow the below steps.

Let’s say the following is our long primitive.

// primitive
long val = 45;
System.out.println("long primitive: "+val);

Now, to convert it to Long object is not a tiresome task. Include the same long value while creating a new Long object −

// object
Long myObj = new Long(val);
System.out.println("Long object: "+myObj);

The following is the complete example −

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      // primitive
      long val = 45;
      System.out.println("long primitive: "+val);
      // object
      Long myObj = new Long(val);
      System.out.println("Long object: "+myObj);
   }
}

Output

long primitive: 45
Long object: 45

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements