Co-Authored By:

##### Asked by: Darleen Bellucci

technology and computing programming languages# How do you cast long to int?

**long**l = 42;

**int**i = (

**int**) l; However, a

**long**can hold more information than an

**int**, so it's not possible to perfectly

**convert**from

**long to int**, in the general case. If the

**long**holds a number less than or equal to Integer. MAX_VALUE you can

**convert**it by

**casting**without losing any information.

Considering this, what happens when you cast long to int?

When one needs to force a **Long** into an **int** there is potential for **integer** overflow because the numeric value of the **Long** may have a greater magnitude than the **int** can accurately represent. If one is told that a given **Long** will never be larger than what an **int** can hold, the static method Math.

**convert**a

**long**value to

**String**in Java e.g. by using

**Long**. toString(

**long**value) method, by using

**String**. valueOf(

**long**) and by concatenating with an empty

**String**. You can use any of these method to

**convert**a

**long**data type into

**String**object.

Also question is, how do you convert long to int in Python?

**Number Type Conversion**

- Type int(x) to convert x to a plain integer.
- Type long(x) to convert x to a long integer.
- Type float(x) to convert x to a floating-point number.
- Type complex(x) to convert x to a complex number with real part x and imaginary part zero.

2 Answers. Yes, that's fine. The **int will** be implicitly converted to a **long** , which **can** always be done without any loss of information. You **can compare long and int** directly however this is not recommended.