Q#1 :
byte b = 5;
you assign integer literal (5) directly to b, so the constant value will be type byte.
b = +b or b = -b //Does NOT compile
you are NOT assign integer literal (5) directly to b, but you are using variable "b" and you add a unary arithmetic (+/-), so because this is a unary, it will be promote to "int", that's why it won't compile, you need to do an explicit cast : b = (byte)-b .
Q#2 :
Remember this rule :
Given : the variable of type is 'T', the value is 'expr', and the binary operator 'op',
the expression : var op= expr,
is equivalent to : var = (T)((var) op (expr)).
byte b = 5;
b += 1; // b = (byte)((b) + (1))
b += b; // straightforward
b += 1048576; // b = (byte)((b) + (1048576))
int i = 1048576; // clearly an int
b += i; // b = (byte)((b) + (i)) note : b+i return int.
long l = 2147483647 + 10; // clearly a long
b += l; // b = (byte)((b) + (l)) note : b+l return long.
double d = 12.3; // clearly a double
b += d; // b = (byte)((b) + (d)) note : b+d return double.
Q#3 :
long l = (long) 4294967296;
even the code is :
long l = 4294967296;
first compiler will evaluate the int number (4294967296)before assign it to l.
since the int number is to large, it won't compile.
if you want to assign number outside of the range, the integer constant must followed with "L" for "long", and "D" for double.
so the code will be :
long l = 4294967296L;
bonus :
List of Literals :
1. Reference Literal : constant value is "null",
2. Primitive Literals :
a. boolean : constant value "true" & "false"
b. character : single quotes 'x'
c. integer : constant value are asume "int"
this include (byte, short, int, long, Dec, Octal, Hex).
d. floating point : constant value are assume "double"
this include (double)
3.
String Literals : double quotes "..xxx.."
4. Class Literals : every type is an associated class object :
for example : String.class, boolean.class
hope this help.
stevie