• Post Reply Bookmark Topic Watch Topic
  • New Topic

some question about the compiler error  RSS feed

 
jwiscou chen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone
I have some doubt follow can anyone suggest why?
1. byte a=0xFF //compile error
byte b=0xFFF //compile error
byte c=0xFFFFFFFF //but here compile succeed why?
2. char a='A';
char b='A'+8; //compile succeed
char c=a+8; //but here compile error why?
Thanks advance
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jwiscou.chen:
Hello everyone
I have some doubt follow can anyone suggest why?
1. byte a=0xFF //compile error
byte b=0xFFF //compile error
byte c=0xFFFFFFFF //but here compile succeed why?


0xFF = 255
0xFFF = 4095
0xFFFFFFFF = -1

and the byte can hold values from -128 to 127. Hence first two statements throw errors, while last one getting compiled.


Originally posted by jwiscou.chen:

2. char a='A';
char b='A'+8; //compile succeed
char c=a+8; //but here compile error why?
Thanks advance



char c = a+8;

Here a is char and 8 is an int. When you perform arithmatic operation between char and int, the result will be int. Hence it is throwing error - beucase char cann't hold int value.
[ September 16, 2006: Message edited by: Chetan Parekh ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two pieces of knowledge that are required to answer the question:
1) the range of the primitive types
2) what a constant expression is
Once you know these, you can reason easily for each of your cases.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!