Assigning Variable Types

Variables are the main way that a computer remembers something as it runs a program. The Saluton program in Hour 2 used the greeting variable to hold “Saluton mondo!”. The computer needed to remember that text so that the message could be displayed.

In a Java program, variables are created with a statement that must include two things:

• The name of the variable

• The type of information the variable will store

Variables also can include the value of the information being stored.

To see the different types of variables and how they are created, run NetBeans and create a new empty Java file with the class name Variable.

Start writing the program by entering the following lines:

class Variable {
    public static void main(String[] args) {
        // Coming soon: variables
    }
}

Go ahead and save these lines before making any changes.

Integers and Floating-Point Numbers

So far, the Variable program has a main() block with only one statement in it—the comment // Coming soon: variables. Delete the comment and enter the following statement in its place:

int tops;

This statement creates a variable named tops. It does not specify a value for tops, so for the moment this variable is an empty storage space. The int text at the beginning of the statement designates tops as a variable that is used to store integer numbers. You can use the int type to store most of the nondecimal numbers you need in your computer programs. It can hold any integer ranging from around –2.14 billion to 2.14 billion.


Note

You can use a floating-point variable to store a grade point average such as 2.25 (to pick my own at the University of North Texas—hi, Professor Wells!). You can also use it to store a number such as 0, which is the percentage chance of getting into a good graduate school with my grade point average, which is why I was available in the job market in 1996 when this publisher was looking for computer book authors.


Create a blank line after the int tops statement and add the following statement:

float gradePointAverage;

This statement creates a variable with the name gradePointAverage. The float text stands for floating-point numbers. Floating-point variables are used to store numbers that might contain a decimal point.

The float variable type holds decimal numbers of up to 38 figures. The larger double type holds decimal numbers up to 300 figures.

Characters and Strings

Because the variables you have dealt with so far are numeric, you might have the impression that all variables are used to store numbers. Think again. You also can use variables to store text. Two types of text can be stored as variables: characters and strings. A character is a single letter, number, punctuation mark, or symbol. A string is a group of characters.

Your next step in creating the Variable program is to create a char variable and a String variable. Add these two statements after the line float gradePointAverage:

char key = 'C';
String productName = "Larvets";

When you are using character values in your program, you must put single quotation marks on both sides of the character value being assigned to a variable. For string values, you must surround them with double quotation marks.

Quotation marks prevent the character or string from being confused with a variable name or another part of a statement. Take a look at the following statement:

String productName = Larvets;

This statement might look like one telling the computer to create a string variable called productName and give it the text value of Larvets. However, because there are no quotation marks around the word Larvets, the computer is being told to set the productName value to the same value as a variable named Larvets.

After adding the char and String statements, your program resembles Listing 5.1. Make any necessary changes and be sure to save the file.

Listing 5.1. The Variable Program


1: class Variable {
2:    public static void main(String[] args) {
3:       int tops;
4:       float gradePointAverage;
5:       char key = 'C';
6:       String productName = "Larvets";
7:    }
8: }


The last two variables in the Variable program use the = sign to assign a starting value when the variables are created. You can use this option for any variables you create in a Java program, as you discover later in this hour.


Note

Although the other variable types are all lowercase letters (int, float, and char), the capital letter is required in the word String when creating string variables. A string in a Java program is different than the other types of information you use in variable statements. You learn about this distinction in Hour 6, “Using Strings to Communicate.”


This program can be run but produces no output.

Other Numeric Variable Types

The types of variables you have been introduced to thus far are the main ones you use for most of your Java programming. You can call on a few other types of variables in special circumstances.

You can use the first, byte, for integer numbers that range from –128 to 127. The following statement creates a variable called escapeKey with an initial value of 27:

byte escapeKey = 27;

The second, short, can be used for integers that are smaller in size than the int type. A short integer can range from –32,768 to 32,767, as in the following example:

short roomNumber = 222;

The last of the numeric variable types, long, is typically used for integers that are too big for the int type to hold. A long integer can be from –9.22 quintillion to 9.22 quintillion, which is a large enough number to cover everything but government spending.

When working with large numbers in Java, it can be difficult to see at a glance the value of the number, as in this statement:

long salary = 264400000;


Caution

All the improvements offered in Java 7, including underscores in numbers, will be flagged as an error in the NetBeans source code editor unless the IDE has been set up to recognize Java 7. You learn how to do this in Hour 7, “Using Conditional Tests to Make Decisions.”


Unless you count the zeros, you probably can’t tell that it’s $264.4 million. Java 7 makes it possible to organize large numbers with underscore (_) characters. Here’s an example:

long salary = 264_400_000;

The underscores are ignored, so the variable still equals the same value. They’re just a way to make numbers more human readable.

The boolean Variable Type

Java has a type of variable called boolean that only can be used to store the value true or the value false. At first glance, a boolean variable might not seem particularly useful unless you plan to write a lot of true-or-false quizzes. However, boolean variables are used in a variety of situations in your programs. The following are some examples of questions that boolean variables can be used to answer:

• Has the user pressed a key?

• Is the game over?

• Is my bank account overdrawn?

• Do these pants make my butt look fat?

• Can the rabbit eat Trix?

The following statement creates a boolean variable called gameOver:

boolean gameOver = false;


Note

Boolean numbers are named for George Boole (1815–1864). Boole, a mathematician who was mostly self-taught until adulthood, invented Boolean algebra, which has become a fundamental part of computer programming, digital electronics, and logic. One imagines that he did pretty well on true-false tests as a child.


This variable has the starting value of false, so a statement like this could indicate in a game program that the game isn’t over yet. Later, when something happens to end the game, the gameOver variable can be set to true.

Although the two possible boolean values look like strings in a program, you should not surround them with quotation marks. Hour 7, “Using Conditional Tests to Make Decisions,” describes boolean variables more fully.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.126