Java Program to Convert long to int
Last Updated :
27 Nov, 2020
Long is a larger data type than int, we need to explicitly perform typecasting for the conversion. Typecasting is performed through the typecast operator. There are basically three methods to convert long to int:
- By type-casting
- Using toIntExact() method
- Using intValue() method of the Long wrapper class.
1. Using Explicit Type Casting
In typing casting, a data type is converted into another data type by the programmer using the casting operator during the program design. In typing casting, the destination data type may be smaller than the source data type when converting the data type to another data type, that’s why it is also called narrowing conversion.
Syntax/Declaration:
destination_datatype = (target_datatype)variable;
() is a casting operator.
target_datatype: is a data type in which we want to convert the source data type.
Type Casting Example:
float x;
byte y;
...
...
y=(byte)x;
Java
// Java Program to convert long to int
import java.util.*;
class GFG {
public static void main(String[] args)
{
// long value
long longnum = 10000;
// explicit type casting from long to int
int intnum = (int)longnum;
System.out.println("Converted type: "+ ((Object)intnum).getClass().getName());
System.out.println("Converted int value is: "
+ intnum);
}
}
OutputConverted type: java.lang.Integer
Converted int value is: 10000
2. Using toIntExact() method
Syntax :
public static int toIntExact(long value)
Parameter:
Return:
This method returns the input argument as an int(integer).
Exception:
It throws ArithmeticException - if the result overflows an int
Java
// Java Program to convert long to int
class Main {
public static void main(String[] args) {
// create long variable
long value1 = 523386L;
long value2 = -4456368L;
// change long to int
int num1 = Math.toIntExact(value1);
int num2 = Math.toIntExact(value2);
// print the type
System.out.println("Converted type: "+ ((Object)num1).getClass().getName());
System.out.println("Converted type: "+ ((Object)num2).getClass().getName());
// print the int value
System.out.println(num1); // 52336
System.out.println(num2); // -445636
}
}
OutputConverted type: java.lang.Integer
Converted type: java.lang.Integer
523386
-4456368
3. Using intValue() method of Long wrapper class
Syntax:
public int intValue()
Parameters: The method does not accept any parameters.
Return Value: The method returns the numeric value which is represented by the object after conversion to the integer type.
Java
// Java Program to convert long to int using intValue()
// method
class GFG {
public static void main(String[] args)
{
// Long object
Long longnum = 100L;
// Converting Long object to int primitive type
int intnum = longnum.intValue();
// printing the type
System.out.println("Converted type: "
+ ((Object)intnum).getClass().getName());
// printing the int value
System.out.println("Converted int value: "
+ intnum);
}
}
OutputConverted type: java.lang.Integer
Converted int value: 100
Similar Reads
Java Program to Convert int to long Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6
2 min read
Java Program to Convert Double to Long Given a Double real number. Write a Java program to convert the given double number into a Long (long) in Java. Examples: Input: double = 2545.241 Output: 2545 Input: double = 21.54 Output: 21 Double data type: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value rang
4 min read
Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read
Java Program to Convert String to Long To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a
3 min read
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read
Java Program to Convert Byte Array to Long A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Given an array of bytes, convert it to a long value. Example: Byte Array: 1 2 3 4 Long Value: 16909060 Equivalent Hexadecimal String: 0x1020304 There are numerous approaches fo
3 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210
2 min read