wood burning stoves 2.0*
The moose likes Mock Exam Errata and the fly likes (#61) Can you automatically/implicitly convert a char to a short? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "(#61) Can you automatically/implicitly convert a char to a short?" Watch "(#61) Can you automatically/implicitly convert a char to a short?" New topic
Author

(#61) Can you automatically/implicitly convert a char to a short?

Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
The answer for this :
"No. They're the same bit-depth, but since chars are unsigned they might have a higher positive value than a short can accept"

I quite don't understand this. If by saying "automatically/implicitly convert a char to a short" we mean this:



this compile and run ok, and i did it automatically/implicitly.
Now if we try to assign character which value is greater than 32767 then i understand that this will cause compilation error but does it mean i can't
"automatically/implicitly" try to do it (just like in casting)?
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
That is a interresting question. What is the answer?


Power from within.

Failed SCJP 2 times :-(
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Pawel Nowacki wrote:The answer for this :
"No. They're the same bit-depth, but since chars are unsigned they might have a higher positive value than a short can accept"

I quite don't understand this. If by saying "automatically/implicitly convert a char to a short" we mean this:



this compile and run ok, and i did it automatically/implicitly.
Now if we try to assign character which value is greater than 32767 then i understand that this will cause compilation error but does it mean i can't
"automatically/implicitly" try to do it (just like in casting)?


Your example isn't taking a char and implicitly converting it to a short. You are just assigning the code for 'c' to a short.



This code declares little as a char and gives it the value 'c'. The next line declares t as a short, and tries to pass the value of 'little' into it. When the compiler tries to implicitly convert little to a short it fails, giving the following error:



SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: (#61) Can you automatically/implicitly convert a char to a short?
 
Similar Threads
Char to Short
Javarules Roundup #125
Assigning a char array to an int array
Casting
char compability...