*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Javarules Roundup #125 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Javarules Roundup #125" Watch "Javarules Roundup #125" New topic
Author

Javarules Roundup #125

dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
hi all. just took javarules roundup and....
Question states: Can you automatically/implicitly convert a char to a short?
Answer given is no. They're the same bit-depth but since chars are unsigned, they might have a higher positive value than a short can accept.
Comment: I can automatically / implicitly convert a char to a short if the char variable is declared final and value not more than short's allowable maximum value. The code below when placed in a method and class compiles just fine.

Am i not reading the question right?


SCJP 1.4<br />SCWCD 1.4
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
I think the question reffers implicitly to variables...
when you decared final char ...
you are dealing with a constant so not a variable but a value so its known at compile time) thats why u don't get a compile error
(value 3 is OK wihin range)
to better understand try the values 32767 (compiles since is last valid value for short) and 32768 (error) with you final char definition


"Did anyone understand what I have just explained? ... because I did not!"
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
thanks Dan.
 
 
subject: Javarules Roundup #125
 
Similar Threads
Char to Short
FINAL VARIABLE
Casting hierarchy
(#61) Can you automatically/implicitly convert a char to a short?
Doubt