Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

final variable.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error is online 4, but if i remove final modifier from byte , it gives error on line 1 too!! why is it so??
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that a char is an unsigned 2-byte integer, and a byte is a 1-byte signed integer. There are potential values of a byte which can't be stored in a char.

The reason that it doesn't give a compile-time error when you include final is that the value of the byte is known and can't be changed and the value of the byte can be stored in a char.

Note what happens if you change the line to

final byte b = -1;

Test9.java:8: possible loss of precision
found : byte
required: char
c = b; // 1
^



[ March 03, 2006: Message edited by: Keith Lynn ]
[ March 03, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one thing to note about these "constants" is that the compiler checking is only done when declaration and initialzation are done in the same statement:
final byte x ;
x = 10;
char c = x;
--> won't compile!
reply
    Bookmark Topic Watch Topic
  • New Topic