• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 1056
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 13974
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic