• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does primitive conversion involving "char" always require explicit cast?

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does primitive conversion involving "char" always require explicit cast?
When I do a conversion from byte->char and vice versa it always requires cast.
If I had done this with byte->int and vice versa
then only conversion "int to byte" would have reqired explicit cast (as narrowing primitive conversion):
Conversion char to byte and vice versa


C:\Java\EigeneJavaProgramme>javac Question02d.java
Question02d.java:6: possible loss of precision
found : char
required: byte
byte i = j; //line 2
corrected :

C:\Java\EigeneJavaProgramme>java Question02d
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forgot the rest:

result
C:\Java\EigeneJavaProgramme>java Question02d
99

Conversion byte => char

C:\Java\EigeneJavaProgramme>java Question02d
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a char is unsigned, casting a byte or short can cause a loss of precision.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Paul,
I tried out to convert long to char and vice versa. It seems that all conversions involving a number and char reqire explicit cast.
Is it so?
Ciao
Thomas
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general rules for primitive assignment conversion can be stated as follows:
  • A boolean may not be converted to any other type
  • a non-boolean may be converted to another non-boolean type provided the conversion is a widening conversion.
  • A non-boolean may not be converted to another non-boolean type if the conversion would be a narrowing conversion.

  • Diagram of widening conversions:

     
    Marilyn de Queiroz
    Sheriff
    Posts: 9065
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems that all conversions involving a number and char reqire explicit cast.

    A long is too big to fit into a char.

    Conversions assigning a number variable to a char require an explicit cast. However, you can assign the int literal '45' to a char.
     
    Ron Newman
    Ranch Hand
    Posts: 1056
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A byte is not too long to fit in a char, but a byte is signed while a char is unsigned. If the value of your byte is between -128 and -1, it will be changed into some positive number.
    The compiler is trying to draw your attention to this fact -- by writing an explicit cast, you can tell the compiler that you're aware of it.
     
    Thomas Paul
    mister krabs
    Ranch Hand
    Posts: 13974
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Thomas Markl:
    Hello Paul,
    I tried out to convert long to char and vice versa. It seems that all conversions involving a number and char reqire explicit cast.
    Is it so?
    Ciao
    Thomas
    Yes it is.
    Here is the chart:
    byte - short - int - long - float - double
    Any movement towards the right does not require a cast. Any movement towards the left requires a cast. Any movement to char (which isn't in the list) requires a cast.
    [ August 31, 2002: Message edited by: Thomas Paul ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic