File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte to char conversion compile error

 
dgjcp kumar
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic