Java Program to convert int type variables to long

In the last Program we learnt about converting a long to int and now we will do vice-versa.

Example 1: Java Program to Convert int to long using Typecasting

class IntToLong {
  public static void main(String[] args) {

    // create int variables
    int a = 24;
    int b = 35;

    // convert int into long using typecasting
    long c = a;
    long d = b;

    System.out.println(c);
    System.out.println(d);
  }
}
Output
24
35

Example 2: Java Program to Convert int into object of Long using valueof()

class IntObjectToLong {
  public static void main(String[] args) {

    // create an int variable
    int a = 250;

    // convert to an object of Long using valueOf()
    Long obj = Long.valueOf(a);

    System.out.println(obj);   
  }
}
Output
250

Leave a Reply