Java Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we'll discuss the concept of lossy conversion in Java and the reason behind it.

At the same time, we'll explore some handy conversion techniques to avoid this error.

2. Lossy Conversion

Lossy conversion is simply the loss of information while handling data.

In Java, it corresponds to the possibility of losing the value or precision of a variable while converting one type to another.

When we try to assign a variable of large-sized type to a smaller sized type, Java will generate an error, incompatible types: possible lossy conversion, while compiling the code.

For example, let's try to assign a long to an int:

long longNum = 10;
int intNum = longNum;

Java will emit an error while compiling this code:

incompatible types: possible lossy conversion from long to int

Here, Java will find long and int incompatible and result in lossy conversion error. Because there can be long values outside the int range -2,147,483,648 to 2,147,483,647.

Similarly, let's try to assign a float to a long:

float floatNum = 10.12f;
long longNum = floatNum;
incompatible types: possible lossy conversion from float to long

As float can have decimal values that don't have corresponding long value. Therefore, we'll receive the same error.

Similarly, assigning a double number to an int will cause the same error:

double doubleNum = 1.2;
int intNum = doubleNum;
incompatible types: possible lossy conversion from double to int

The double values can be too large or too small for an int and decimal values will get lost in the conversion. Hence, it is a potential lossy conversion.

Also, we can run into this error while performing a simple calculation:

int fahrenheit = 100;
int celcius = (fahrenheit - 32) * 5.0 / 9.0;

When a double multiply with an int, we get the result in a double. Consequently, it is also a potential lossy conversion.

Therefore, the incompatible types in lossy conversion can either have different sizes or types (integers or decimals).

3. Primitive Data Types

In Java, there are many primitive data types available with their corresponding wrapper classes.

Next, let's compile a handy list of all possible lossy conversions in Java:

  • short to byte or char
  • char to byte or short
  • int to byte, short or char
  • long to byte, short, char or int
  • float to byte, short, char, int or long
  • double to byte, short, char, int, long or float

Note that though short and char have the same size. Still, the conversion from short to char is lossy because char is an unsigned data type.

4. Conversion Techniques

4.1. Converting Between Primitive Types

The easy way of converting primitives to avoid lossy conversion is through downcasting; in other words, casting the larger-sized type to a smaller-sized type. Hence, it is also called narrowing primitive conversion.

For instance, let's convert a long number to a short using downcasting:

long longNum = 24;
short shortNum = (short) longNum;
assertEquals(24, shortNum);

Similarly, let's convert a double to an int:

double doubleNum = 15.6;
int integerNum = (int) doubleNum;
assertEquals(15, integerNum);

However, we should note that converting large-sized type with values too large or too small to smaller-sized type through downcasting can result in unexpected values.

Let's convert long values outside the range of short:

long largeLongNum = 32768; 
short minShortNum = (short) largeLongNum;
assertEquals(-32768, minShortNum);

long smallLongNum = -32769;
short maxShortNum = (short) smallLongNum;
assertEquals(32767, maxShortNum);

If we carefully analyze the conversion, we'll see that these are not the expected values.

In other words, when Java hits the highest value of a small-sized type while converting from large-sized type, the next number is the lowest value of the small-sized type and vice-versa.

Let's understand this through examples. When largeLongNum with the value of 32768 is converted to short, the value of shortNum1 is -32768. Because the max value of short is 32767, therefore, Java goes for the next min value of the short.

Similarly, when smallLongNum is converted to short. The value of shortNum2 is 32767 as Java goes for the next max value of the short.

Also, let's see what happens when we convert the max and min values of a long to an int:

long maxLong = Long.MAX_VALUE; 
int minInt = (int) maxLong;
assertEquals(-1, minInt);

long minLong = Long.MIN_VALUE;
int maxInt = (int) minLong;
assertEquals(0, maxInt);

4.2. Converting Between Wrapper Objects and Primitive Types

To directly convert a wrapper object to a primitive, we can use various methods in wrapper classes such as intValue(), shortValue() and longValue(). This is called unboxing.

For instance, let's convert a Float object to a long:

Float floatNum = 17.564f;
long longNum = floatNum.longValue();
assertEquals(17, longNum);

Also, if we look at the implementation of longValue or similar methods, we'll find the use of narrowing primitive conversion:

public long longValue() {
    return (long) value;
}

However, at times, narrowing primitive conversion should be avoided to save valuable information:

Double doubleNum = 15.9999;
long longNum = doubleNum.longValue();
assertEquals(15, longNum);

After conversion, the value of longNum will be 15. However, the doubleNum is 15.9999, which is very close to 16.

Instead, we can use Math.round() for conversion to the closest integer:

Double doubleNum = 15.9999;
long longNum = Math.round(doubleNum);

assertEquals(16, longNum);

4.3. Converting Between Wrapper Objects

For this, let's use the already discussed conversion techniques.

First, we'll convert wrapper object to a primitive value, downcast it and convert it to another wrapper object. In other words, we'll perform unboxing, downcasting, and boxing techniques.

For example, let's convert a Double object to an Integer object:

Double doubleNum = 10.3;
double dbl = doubleNum.doubleValue(); // unboxing
int intgr = (int) dbl; // downcasting
Integer intNum = Integer.valueOf(intgr);
assertEquals(Integer.valueOf(10), intNum);

Lastly, we're using Integer.valueOf() to convert the primitive type int to an Integer object. This type of conversion is called boxing.

5. Conclusion

In this article, we've explored the concept of lossy conversion in Java with the help of a number of examples. In addition, we've compiled a handy list of all possible lossy conversions as well.

Along the way, we've identified narrowing primitive conversion as an easy technique to convert primitive numbers and avoid the lossy conversion error.

At the same time, we've also explored additional handy techniques for numeric conversions in Java.

The code implementations for this article can be found over on GitHub.

Java bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE
Comments are closed on this article!