Java Language Specification: Difference between revisions

From EggeWiki
mNo edit summary
mNo edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
I've been reading the [http://java.sun.com/docs/books/jls/third_edition/html/ Java Language Specification] to improve my knowledge of the Java language.  Here's a list of things I've learned:
I've been reading the [http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html Java Language Specification] to improve my knowledge of the Java language.  Here's a list of things I've learned:


== $ identifier ==
== $ identifier ==
Line 7: Line 7:
This means I can make my Java programs look like Perl.
This means I can make my Java programs look like Perl.
<geshi lang="java5">
<geshi lang="java5">
int $$;String $0="$",$;
public class Test<$> {
void $(){if($$==0)System.out.println($0!=$?$0:$);}
int $$;String $0="$";$ $;
$ $(){if($$==0)System.out.println($0!=$?$0:$);return $;}
public static void main(String args[]) { new Test().$();}
}
</geshi>
</geshi>
== [http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.2 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 [http://www.davidflanagan.com/blog/000044.html 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:
<pre>
7.96875
3.141592653589793
3.141592653589793
true
</pre>
Unfortunately the exponent must be a signed integer, which prevents creating literals like <code>g0pee</code>;
== [http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#184206 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.


[[Category:Java]]
[[Category:Java]]

Latest revision as of 22:48, 9 November 2007

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.