Variables

   


In Java, every variable has a type. You declare a variable by placing the type first, followed by the name of the variable. Here are some examples:

 double salary; int vacationDays; long earthPopulation; boolean done; 

Notice the semicolon at the end of each declaration. The semicolon is necessary because a declaration is a complete Java statement.

A variable name must begin with a letter and must be a sequence of letters or digits. Note that the terms "letter" and "digit" are much broader in Java than in most languages. A letter is defined as 'A' 'Z', 'a' 'z', '_', or any Unicode character that denotes a letter in a language. For example, German users can use umlauts such as 'ä' in variable names; Greek speakers could use a p. Similarly, digits are '0' '9' and any Unicode characters that denote a digit in a language. Symbols like '+' or '©' cannot be used inside variable names, nor can spaces. All characters in the name of a variable are significant and case is also significant. The length of a variable name is essentially unlimited.

TIP

If you are really curious as to what Unicode characters are "letters" as far as Java is concerned, you can use the isJavaIdentifierStart and isJavaIdentifierPart methods in the Character class to check.


You also cannot use a Java reserved word for a variable name. (See Appendix A for a list of reserved words.)

You can have multiple declarations on a single line:

 int i, j; // both are integers 

However, we don't recommend this style. If you declare each variable separately, your programs are easier to read.

NOTE

As you saw, names are case sensitive, for example, hireday and hireDay are two separate names. In general, you should not have two names that only differ in their letter case. However, sometimes it is difficult to come up with a good name for a variable. Many programmers then give the variable the same name of the type, such as

 Box box; // ok--Box is the type and box is the variable name 

Other programmers prefer to use an "a" prefix for the variable:

 Box aBox; 


Initializing Variables

After you declare a variable, you must explicitly initialize it by means of an assignment statement you can never use the values of uninitialized variables. For example, the Java compiler flags the following sequence of statements as an error:

 int vacationDays; System.out.println(vacationDays); // ERROR--variable not initialized 

You assign to a previously declared variable by using the variable name on the left, an equal sign (=), and then some Java expression that has an appropriate value on the right.

 int vacationDays; vacationDays = 12; 

You can both declare and initialize a variable on the same line. For example:

 int vacationDays = 12; 

Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:

 double salary = 65000.0; System.out.println(salary); int vacationDays = 12; // ok to declare a variable here 

In Java, it is considered good style to declare variables as closely as possible to the point where they are first used.

C++ NOTE

C and C++ distinguish between the declaration and definition of variables. For example,

 int i = 10; 

is a definition, whereas

 extern int i; 

is a declaration. In Java, no declarations are separate from definitions.


Constants

In Java, you use the keyword final to denote a constant. For example,

 public class Constants {    public static void main(String[] args)    {       final double CM_PER_INCH = 2.54;       double paperWidth = 8.5;       double paperHeight = 11;       System.out.println("Paper size in centimeters: "          + paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH);    } } 

The keyword final indicates that you can assign to the variable once, and then its value is set once and for all. It is customary to name constants in all upper case.

It is probably more common in Java to want a constant that is available to multiple methods inside a single class. These are usually called class constants. You set up a class constant with the keywords static final. Here is an example of using a class constant:

 public class Constants2 {    public static void main(String[] args)    {       double paperWidth = 8.5;       double paperHeight = 11;       System.out.println("Paper size in centimeters: "          + paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH);     }    public static final double CM_PER_INCH = 2.54; } 

Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if (as in our example) the constant is declared public, methods of other classes can also use the constant in our example, as Constants2.CM_PER_INCH.

C++ NOTE

const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.



       
    top



    Core Java 2 Volume I - Fundamentals
    Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)
    ISBN: 0131482025
    EAN: 2147483647
    Year: 2003
    Pages: 132

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