Learn Java by Example: Java Program to convert long type variables into int

Java Program to convert long type variables into int

In this program, we will learn to convert the long variable into an integer (int) in Java.


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

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

    // create long variables
    long a = 2322331L;
    long b = 52341241L;

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

    System.out.println(c);    // 2322331
    System.out.println(d);    // 52341241

In the above example, we have long type variables a and b. Notice the lines,

int c = (int)a;

Here, the higher data type long is converted into the lower data type int. Hence, this is called narrowing typecasting.

This process works fine when the value of the long variable is less than or equal to the maximum value of int (2147483647). However, if the value of the long variable is greater than the maximum int value, then there will be a loss in data.

Example 2: long to int conversion using toIntExact()

We can also use the toIntExact() method of the Math class to convert the long value into an int.

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

    // create long variable
    long value1 = 52336L;
    long value2 = -445636L;

    // change long to int
    int num1 = Math.toIntExact(value1);
    int num2 = Math.toIntExact(value2);

    // print the int value
    System.out.println(num1);  // 52336
    System.out.println(num2);  // -445636

Here, the Math.toIntExact(value1) method converts the long variable value1 into int and returns it.

The toIntExact() method throws an exception if the returned int value is not within the range of the int data type. That is,

// value out of range of int
long value = 32147483648L

// throws the integer overflow exception
int num = Math.toIntExact(value);


Example 3: Convert object of the Long class to int

In Java, we can also convert the object of wrapper class Long into an int. For this, we can use the intValue() method. For, example,

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

    // create an object of Long class
    Long obj = 52341241L;

    // convert object of Long into int
    // using intValue()
    int a = obj.intValue();

    System.out.println(a);    // 52341241

Here, we have created an object of the Long class named obj. We then used the intValue() method to convert the object into int type.



Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.