Java Language Specification

From EggeWiki

Jump to: navigation, search

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.

public class Test<$> {
	int $$;String $0="$";$ $;
	$ $(){if($$==0)System.out.println($0!=$?$0:$);return $;}
	public static void main(String args[]) { new Test().$();}
}

Hexadecimal Floating Point Literals

I never knew about this part of the grammar.

HexadecimalFloatingPointLiteral:
        HexSignificand BinaryExponent FloatTypeSuffixopt
 
HexSignificand:
        HexNumeral
        HexNumeral .
        0x HexDigitsopt . HexDigits
        0X HexDigitsopt . HexDigits
 
BinaryExponent:
        BinaryExponentIndicator SignedInteger
 
BinaryExponentIndicator:one of
        p P

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.

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 == π);
  }
}

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:

        final int x = 255;
        short y = x;

But this is not.

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

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

Personal tools
Travelling Salesman

Get the app!