To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98
Day 2 Java Language Fundamentals
Today, youll learn about the Java language. C and C++ programmers will be comfortable with the material because the syntax of Java and C/C++ are almost identical. If youre already comfortable with the Java language, or youre a competent C/C++ programmer, this chapter serves as a reference. If its all new to you, this chapter makes an excellent tutorial.
Well cover the basics of the Java language in a logical order. The following are the topics well cover today:
- Variables
- Expressions
- Program control
- Exceptions
Java Variables
Your programs need to store values. For instance, you might have a program that calculates the resistance of an electrical circuit based on the amperage and voltage. The user inputs the amperage and voltage, and the resistance is calculated. The amperage and voltage, though, need to be stored somewhere so that when the calculation is performed, the values that the user entered are available.
Not only do variables store values, but they also can be altered. You can multiply a value thats stored in a variable by 5, increment it, negate it, or perform any number of other operations.
Variables can control the flow of program execution and affect the overall state of a program. They are a fundamental part of programming languages; without them, youd be hard-pressed to develop software.
Declaring Variables
Before you use a variable, you must declare it. A variable has two parts, the data type and the identifier:
type identifier;
The data type determines the legal range of values that a variable can contain, what operations can be applied to the variable, and how such operations are performed. For instance, an integer variable deals with whole numbers, and a double variable deals with floating-point numbers.
The identifier is used to associate a name with a variable. This gets confusing for some of my students. They think the name of a variable is int. Its not. The name is anything you want to give it such as George, Sam, or Steve. The int part of it has nothing to do with the name.
Any number of variables can be declared on a single line, each of the same type, as long as each identifier is unique and is separated from the others by a comma. A semicolon is used to signal the end of a variable declaration.
Here, an integer variable is declared and is given the name horizontal:
int horizontal;
Naming Variables
In Java, variable names commonly start with a lowercase letter. All other words that make up the variable are uppercase. For instance, you might have leftCorner, rightSide, or niceBigValue.
Many C++ programmers who program in Java are now adopting some of the Hungarian notation conventions. In this case, because the variable already starts with a lowercase letter, all the first words that make up the variable will begin with an uppercase character. Table 2.1 shows the basic numeric variables that observe this convention. Similarly named variables will be used throughout the book.
Table 2.1 Basic Numeric Variable Naming Conventions used Throughout the Book
|
Declaration
| Description
|
|
int nValue;
| Integers can be preceded by an n or i character.
|
int iValue;
| In this book, the n character is used for signed variables; i, for unsigned.
|
long lValue;
| Longs are preceded by an l character.
|
double dValue;
| Doubles are preceded by a d character.
|
String strData;
| Strings are preceded by the str characters.
|
|
Member variables are usually preceded by m_, whereas local variables are not, as shown in Table 2.2.
Table 2.2 Naming Conventions for Member and Local Variables
|
Declaration
| Description
|
|
int m_nValue;
| Integer member variable
|
String m_strData;
| String member variable
|
int nValue;
| Integer local variable
|
String strData;
| String local variable
|
|
Some variables with single-character names are left as single characters if its clear what theyre being used for. Examples of this are variables named x, y and i. A variable named x almost always signifies the x value of a coordinate pair, and a variable named y almost always signifies the y value of a coordinate pair. A variable named i is almost always used as a local loop counter.
In the following example, several integer variables are declared, all on the same line:
int nHorizontal, nVertical, x, y, nMonths, nYears, nDays;
However, you could have broken the preceding declaration into several separate declarations:
int nHorizontal;
int nVertical;
int x;
int y;
int nMonths;
int nYears;
int nDays;
How you declare variables depends mainly on personal taste, although most programmers would choose the first example for ease of readability. You arent required to place all variable names on the same line. You can spread them over several lines, if you want, as long as the last variable name is immediately followed by a semicolon:
int nHorizontal, nVertical,
x, y,
nMonths, nYears, nDays;
Notice how much more readable the preceding declarations are. With the variables grouped according to their purpose, its easier for people reading the code to understand the variable organization. You should also notice that the second and third lines of the preceding example are indented so that its easy to see that all three lines of the declaration are related.
|