Java Language Specification: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 12: | Line 12: | ||
} | } | ||
</geshi> | </geshi> | ||
== [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]] |
Revision as of 03:30, 3 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 $;} } </geshi>
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.