0

I'd a code snippet:

class AutoTypeCast{
    public static void main(String...args){
        int x=10;
        byte b=20;//no compilation error
        byte c=x;//compilation error
    }
}

Why 20 is automatically type-casted to byte while x not?

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • possible duplicate of [Why do I have to cast 0 to byte when the method argument is byte?](http://stackoverflow.com/questions/7324372/why-do-i-have-to-cast-0-to-byte-when-the-method-argument-is-byte) – Lukas Eder Feb 21 '12 at 15:15
  • Many more similar/duplicate questions: http://stackoverflow.com/questions/1935699/java-implicit-conversion-of-int-to-byte, http://stackoverflow.com/questions/7369493/integer-to-byte-casting-in-java, http://stackoverflow.com/questions/81392/java-why-do-i-receive-the-error-message-type-mismatch-cannot-convert-int-to-b – Lukas Eder Feb 21 '12 at 15:17

8 Answers8

5

Because x is an int and has a wider range as byte. That is why there may be data loss is you assign it to byte.

20 is a constant and while compile time garanteed to be in the range of byte.

juergen d
  • 201,996
  • 37
  • 293
  • 362
3

Because compiler is not able to figure out the value of X at compile time. So it assume that X can contain value which is greater than byte range. If you make variable X as final then it will not give you compile time error.

        final int x=10;
        byte b=20;//no compilation error
        byte c=x;//no compilation error
JProgrammer
  • 1,135
  • 1
  • 10
  • 27
1

20 always can be represented by a byte, while x, which according to the compiler can be any integer, may be too large to be represented by a byte.

Steven
  • 2,437
  • 5
  • 32
  • 36
1

20 is in -128..127 range, so it's value fits into byte.

yatskevich
  • 2,085
  • 16
  • 25
1

In this case x is initialized to 10, so there will be no data loss in the conversion from a 32-bit int to an 8-bit byte. But in general, when converting from int to byte, there can be data loss, so the rules of the Java language forbid assigning int values to a byte without a cast. This rule is designed to make it more difficult to write buggy code. By inserting the (byte) cast, you are effectively telling the compiler: "Yes, I have thought about the possibility of data loss, and it is not a problem here (or, that's actually what I want)."

Alex D
  • 29,755
  • 7
  • 80
  • 126
1

When the compiler looks at the line

byte b=20;

it knows it's looking for a byte after b=. When it finds a constant numeric value it knows at compile time that it will definitely be in the range of byte so it will automatically cast it.

When is sees the line

byte c=x;

it's once again looking for a byte after c= but instead of finding a numeric constant, finds a variable that already has a defined type and, at compile time, can't be sure that it will be in the range of byte so you get an error.

Foggzie
  • 9,691
  • 1
  • 31
  • 48
0

X defined as integer, while narrowing there may be data loss, that is why compiler error. Refer jvm spec for conversions & promotions

kosa
  • 65,990
  • 13
  • 130
  • 167
0

Up-casting is automatic where as down-casting or narrowing (byte c=x;) should be explicit as it might cause loss of precision which a programmer should be aware of explicitly. To correct it you need to put an explicit cast byte c=(byte)x;

Santosh
  • 17,667
  • 4
  • 54
  • 79