Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte -- what's going on

 
Prosenjit Banerjee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prosenjit Banerjee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
william shen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]


------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic