Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: incompatible types: possible lossy conversion from int to short. I don't know why i'm getting this error message

Tags:

java

public class Demo 
{
    public static void main(String[] args) 
    {
       int a=10,b=20;
       short c = (a<b)?a:b;
       System.out.println(c);
    }
}

this is my program and I got below error why I'm not getting

"Demo.java:6: error: incompatible types: possible lossy conversion from int to short
short c = (a<b)?a:b;
1 error" 

And I write "final" with variable declaration and it works fine. But why does this happen?

public class Demo 
{
    public static void main(String[] args) 
    {
        final int a=10,b=20;
        short c = (a<b)?a:b;
        System.out.println(c);
    }
}
like image 480
MAHESH Avatar asked Dec 14 '18 05:12

MAHESH


People also ask

How do you fix a lossy conversion in Java?

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.

How do you solve a lossy conversion from double to int?

Solution: 1) be consistent, and 2) use names starting with lowercase letters for method names ... as per the Java style guides. The second problem is due to the method signature for Math. pow ; see the javadoc. It returns the result as a double .


2 Answers

To answer your question, you are basically Down-casting the int data type to short in both the cases which is not acceptable in Java. To describe in detail, below are the detailed description:-

Case 1:-

public class Demo 
{
    public static void main(String[] args) 
    {
       int a=10,b=20;
       short c = (a<b)?a:b;
       System.out.println(c);
    }
}

In this scenario, you are just down-casting data type from int to short which is throwing

error: incompatible types: possible lossy conversion from int to short

Case 2:-

public class Demo 
{
    public static void main(String[] args) 
    {
        final int a=10,b=20;
        short c = (a<b)?a:b;
        System.out.println(c);
    }
}

In this scenario, since you have declared your variables viz. a & b as final. As a result, both the values becomes CONSTANT for variable c. It means for the variable c, the values of a & b won't get changed & it has been finalized to 20 which is in range of short data type (-32,768 to 32,767). Therefore, you won't get any error unless & until you keep the value within the range.

Real Test case:-

public class Demo 
{
    public static void main(String[] args) 
    {
        final int a=32768,b=32788;
        short c = (a<b)?a:b;
        System.out.println(c);
    }
}

Compile this class & see the magic! You'll again get the same error.

For your further understanding, please refer here to get a clear picture in general.

Moral of the story:- Just do not downcast your data type!!

like image 74
Abhinav Avatar answered Oct 13 '22 04:10

Abhinav


This is specified in the Java Language Specification §5.2 Assignment Contexts:

Assignment contexts allow the use of one of the following:

...

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

The expression with ternary operator that you have used in the second snippet is a constant expression, because all the variables used in that expression are final. In addition, 10 is representable in short. Therefore, a narrowing primitive conversion from int to short is allowed.

This makes sense, doesn't it? The compiler knows all about the values of the final variables, so this can surely be allowed.

In the first case however, a and b are not constant variables, so (a<b)?a:b is not a constant expression, so an explicit cast is needed:

short c = (short)((a<b)?a:b);
like image 29
Sweeper Avatar answered Oct 13 '22 04:10

Sweeper