• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

some question about the compiler error

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic