File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte -- what's going on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Byte -- what Watch "Byte -- what New topic
Author

Byte -- what's going on

Prosenjit Banerjee
Greenhorn

Joined: May 04, 2001
Posts: 20
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

Joined: Sep 23, 2000
Posts: 3285
    
    7
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Prosenjit Banerjee
Greenhorn

Joined: May 04, 2001
Posts: 20
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

Joined: Jun 07, 2001
Posts: 60
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]


------------------
 
Don't get me started about those stupid light bulbs.
 
subject: Byte -- what's going on