Syntax error Convert long primitive to Long object in Java

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
Updated on: 2019-07-30T22:30:24+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements