Java Reference
In-Depth Information
Clearly, it makes sense to choose names for your variables that give a good indication of the sort of data
they hold. If you want to record the size of a hat, for example, hatSize is not a bad choice for a
variable name whereas qqq would be a bad choice. It is a common convention in Java to start variable
names with a lower case letter and, where you have a name that combines several words, to capitalize
the first letter of each word, as in hatSize or moneyWellSpent . You are in no way obliged to follow
this convention but since almost all the Java world does, it helps to do so.
If you feel you need more guidance in naming conventions (and coding conventions in general) take
a look at http://www.javasoft.com/docs/codeconv/ .
Variable Names and Unicode
Even though you are likely to be entering your Java programs in an environment that stores ASCII, all
Java source code is in Unicode (subject to the reservations we noted in Chapter 1). Although the
original source that you create is ASCII, it is converted to Unicode characters internally, before it is
compiled. While you only ever need ASCII to write any Java language statement, the fact that Java
supports Unicode provides you with immense flexibility. It means that the identifiers that you use in
your source program can use any national language character set that is defined within the Unicode
character set, so your programs can use French, Greek, or Cyrillic variable names, for example, or even
names in several different languages, as long as you have the means to enter them in the first place. The
same applies to character data that your program defines.
Variables and Types
As we mentioned earlier, each variable that you declare can store values of a type determined by the
data type of that variable. You specify the type of a particular variable by using a type name in the
variable declaration. For instance, here's a statement that declares a variable that can store integers:
int numberOfCats;
The data type in this case is int , the variable name is numberOfCats , and the semicolon marks the
end of the statement. The variable, numberOfCats , can only store values of type int .
Many of your variables will be used to reference objects, but let's leave those on one side for the moment as
they have some special properties. The only things in Java that are not objects are variables that correspond
to one of eight basic data types, defined within the language. These fundamental types, also called primitive
types, allow you to define variables for storing data that fall into one of three categories:
Numeric values, which can be either integer or floating point
Variables which store a single Unicode character
Logical variables that can assume the values true or false
All of the type names for the basic variable types are keywords in Java so you must not use them for other
purposes. Let's take a closer look at each of the basic data types and get a feel for how we can use them.
Search WWH ::




Custom Search