• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Tell me Why ?  RSS feed

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I compiled this code...


and this code...


The first code compiles but the second does not. Why ?
Please help.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sherry, please give your topic a meaningful title, and please tell us the compilation error you get. Is the error given by the compiler not meaningful?

Yes, I can see why the second does not compile:In the first case i is a compile time constant within the range of a byte (-128 to 127). In the second it is not a constant and the compiler cannot guarantee in all possible cases that at the time of the assignment that i still contains 100.
[ September 06, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sherry,

Reason is simple in the first code u declared variable as a final and final
variables are constants so compiler knows that the value of this particular variable will not change in runtime ie. will not change from 100.
So it will not go out of range.

In other way compiler replaces all final variable with there values at compile time as in ur code


statement
byte b = i; //for first code

will replace to

byte b = 100; before compilation then compilation will occur.


but in second snap shot of code i

byte b = i ;

will not replace to

byte b =100;

it will simply

byte b = i;


Cheers
Amit Goyal
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sherry, the reason your second file fails to compiles is because i is promoted to an integer before it is assigned to the byte b. To fix the problem you will have to cast i back to a byte (byte) i.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

because i is promoted to an integer before it is assigned to the byte b



i is already an int, it does not have to be promoted.
 
Ranch Hand
Posts: 980
 
What a show! What atmosphere! What fun! What a tiny ad!
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!