站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Variable Names - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Variable Names

A program refers to a variable's value by the variable's name. For example, when it displays the value of the largestByte variable, the MaxVariablesDemo program uses the name largestByte. A name, such as largestByte, that's composed of a single identifier is called a simple name. Simple names are in contrast to qualified names, which a class uses to refer to a member variable that's in another object or class. This topic is covered further in the Using Objects (in the Learning the Java Language trail) section.

In the Java programming language, the following must hold true for a simple name:

  • It must be a legal identifier. Recall from the preceding section that an identifier is an unlimited series of Unicode characters that begins with a letter.
  • It must not be a keyword (in the Learning the Java Language trail), a boolean literal (true or false), or the reserved word null.
  • It must be unique within its scope. A variable can have the same name as a variable whose declaration appears in a different scope. In some situations, a variable can share names with another variable, which is declared in a nested scope. Scope is covered in the next section.

By Convention:  Variable names begin with a lowercase letter and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter (for example, isVisible). The underscore character ( _ ) is acceptable anywhere in a name, but by convention it is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited).

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.