Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Day 2
Java Language Fundamentals

Today, you’ll 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 you’re already comfortable with the Java language, or you’re a competent C/C++ programmer, this chapter serves as a reference. If it’s all new to you, this chapter makes an excellent tutorial.

We’ll cover the basics of the Java language in a logical order. The following are the topics we’ll 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 that’s 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, you’d 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. It’s 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 it’s clear what they’re 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 aren’t 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, it’s 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 it’s easy to see that all three lines of the declaration are related.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.