Java is the future !: May 2011

Saturday, May 14, 2011

char a = '\u000A'. Why is this invalid?

Unicode escape characters of the form '\Uxxxx', where xxxx is a hexadecimal value, are processed very early in the translation process (see JLS 3.10.4 ). As a result, the special characters '0A' (line feed) and '0D' (carriage return) are interpreted literally as "end of line."
For example, the expression...

char A = '\u000A';
...therefore becomes...

char A =;
...which results in a compile-time error.
To avoid this error, always use the special escape characters '\n' (line feed) and '\r' (carriage return).

Is it allowed to declare the main method private?

Former JVM versions (pre-1.4) allowed the main method to have any accessibility (private, etc). This incompatibility with Section 12.1.4 of the Java Language Specification has been fixed as of version 1.4. In order to invoke a main method from the command-line, it is now mandatory to declare the main method as follows:

public static void main (String[] args)
If the main method has any access level other than public, it will no longer run from the command-line.