• 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

Cannot convert char to byte

 
Greenhorn
Posts: 11
Google Web Toolkit Angular Framework Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

Can you please tell me why the below code giving me compilation error : Type mismatch


In above code method m1() working fine but method m2() giving compilation error.

So can you please tell me why?

Regards,
Shainaz
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your method m2 is declared to return a bye value (8 bits size), and it is returning a char (16 bits size)!
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

size of the type does not matter the range of the values matter.

as range of character is 0 to 65536 and range of byte is -127 to 128.

byte to chat conversion
say
byte bt = -10 ;
char ch1 = bt ; //-------error , why -10 does not lie in the range of char.
char ch1 = (char) bt; //---------It is a narrow conversion Always cast.


similarly char to byte conversion
char ch = 666 ;
byte bt = ch ; //-------error , why 666 does not lie in the range of byte.
byte bt = (byte) ch; //---------It is a narrow conversion Always cast.




 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:
size of the type does not matter the range of the values matter.

as range of character is 0 to 65536 and range of byte is -127 to 128.

byte to chat conversion
say
byte bt = -10 ;
char ch1 = bt ; //-------error , why -10 does not lie in the range of char.
char ch1 = (char) bt; //---------It is a narrow conversion Always cast.


similarly char to byte conversion
char ch = 666 ;
byte bt = ch ; //-------error , why 666 does not lie in the range of byte.
byte bt = (byte) ch; //---------It is a narrow conversion Always cast.



Yes, i am sorry. You are right.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naveen,

You are right, But it actually does not answer the question properly,
The complete code is like below, shainaz is my friend.


So, here my question is even if the user supplies the same value as in m1(), then based on your explanation, it should cause the error.
As it is not throwing in m1().
So, as per me there is something to do with the interpretation of this in JVM, where JVM says, if the value is expected to come at runtime, then it falls in your explanation.

Please feel free to correct me.
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic