commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cubeta, James" <jcub...@verisign.com>
Subject RE: byte b = 'a'; not allowed
Date Mon, 07 Jan 2002 15:35:12 GMT
> -----Original Message-----
> From: Jeff Turner [mailto:jeff@socialchange.net.au]
> 
> There seems to be a bug in 'javac', which lets you implicitly 
> typecast a char
> to a byte. Eg:
> 
> public class Test {
>   public static void main(String args[]) {
>     byte b = 'a';
> }

[being a former java instructor, i've discussed this "feature" alot...]

actually, this is *not* a bug. the compiler is smarter than you think. well,
in this case, anyway... ;)

here's what's happening: because you have used the literal 'a' in code, the
compiler determines the decimal equivalent of the literal and then does
bounds checking to see if it will fit in "b". in your example, 'a' evaluates
to the decimal value 97, which fits into a byte just fine.

just for kicks, try a different literal. i tried assigning the Greek letter
theta to "b" [to do this, i need to use the Unicode literal delimiter]:

public class Test {
   public static void main(String args[]) {
      byte b = '\u0398';  // hex value ==> 920 in base10
   }
}

'javac' gives me this: 

Test.java:3: possible loss of precision
found   : char
required: byte
      byte b = '\u0398';
               ^
1 error

make sense?

later,
james
---------------------------------------------------------------------------
James A. Cubeta                                        jcubeta@verisign.com
VeriSign Global Registry Services                           v: 703.948.3326
21345 Ridgetop Circle, #LS2-2-1                             f: 703.421.8709
Dulles, VA 20166                                      

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message