Java Reference
In-Depth Information
Data and Variables
A variable is a named piece of memory that you use to store information in your Java program - a piece
of data of some description. Each named piece of memory that you define in your program will only be
able to store data of one particular type. If you define a variable to store integers, for example, you
cannot use it to store a value that is a decimal fraction, such as 0.75. If you have defined a variable that
you will use to refer to a Hat object, you can only use it to reference an object of type Hat (or any of its
subclasses, as we saw in Chapter 1). Since the type of data that each variable can store is fixed,
whenever you use a variable in your program the compiler is able to check that it is not being used in a
manner or a context that is inappropriate to its type. If a method in your program is supposed to
process integers, the compiler will be able to detect when you inadvertently try to use the method with
some other kind of data, for example, a string or a numerical value that is not integral.
Explicit data values that appear in your program are called literals. Each literal will also be of a
particular type: 25, for instance, is an integer value of type int . We will go into the characteristics of
the various types of literals that you can use as we discuss each variable type.
Before you can use a variable you must specify its name and type in a declaration statement. Before we
look at how you write a declaration for a variable, we should consider what flexibility you have in
choosing a name.
Variable Names
The name that you choose for a variable, or indeed the name that you choose for anything in Java, is
called an identifier. An identifier can be any length, but it must start with a letter, an underscore (_), or a
dollar sign ( $ ). The rest of an identifier can include any characters except those used as operators in
Java (such as + , -, or * ), but you will be generally better off if you stick to letters, digits, and the
underscore character.
Java is case sensitive so the names republican and Republican are not the same. You must not
include blanks or tabs in the middle of a name, so Betty May is out, but you could have BettyMay or
even Betty _ May . Note that you can't have 10Up as a name since you cannot start a name with a
numeric digit. Of course, you could use tenUp as an alternative.
Subject to the restrictions we have mentioned, you can name a variable almost anything you like, except
for two additional restraints - you can't use keywords in Java as a name for something, and a name can't
be anything that is a constant value. Keywords are words that are an essential part of the Java language.
We saw some keywords in the previous chapter and we will learn a few more in this chapter. If you
want to know what they all are, a complete list appears in Appendix A. The restriction on constant
values is there because, although it is obvious why a name can't be 1234 or 37.5 , constants can also be
alphabetic, such as true and false for example. We will see how we specify constant values later in
this chapter. Of course, the basic reason for these rules is that the compiler has to be able to distinguish
between your variables and other things that can appear in a program. If you try to use a name for a
variable that makes this impossible, then it's not a legal name.
Search WWH ::




Custom Search