The following error shows in line 2 : Incompatible type for declaration. Explicit cast needed to convert int to byte. This error goes off if I write . My question is why is trying to widen to type int ? Thanks in advance.
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2912
posted
0
That's the rule. every integral type is widened to atleast an int before any computation. Atleast implies if one operand is bigger than an int (say long) then the other will be widened to long. If both are smaller than int then both will be widened to int. Excercise: Why does this work? byte b = 1, c = 2; b += c; System.out.println(b); Check out JQPlus Study Notes -Paul.
Sorry sir, but I really could not understand how the above code works as I don't understand how the following code works too :
where the following does NOT work :
But as far as I remember I found somewhere in this forum that if we write var1 += var2 where var1 is a variable of type Type and var2 is a variable or a literal of any type then the compiler treated it as follows : var1 = var1 + (Type)var2 ; Am I correct, sir, please please explain me. Thank you very much in advance. I LOVE JAVARANCH.COM
william shen
Ranch Hand
Joined: Jun 07, 2001
Posts: 60
posted
0
Hi,Prosenjit: You should check out JQPlus Study Notes.There this problem is explicitly explained. p += .1 is equal to p=(int)(p+ .1)!
Originally posted by Prosenjit Banerjee: [B]Dear Sir,
Sorry sir, but I really could not understand how the above code works as I don't understand how the following code works too :
where the following does NOT work :
But as far as I remember I found somewhere in this forum that if we write var1 += var2 where var1 is a variable of type Type and var2 is a variable or a literal of any type then the compiler treated it as follows : var1 = var1 + (Type)var2 ; Am I correct, sir, please please explain me. Thank you very much in advance. I LOVE JAVARANCH.COM[/B]