• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cast byte to char

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Just by declaring byte as final, why casting is not needed to assign byte to char ??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shubha Kirani:

Just by declaring byte as final, why casting is not needed to assign byte to char ??



Explicit casting is needed between primative types when the ranges of the source doesn't fit into the target's range. For byte and char, explicit casting is need for both directions.

Declaring bytes as final is not enough to allow implicit casting. The byte has to be defined as a compile time constant. When it is a compile time constant, the compiler can figure out the value of the byte, to check to see if it can be casted implicitedly.

It so happens that your test generates a compile-time constant when it is declared final. If it was a method call, or uses a variable that isn't a compile time constant, declaring it as final would not work.

Henry
 
Shubha Kirani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

Thanks a lot for your reply. What you said is right, I did change the code like this and confirmed.



Good learning, Thank you.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, cowboys!

One small point is sometimes forgotten. Mainly because it is so obvious that you never think about it:
By declaring a variable final (in a compile time constant way) you can only omit the casting when the value of the variable "fits into the container".

So:
final byte b = -5;
char c = b;


The last line will not compile.
b is a compile time constant, but chars can't hold negative numbers.
When you casted against char, it worked - with an overflow.


Yours,
Bu.
[ June 16, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not arguing, just I am not clear on this one.....

if a byte is 8 bits and a char is 16, why would casting be required to fit 8 bits into 16? Does it have to do with signed versus unsigned?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. But among byte, char, short, int, long, float, double
only char is unsigned, so casting of any of these to char is a narrowing conversion that demands explicit casting.

Because of the signs also
char -> short
short -> char
ist a narrowing conversion in both directions, and needs a cast.

You may also ruin your weekend by studying this in detail:
Java Language Specification
5.1.2 Widening Primitive Conversion
5.1.3 Narrowing Primitive Conversions


Yours,
Bu.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the references sir! Much appreciated.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
De nada.

But you shouldn't adress people as Sir here.

Yours,
Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic