java - Why i am getting type mismatch: cannot convert from int to byte -


class test {      public static void main(string[] args)     {       byte t1 = 111;      byte t2 =11;      byte t3 = t1+t2;       system.out.println(t1+t2);        }   }   

in eclipse showing error cannot convert int byte.here sum 122 range in byte range.so why getting error here.

thanks in advance...

when mathematical operations on byte, java widening( automatic type promotion) byte(implicitly casted) integer case. when perform

 byte t3 = t1+t2; //  t1+t2; evaluated integer. 

as t1+t2 result wider byte need downcast byte.

to remove compilation error.

 byte t3 = (byte) (t1+t2); // typecast byte 

for more information please read jls 5.1.2


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -