int to long Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast int to long.

The int takes 4 byte of memory and long takes 8 bytes of memory. Assignment 4 bytes of memory to 8 byte of memory is done implicitly (automatic conversion).

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse like long to int requires explicit casting.

Examples of implicit casting

int x = 10;
longt y = x;

byte x = 10;
short y = x;

Following program on int to long explains where int is assigned to long implicitly.
public class Conversions
  public static void main(String args[])
    int i1 = 10;              // 4 bytes
    long l1 = i1;             // 4 bytes assigned to 8 bytes

    System.out.println("int value: " + i1);              // prints 10
    System.out.println("Converted long value: " + l1);   // prints 10

int to long
Output screenshot of int to long Java

long l1 = i1;

The int i1 is assigned to long l1. This is done implicitly by JVM as it is implicit casting between int of 4 bytes and long of 8 bytes. It is also known as widening conversion.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.