Why is implicit conversion of long not possible (in range)

I understand that byte b = 20; works , and integer 20 is converted to byte type because it is in range,

Why does byte b = 20L; throws an error here also long literal 20 is in range of byte but it does not does implicit conversion , can you please explain this

Hey @bhavik911
byte b = 20L
here L denotes literal entry type is long
20 store in 64 bits
0000000000000000000000000000000000000000000000000000000000010100

yeah same the case with int, right?
it is 00000000000000000000000000010100
but this works , why not with long?

In this Scenario implicit conversion

I mean why does int gets implicitly convert and not long?
20 is int literal which gets implicitly type cast into byte
but 20L is long literal which doesn’t get typecast implicitly, this is what I am not able to understand

@bhavik911
20L is long literal which doesn’t get typecast implicitly .
Yes
we need to explicitly typecast

yeah that’s my question exactly why does long doesn’t get typecast implicitly while int get typecast implicitly!?

@bhavik911
Java has its own specialty .

So its just with int then right?
and not with anything else like double float etc .?

@bhavik911
float f = 3.7; gives error
literal entry is double type

So i think byte b = 20; statements like these only work i.e only integer literal gets typecasted implicitly and no other literal , can this be taken as some kind of exception?

@bhavik911
can this be taken as some kind of exception?
yes

1 Like