2.4. Variables

 
[Page 30 ( continued )]

2.3. Identifiers

Just as every entity in the real world has a name , so you need to choose names for the things you will refer to in your programs. Programming languages use special symbols called identifiers to name such programming entities as variables , constants, methods , classes, and packages. Here are the rules for naming identifiers:

  • An identifier is a sequence of characters that consists of letters , digits, underscores ( _ ), and dollar signs ( $ ).

  • An identifier must start with a letter, an underscore ( _ ), or a dollar sign ( $ ). It cannot start with a digit.


    [Page 31]
  • An identifier cannot be a reserved word. (See Appendix A, "Java Keywords," for a list of reserved words.)

  • An identifier cannot be true , false , or null .

  • An identifier can be of any length.

For example, $2 , ComputeArea , area , radius , and showMessageDialog are legal identifiers, whereas 2A and d+4 are illegal identifiers because they do not follow the rules. The Java compiler detects illegal identifiers and reports syntax errors.

Note

Since Java is case-sensitive , X and x are different identifiers.


Tip

Identifiers are used for naming variables, constants, methods, classes, and packages. Descriptive identifiers make programs easy to read. Besides choosing descriptive names for identifiers, there are naming conventions for different kinds of identifiers. Naming conventions are summarized in §2.14, "Programming Style and Documentation."


Tip

Do not name identifiers with the $ character . By convention, the $ character should be used only in mechanically generated source code.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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