Java Language Specification

From EggeWiki

I've been reading the Java Language Specification to improve my knowledge of the Java language. Here's a list of things I've learned:

$ identifier

The $ character can be part of a Java identifier. "for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems."

This means I can make my Java programs look like Perl. <geshi lang="java5"> public class Test<$> { int $$;String $0="$";$ $; $ $(){if($$==0)System.out.println($0!=$?$0:$);return $;} public static void main(String args[]) { new Test().$();} } </geshi>

Hexadecimal Floating Point Literals

I never knew about this part of the grammar.

<geshi lang="bnf"> HexadecimalFloatingPointLiteral:

       HexSignificand BinaryExponent FloatTypeSuffixopt

HexSignificand:

       HexNumeral
       HexNumeral .
       0x HexDigitsopt . HexDigits
       0X HexDigitsopt . HexDigits

BinaryExponent:

       BinaryExponentIndicator SignedInteger

BinaryExponentIndicator:one of

       p P

</geshi>

I can find no examples on the Internet of anyone using this feature in production code, although I did find a few other samples. It was an obscure feature introduced with Java 5.0.

<geshi lang="java5"> public class A {

 static double f = 0xF.Fp-1D;
 static double pi = Double.longBitsToDouble(0x400921FB54442D18L);
 static double π = 0x3.243F6A8885A308D31319p+0;
 public static void main(String[] args) {
   System.out.println(f);
   System.out.println(pi);
   System.out.println(π);
   System.out.println(pi == π);
 }

} </geshi>

The output is:

7.96875
3.141592653589793
3.141592653589793
true

Unfortunately the exponent must be a signed integer, which prevents creating literals like g0pee;

Assignment Conversions

"A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable."

This means this is legal:

<geshi lang="java5">

       final int x = 255;
       short y = x;

</geshi>

But this is not. <geshi lang="java5">

       int x = 255; // not final
       short y = x;

</geshi>

I found it's interesting that this works for byte, short, and char, but not for long.