Convert int to Long in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Typecasting refers to the process of converting one data type to another. Several times, it is required to convert a String to int, a double to long, an int to long, etc.

The int data-type can be converted to long data-type using both implicit and explicit conversions. We can perform implicit conversion because lower data type can be converted to higher data type without any loss.

Introduction

To understand the conversion of an int variable to a long variable, we must first understand typecasting in Java.

Casting means treating or converting a variable/expression from one data type to another. It is used to avoid data loss due to mathematical expressions.

There exist two types of casting in Java:

  • Widening Casting: This is an automatic/implicit conversion of a small data type to a large data type by the compiler without losing any data.
    • This is possible because converting a smaller data type to a larger one is highly compatible with Java programming.
    • Widening Casting cannot lead to any data loss under any circumstances.
    • We can also use explicit conversion in widening casting, but explicit conversions are not necessarily required here.
    • It is also called implicit casting.

widening-casting-data-types

The above-shown data types are arranged in an ascending order on which widening casting can be applied in the given order only, like byte can be converted into short, short can be converted into an int, an int can be converted into long, and so on.

In Java, a byte variable takes the least bytes (1 byte), while a double variable takes the most bytes (8 bytes) in the memory.

  • Narrowing Casting: This casting type requires manually converting larger data types to smaller data types.
    • This conversion is also called explicit casting, as it is explicitly required to perform this conversion.
    • Narrowing casting can lead to data loss, and it must be done consciously.
    • It can be performed by the user using explicit typecasting methods only.

narrowing casting data types

The above-shown data types are arranged in descending order on which the user can apply narrowing casting.

Note: From the above explanation, we can conclude that a variable/expression of type int can be converted into the type long without data loss using the widening casting technique. We can also convert an int to a long using explicit typecasting methods. Let's see these methods below.

Method 1: Convert int to Long in Java using Implicit Conversion (Simple Assignment Operation)

In Java, an int variable (4 bytes) is a smaller data type than a long variable (8 bytes). So, we can convert an int to a long variable using a simple assignment operation, i.e., implicit/automatic conversion by the compiler. This is also known as automatic type promotion by the compiler.

Example Java Program:

Output:

Explanation:

  • Simple assignment of the variables' i', j, and k to the variables l, m', and n, respectively, implicitly converts these intvalues tolong`.
  • We can see the class type Long in the output using the getClass() method.

Method 2: Convert int to Long in Java Using Explicit Typecasting

The process of sending information by the user to the compiler that the program is attempting a data type conversion is known as explicit conversion or explicit typecasting. We can also convert an int variable to a long variable using explicit typecasting. We have to put the data type we want and the other variable/expression in the parentheses like the given syntax below:

Syntax:

It creates a long data type value var that contains the same value as the int_var_name variable of type int.

Example Java Program:

Output:

Explanation:

  • The (long) expression before the int variables (i), (j), and (k) in the above Java program converts the value of i = 10, j = 15, and k = 20 to long type, which is the explicit type casting method.
  • We can see from the output that the getClass() method prints Long in the output after casting the variables.

Method 3: Convert int to Long Object in Java Using valueOf() Method

We can also convert an int variable to an object of the Long wrapper class using the in-built valueOf() method of the Long wrapper class in Java. Let's see the syntax and parameters for the valueOf() function.

Syntax:

Parameter:

  • (int_var_name)

The integer value we're converting into Long is the only parameter passed to the function valueOf() method.

Return value: It returns an object of type Long wrapper class with the same value as the int_var_name variable of type int.

Let's see the Java program to understand this method:

Example Java Program:

Output:

Explanation:

In the above program, the valueOf() method of the Long wrapper class in Java converts the int variable i = 15 to an object of the Long wrapper class, i.e., L = 15.

Conclusion

  • In programming, changing a variable's data type is often required to execute a mathematical operation or avoid data loss. This phenomenon is called typecasting. The int data type is smaller than long, so it can be converted using a simple assignment operation (implicit type casting) of an int variable to a long variable.
  • It can be converted using explicit type casting, (long)(int_var_name).
  • It can also be converted using the valueOf() method of the Long wrapper class in Java, Long.valueOf(int_var_name).