aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes byte to char conversion compile error 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 to char conversion compile error" Watch "byte to char conversion compile error" New topic
Author

byte to char conversion compile error

dgjcp kumar
Greenhorn

Joined: Nov 23, 2004
Posts: 4
public class first{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
}
}

if i remove final before byte b then stmt marked as 1 gives compile time error? why.
Federico Zuppa
Greenhorn

Joined: Nov 23, 2004
Posts: 7
In theory, a byte can't be assigned to a char as the byte is signed and the char unsigned. The final modifier allows compile time optimizations (See Bruce Eckels book: "In the case of a compile-time constant, the compiler is allowed to �fold� the constant value into any calculations in which it�s used; that is, the calculation can be performed at compile time, eliminating some run-time overhead. In Java, these sorts of constants must be primitives and are expressed with the final keyword. A value must be given at the time of definition of such a constant."). I guess this may have to do with the answer.
Anyway, does the real exam include these types of questions (optimization questions - this one, String optimization with pools, etc.). Besides, don't they depend on the special compiler (I mean, does the JLS say something about it ?)
Premil Jacob
Ranch Hand

Joined: Sep 10, 2004
Posts: 30
kumar,

The "char" is unsigned datatype in that it stores only positive values.
The "byte" datatype on the other hand stores both positive and negative values.

The conversion from byte to char will only happen if the compiler can determine the value of the byte variable ( variable b in ur example) at compile time.
Since u have specified the byte variable with the final modifier, the compiler knows the value that you are assigning to the char variable at compile time.

When you removed the final modifier the compiler could not determine the value of the byte variable at compile time. So it could be that the byte value might have negative values so the compiler complains that the types are incompatible.


Thanks,<br />Premil<br /> <br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte to char conversion compile error