Convert long primitive to Long object in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 26-Dec-2018 15:17:25
Advertisements