Java convert long to int – Java Program to Convert long to int

Java convert long to int: In the previous article we have discussed Java Program to Convert long to String

In this article we will see how to convert a long type  to int type.

Program to Convert long to int

Convert long to integer java: Let’s see some examples of both the types.

Example: long type

long a = 2322331L
long b = 1234567890123456L
Example: int type

int a = 2
int b = 123

Now, let’s see different ways to convert long type to int type.

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Method 1 : Java Program to Convert long to int By Using  typecasting

How to convert long to int in java: Long type variable can be converted to integer  by using  typecasting. Let’s see how it works.

Here this method is also called narrowing typecasting because in this  the higher data type long is converted into the lower data type int.

Approach :

  1. Take a long value and store it in a long variable input1
  2. Typecast the variable with int and store it in an integer variable output .
  3. Display the result .

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        long input1=2322331L;
       // converting to int
        int output =(int)input1 ;
        System.out.println("Converted integer  is : " + output);
    }
} 


Output : 

Converted integer  is : 2322331

Method 2 : Java Program to Convert long to int By Using Math.toIntExact( ) method

Integer can be converted to Character  by using  Math.toIntExact( ) method. Lets see how it works.

Here, the Math.toIntExact( ) method will convert the long variable into int variable  and return it.

Approach :

  1. Take a long value and store it in a long  variable input1
  2. Then pass that input1 variable as parameter to Math.toIntExact( ) method which will convert the long to int value and return it .
  3. Store that integer value  in an int variable output .
  4. Display the result .

Program:

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    
    {
    //long value assigned
    long input1=2322331L;
    // converting to int
    int output = Math.toIntExact(input1) ;
    System.out.println("Converted integer is : " + output);
    }
}
Output : 

Enter a long variable   : 2322331L
Converted integer  is : 2322331

Method 3 : Java Program to Convert long to int By Converting the object of the Long class to int

Integer can be converted to Character  by converting the object of the Long class to int . Let’s see how it  works.

By using intValue() method we can convert  the object of wrapper class Long into an int.

Approach :

  1. Take a long value and store it in a long object  ”ob”.
  2. Then pass that object as ob.intValue()  which will convert the long to int value and return it .
  3. Store that int value in a variable output .
  4. Display the result .

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
    Long ob= new Long(2322331L);
    // converting to int
    int output = ob.intValue() ;
    System.out.println("Converted integer is : " + output);

    }
}
Output : 

Enter a long variable   : 2322331L
Converted integer  is : 2322331

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple Java program example guide and practice well.

Related Java Program: