How To Convert Int To Long In Java

In Java, int and long are primitive data types used to represent integer values. The int type is a 32-bit signed integer and has a range of -2^31 to 2^31-1, while the long type is a 64-bit signed integer and has a range of -2^63 to 2^63-1.

Converting an int to a long in Java can be done implicitly, as the long type has a greater range and can hold all values that can be represented by an int. This means that if you assign an int value to a long variable, Java will automatically convert the value to a long without any loss of information.

Methods For Converting  Int To Long In Java

In Java, converting an int to a long is a simple process as it can be done implicitly. However, there are situations where you may need to explicitly convert an int to a long. Here are three approaches you can use to convert an int to a long in Java:

1. Method 1: Implicit Conversion

int myInt = 5;
long myLong = myInt;

2. Using Long.valueOf() method

int myInt = 5;
Long myLong = Long.valueOf(myInt);

3. Using Casting

int myInt = 5;
long myLong = (long)myInt;

We have three approaches for converting  Int To Long In Java, now let’s get into detail of each approach:

Approach 1: Converting  Int To Long In Java using Implicit Conversion

The simplest way to convert an int to a long is to assign the int value to a long variable. Java will perform the conversion automatically, as the long type has a greater range than int.

Here is a complete Java program that demonstrates how to convert an int to a long using implicit conversion:

Code:

public class IntToLongExample {
    public static void main(String[] args) {
        // Step 1: Create an integer variable
        int myInt = 10;
        
        // Step 2: Convert the int to a long using implicit conversion
        long myLong = myInt;
        
        // Step 3: Print the original int and the converted long values
        System.out.println("Original int value: " + myInt);
        System.out.println("Converted long value: " + myLong);
    }
}

Output:

Original int value: 10
Converted long value: 10

Explanation:

  • Create an int variable: We create an int variable called myInt and initialize it to the value of 10.
  • Convert the int to a long: We then convert the int variable to a long variable called myLong using implicit conversion. This is done by simply assigning the int variable to the long variable. Since the long type has a greater range than int, Java performs the conversion automatically.
  • Print the original int and converted long values: We print both the original int value and the converted long value using the System.out.println() method. This will output the values to the console.

Approach 2: Converting  Int To Long In Java using Long.valueOf() method

Another approach to convert an int to a long is to use the Long.valueOf() method. This method returns a Long object that represents the specified int value as a long.

This is an snippet of code of how to convert an int to a long using the Long.valueOf() method in Java:

Code:

public class IntToLongExample {
    public static void main(String[] args) {
        // Step 1: Create an integer variable
        int myInt = 50;
        
        // Step 2: Convert the int to a long using Long.valueOf() method
        Long myLong = Long.valueOf(myInt);
        
        // Step 3: Print the original int and the converted long values
        System.out.println("Original int value: " + myInt);
        System.out.println("Converted long value: " + myLong);
    }
}

Output:

Original int value: 50
Converted long value: 50

Explanation:

  • Create an int variable: We create an int variable called myInt and initialize it to the value of 50.
  • Convert the int to a long: We then convert the int variable to a Long object called myLong using the Long.valueOf() method. This method takes an int as its argument and returns a Long object representing the same value. The Long class is a wrapper class for the long primitive type, and it provides utility methods for working with long values.
  • Print the original int and converted long values: We print both the original int value and the converted long value using the System.out.println() method. This will output the values to the console.

Approach 3: Converting  Int To Long In Java using Casting

You can also use casting to explicitly convert an int to a long. Casting involves specifying the target type in parentheses before the value you want to cast.

This is a demonstration for converting an int to a long using casting in Java:

Code:

public class IntToLongExample {
    public static void main(String[] args) {
        // Step 1: Create an integer variable
        int myInt = 56;
        
        // Step 2: Convert the int to a long using casting
        long myLong = (long) myInt;
        
        // Step 3: Print the original int and the converted long values
        System.out.println("Original int value: " + myInt);
        System.out.println("Converted long value: " + myLong);
    }
}

Output:

Original int value: 56
Converted long value: 56

Explanation:

  • Create an int variable: We create an int variable called myInt and initialize it to the value of 56.
  • Convert the int to a long using casting: We then convert the int variable to a long variable called myLong using casting. This is done by placing the type we want to convert to, in this case long, in parentheses before the value to be converted. The cast operator, represented by the parentheses, tells Java to convert the value to the specified type.
  • Print the original int and converted long values: We print both the original int value and the converted long value using the System.out.println() method. This will output the values to the console.

Best Approach for Converting  Int To Long In Java

Long.valueOf() method is considered as the best approach for converting an int to a long in Java:

  • It is safe: Using the Long.valueOf() method is a safe way to convert an int to a long, because it takes care of any overflow or underflow issues that might occur when dealing with large or small numbers.
  • It returns a Long object: The Long.valueOf() method returns a Long object, which can be useful in situations where you need to work with long values as objects, rather than primitives. The Long class provides utility methods for working with long values, such as comparing them or formatting them as strings.
  • It is easy to use: The Long.valueOf() method is easy to use, because it takes only one argument, which is the int value to be converted. This makes it simple to integrate into your code, without requiring a lot of extra boilerplate.
  • It is efficient: The Long.valueOf() method is efficient, because it uses a cache of frequently used Long objects to avoid creating unnecessary objects. This can help to reduce memory usage and improve performance.
  • It is widely used: The Long.valueOf() method is widely used in Java code, and is considered to be a best practice for converting an int to a long. This means that it is likely to be familiar to other developers who may work on your code in the future.

Sample Problems for Converting  Int To Long In Java

Sample Problem 1

Write the code for converting Int to Long in Java using Long.valueOf() method with int value = ‘26’

Solution:

  • We create an int variable called value and initialize it to the value of 26.
  • We then convert the int variable to a long variable called longValue using the Long.valueOf() method. This method takes an int value as its argument and returns the corresponding Long object.
  • We print both the original int value and the converted long value using the System.out.println() method. This will output the values to the console.
public class IntToLongExample {
    public static void main(String[] args) {
        // Step 1: Create an integer variable
        int value = 26;
        
        // Step 2: Convert the int to a long using Long.valueOf() method
        Long longValue = Long.valueOf(value);
        
        // Step 3: Print the original int and the converted long values
        System.out.println("Original int value: " + value);
        System.out.println("Converted long value: " + longValue);
    }
}

Output:

Original int value: 26
Converted long value: 26

Sample Problem 2

Write the code for converting Int to Long in Java using Implicit Conversion with int value = ‘1589’

Solution:

  • First, we create an int variable called value and initialize it to the value of 1589.
  • Then, we then convert the int variable to a long variable called longValue using implicit conversion. Since a long can store larger values than an int, the conversion is done implicitly by the Java compiler.
  • Print both the original int value and the converted long value using the System.out.println() method. This will output the values to the console.
public class IntToLongExample {
    public static void main(String[] args) {
        // Step 1: Create an integer variable
        int value = 1589;
        
        // Step 2: Convert the int to a long using implicit conversion
        long longValue = value;
        
        // Step 3: Print the original int and the converted long values
        System.out.println("Original int value: " + value);
        System.out.println("Converted long value: " + longValue);
    }
}

Output:

Original int value: 1589
Converted long value: 1589

Sample Problem 3

Write the code for converting Int to Long in Java using Casting method with int value = ‘2345’

Solution:

  • Create an int variable called value and initialize it to the value of ‘2345’. Note that in Java, characters can be implicitly converted to their corresponding ASCII values, so the character literal ‘2’ is converted to the integer value 50, and so on.
  • Convert the int variable to a long variable called longValue using casting. Since the int value can fit within the range of values that can be represented by a long, the casting is done explicitly by the programmer.
  • Print both the original int value and the converted long value using the System.out.println() method. This will output the values to the console.
public class IntToLongExample {
    public static void main(String[] args) {
        // Step 1: Create an integer variable
        int value = 2345;
        
        // Step 2: Convert the int to a long using casting
        long longValue = (long) value;
        
        // Step 3: Print the original int and the converted long values
        System.out.println("Original int value: " + value);
        System.out.println("Converted long value: " + longValue);
    }
}

Output:

Original int value: 2345
Converted long value: 2345

Conclusion

Convert an int to a long includes methods like Casting, Implicit conversion and Long.valueOf() method. Implicit conversion happens automatically when assigning an int to a long. Casting involves explicitly converting an int to a long by using a cast operator.

Long.valueOf() method is a built-in method in the Long class that converts an int to a long.