Write a Java Program to convert long type variables into int

Java Program to convert long type variables into int

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert long type variables into int
Java Program to convert long type variables into int

Write a Java Program to convert long type variables into int

PROGRAM: Java Program to convert long type variables into int

/*Java Program to convert long type variables into int*/

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
}
}

2: long to int conversion using toIntExact()

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
}
}

3: Convert object of the Long class to int

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
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *