Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Introduction

Data type conversions form a critical part of programming in any language. In Java, developers frequently need to convert an int to a Long or vice-versa, as presented in our article.

While this operation may seem straightforward, Java offers several methods with unique characteristics and use cases.

In this tutorial, we’ll delve into these methods, providing a clear understanding of how each approach works and when to use them.

2. The Basics: Understanding int and Long

Before we explore the conversion methods, let’s first understand what int and Long are. Both int and Long are different types of data types we use in Java to store numerical values.

The int data type is a 32-bit signed two’s complement integer, which can hold a minimum value of -2^31 and a maximum value of 2^31-1.

In contrast, Long is a 64-bit two’s complement integer, which can hold values from -2^63 to 2^63-1. We usually use Long when the range provided by int is not sufficient.

3. Autoboxing: The Implicit Conversion

Java introduced a feature called autoboxing in its 5th version, which allows automatic conversion between primitive types and their corresponding wrapper classes.

Let’s use autoboxing to convert an int to a Long directly:

int intTen = 10;
Long longTen = (long) intTen;

We declare an int variable intTen and assign it a value of 10. We then declare a Long variable longTen, and assign it the value of intTen. The (long) before intTen is a type-casting operator that tells the compiler to convert intTen to long before assigning it to longTen. This process is automatic, hence the term autoboxing.

4. Using Long.valueOf(): The Wrapper Class Method

The Long class in Java is a wrapper class that provides several utility methods for dealing with long values. One such method is valueOf(), which can take an int and return a Long object.

This method is a straightforward and efficient way to convert an int to a Long:

int intTen = 10;
Long longTen = Long.valueOf(intTen);

We use the valueOf() method of the Long class to convert the int value to a Long. The valueOf() method returns a Long instance representing the specified int value. This method is part of the Long class, which is a wrapper class for the long data type in Java.

5. Using new Long() Constructor: The Object Creation Method

Another way to convert an int to a Long is by using the Long constructor. This method creates a new Long object from an int. However, this approach is less efficient due to the overhead of creating a new object:

int intTen = 10;
Long longTen = new Long(intTen);

We create a new Long object by using the Long constructor. The Long constructor takes an int value and creates a new Long object representing the same numerical value. While this method is straightforward, we should avoid using it for performance-critical applications due to object creation overhead, so the static factory valueOf() is generally a better choice.

6. Using Long.parseLong(): The String Conversion Method

We typically use the Long.parseLong() method to convert a String to a Long. However, we can also use it with an int by first converting the int to a String:

int intTen = 10;
Long longTen = Long.parseLong(String.valueOf(intTen));

We first convert the int to a String using the String.valueOf() method. We then pass this String to the Long.parseLong() method, which parses the String as a long value and returns a Long object representing this value. This method is a bit roundabout, but it can be helpful in specific scenarios where the input is a String.

7. Additional Considerations

When converting from int to Long, it’s important to note that you’re going from a smaller data type to a larger one. This means there’s no risk of losing data during the conversion.

However, the reverse operation (from Long to int) may result in data loss if the Long value is larger than Integer.MAX_VALUE, which represents the maximum positive integer value that can be represented in 32 bits, holds the result of the formula 2^31-1 (i.e. 2147483647).

Also, while the Long data type can hold larger values, it consumes more memory. An int in Java takes up 4 bytes of memory, while a Long takes up 8 bytes. Therefore, it’s important to consider the memory implications when deciding to use Long instead of int.

8. Deep Dive: Understanding the Underlying Mechanisms

To fully appreciate the conversion methods, it’s beneficial to understand the underlying mechanisms. Autoboxing, for instance, is a syntactic sugar that the Java compiler provides. It automatically inserts the necessary code to convert between the primitive type and the corresponding wrapper class. This feature simplifies the code and makes it more readable.

On the other hand, the valueOf() method and the Long constructor are part of the Long class, a wrapper class for the long primitive type. Wrapper classes in Java provide a way to use primitive data types as objects. They offer several utility methods, including those for conversion, which we use in our examples.

Notably, the valueOf() method is favored for its object reusability through internal caching for values ranging from -128 to 127, improved performance, minimized garbage collection, code consistency, alignment with autoboxing, and sustained applicability, especially considering the deprecation of the new Long() constructor in recent Java versions.

The parseLong() method, typically used for String to Long conversion, can also handle int to Long conversion. However, it requires an extra step of first converting the int to a String. This method is a bit more roundabout, but it’s useful when dealing with numerical inputs that come in string format, such as user inputs or data read from a file.

9. Conclusion

We can convert an int to a Long in Java in several ways, each method has advantages and use cases. Autoboxing is a simple and direct approach, while Long.valueOf() and Long.parseLong() provide more explicit conversions. The Long constructor can also be used, but it’s less efficient due to the object creation overhead.

Choosing the method that best fits your needs and the context of your code is essential.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Notify of
Inline Feedbacks
View all comments