Java Reserved Keywords


Identifiers and Identifier Naming Rules

Identifiers are used in Java programs to formulate the names of classes, variables, constants, and methods. There are a few rules you must be aware of regarding the correct formulation of identifier names and they are easy to remember.

  • Must Begin With Valid Java Letter - An identifier must begin with a valid Java letter. Generally speaking, a valid Java letter is any unicode character other than the Java operators (%, *, /, etc.) and a few other symbols. One way to test whether or not a character you wish to use as the start of an identifier is valid is to use a handy method supplied by the Character class named isJavaIdenti-fierStart(). An example of its use is shown in example 6.3 below.

    Example 6.3: IdentifierTest.java

    image from book
     1      public class IdentifierTest { 2       public static void main(String[] args){ 3            String  \u03B63_3 = "Hello - \u03B63_3 is a valid identifier!"; 4            ystem.out.println(\u03b63_3); 5            System.out.println(Character.isJavaIdentifierStart('\u03B6')); 6            System.out.println(Character.isJavaIdentifierStart('Ô')); 7            System.out.println(Character.isJavaIdentifierStart('')); 8            System.out.println(Character.isJavaIdentifierStart('$')); 9            System.out.println(Character.isJavaIdentifierStart('a')); 10           System.out.println(Character.isJavaIdentifierStart('Å')); 11           System.out.println(Character.isJavaIdentifierStart('Í')); 12       } 13    }
    image from book

  • First Letter Can Be Followed By Valid Letters Or Digits - The first letter of an identifier can be followed by any number of valid Java letters and digits. Characters to avoid are, again, the Java operators. You can use the underscore ‘_’ character if you desire. If in doubt you can check the validity of the follow-on characters with another method supplied by the Character class named isJavaIdentifierPart().

  • Identifier Cannot Spell The Name Of A Reserved Java Keyword - The reserved Java keywords hold special meaning in the language and cannot be reused for your special purposes. The Java keywords are covered in detail in the next section.

  • Identifier Cannot Spell true, false, or null - Although true, false, and null are not reserved keywords, they cannot be used as identifiers because they represent the boolean and null literals.

Example 6.3 gives the code for a short program that uses a Unicode escape sequence (\uxxxx) to start an identifier.

The identifier \03B63_3 used on line 3 actually contains two valid Java letters and two valid Java digits. The identifier begins with the Unicode escape sequence ‘\u03B6’. This represents one Java letter. It is then followed by the character sequence 3_3 which is the Java digit ‘3’, followed by the underscore ‘_’ character, which is then followed by the digit ‘3’ again.

On line 3 of example 6.3 the identifier \u03B63_3 is declared to be a String reference. It is used again on line 4 as an argument to the System.out.println() method. This results in its String value being printed to the console.

Lines 5 through 11 of example 6.3 test several characters for their validity as identifier starting letters. The isJavaIdentifierStart() method returns the boolean value true if a letter being tested can serve as an identifier starting letter or false otherwise.

Figure 6-2 shows the results of running the IdentifierTest program. Notice how the Unicode character represented by the escape sequence \u03B6 is represented by a ? in the console output. That’s happening because my terminal emulator is using an encoding scheme that contains just a subset of the Unicode characters, namely, ASCII. In fact, the Unicode escape sequence is a mechanism that lets you use ASCII characters to represent Unicode characters in your programs using a standard text editor. Since most text editors only operate on the ASCII character set, the Unicode escape sequence is the only way to include Unicode characters in your Java programs.

image from book
Figure 6-2: Results of Running IdentifierTest Program

Well-Named Identifiers Will Simplify Your Life

OK, now that you know what an identifier is and the valid forms an identifier can take you will be relieved to learn that identifier naming is easy to learn. Simply avoid naming your identifiers after the reserved keywords and you’ve just about got it licked.

The most difficult part of identifier naming involves deciding on what names to use given the context of your program. The best help you can give yourself in your early programming career is to put some thought into your identifier names. Well-named identifiers will make your source code easy to read. Easy-to-read code is easier to understand. Code that’s easy to understand is easy to fix should it contain an error or two. Problems can be quickly spotted when your program reads like a story. (See chapter 1 for a detailed discussion on an approach to identifier naming.)




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net