Sunday, June 29, 2008

Rules for legal identifiers...


The SCJP exam wants you to know perfectly the rules which define legal and illegal identifiers names. Make sure you know this perfectly as there is no excuse for this. Read the following carefully and you will understand it's not that difficult:

Rules for Legal Identifier Names:

1. Identifiers must start with a letter, a currency character ($), or a connectingcharacter such as the underscore ( _ ). Identifiers cannot start with a number!

2. After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.

3. In practice, there is no limit to the number of characters an identifier cancontain.

4. You can't use a Java keyword as an identifier. Following figure lists all of the Java keywords including one new one for 5.0, enum.

5. Identifiers in Java are case-sensitive; so pen and PEN are two different identifiers.

The list of Java 1.5 Keywords follow -

Some of the legal identifiers are:

$aturn, _$23, _23, $_aturn, there_is_no_limit_to_the_number_of_characters

Some illegal identifiers are:

23$, -saturn, :saturn, saturn#, .saturn

If you have any questions...let them come through the comments, I will gladly answer each one of them.

No comments: