Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Convert Int to Long in Java

In this tutorial, we will learn how to convert int numeric value to long value, int value to Long wrapper object and vice versa in Java easily.

To convert int to long numeric value in Java, we use an assignment operator.

We do not need to do extra to convert int to long. This is because the lower type can be converted to higher type implicitly.

We also known this conversion as implicit type casting or type promotion.

Let’s create a simple Java Program to convert int to long easily.

Program code 1:

// Java Program to demonstrate the conversion of int into long implicitly.
package javaConversion;
public class IntToLongConversion {
public static void main(String[] args) 
{
 int i = 9999; 
 long l = i;  
 System.out.println("Long numeric value after conversion: " +l);
 }
}
Output:
     Long numeric value after conversion: 9999

Converting Int to Long object in Java using Long.valueOf() method


We can convert int numeric value to Long object by creating an object of Long wrapper class and then call its valueOf() method.

Let’s make a simple Java program for converting int to Long object using valueOf() method of Long class.

Program code 2:

// Java Program to demonstrate the conversion of int into long object.
package javaConversion;
public class IntToLongConversion {
public static void main(String[] args) 
{
 int num = 9999; 
 Long l1 = new Long(num); // first way  
 Long l2 = Long.valueOf(num); // second way  
 System.out.println(l1);  
 System.out.println(l2); 
 }
}
Output:
      9999
      9999

In the above program, the valueOf() method of Long wrapper class returns a Long instance representing the specified long value.

Convert Long to Int in Java using typecasting


To convert long to int numeric value in Java, we use typecasting. To convert the higher data type to lower, we perform typecasting in the program through a typecast operator (datatype).

Let’s make a Java program for converting long primitive type into int value using typecasting.

Program code 3:

// Java Program to demonstrate the conversion of long into int value.
package javaConversion;
public class LongToIntConversion {
public static void main(String[] args) 
{
 long l = 600;  
 int i = (int)l;  
 System.out.println(i); 
 }
}
Output:
     600

Long object to int Conversion


For converting Long object to int value in Java, we use intValue() method of Long class. Let’s take a simple program for converting Long object to int in Java.

Program code 4:

// Java Program to demonstrate the conversion of Long object into int value using intValue() method.
package javaConversion;
public class LongToIntConversion {
public static void main(String[] args) 
{  
 Long l = new Long(657);  
 int i = l.intValue();  
 System.out.println(i); 
 }
}
Output:
      657

In this example program, we have used intValue() method of Long class for converting Long object to int value. The intValue() method returns the value of this Long as an int after a narrowing primitive conversion.


In this tutorial, you learned how to convert a primitive type long value to int and vice versa in Java easily. Hope that you will have understood and practiced all programs based on the conversion of int to long in Java.
Thanks for reading!!!

The post How to Convert Int to Long in Java appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

How to Convert Int to Long in Java

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×