Core Java Variables Core Java

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

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

A variable name must begin with a letter and must be a sequence of letters or digits. Notethat the terms “letter” and “digit” are much broader in Java than in most languages. A letteris 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 speaker scould use a ð. 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 ofa variable name is essentially unlimited.

TIP: If you are really curious as to what Unicode characters are “letters” as far as Java isconcerned, you can use the is Java Identifier Start and is Java Identifier Part methods in the Character class to check.

You also cannot use a Java reserved word for a variable name. You can have multiple declarations on a single line:

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 separatenames. 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 programmersthen 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 assignmentstatement—you can never use the values of uninitialized variables. For example, theJava compiler flags the following sequence of statements as an error:

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

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

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

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

C++ NOTE: C and C++ distinguish between the declaration and definition of variables. Forexample,

is a definition, whereas

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


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

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

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

Note that the definition of the class constant appears outside themain method. Thus, theconstant can also be used in other methods of the same class. Further more, if (as in ourexample) 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. Youmust use final for a constant.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Core Java Topics