I\'m preparing for Java 7 certification and have the following question.
Byte b = 10 compiles ok. Looks like the compiler is narrowing int 10 to byte 10
i think i have a solution for your problem...
//constructor for Byte class
Byte(byte value){
}
There are two rules for java type conversion
Now in Your case you trying to convert int into byte which is against our second rule.... but below is the solution
Byte b = new Byte((byte)10);
Now let's talk about your Second issue...
Long x = 10;//incompatible type
This is the issue of autoboxing... Now as we all know that autoboxing automatically converted primitive type into it's wrapper class.. But conversion not happens in case of autoboxing means....int is converted into Integer byte is converted into Byte.. Now when you assign int primitive type to Long, it gives you error of incompatible type....... Solution
Long x = (long) 10;//works fine....