In this lab, you will be introduced to variables that store primitive data types.
Download the jar file containing the program VarLab.jar. Follow the instructions that you learned in the NetBeans Lab to load this file into a project called VarLab. Here is a brief summary, in case you have forgotten:
[user@mach dir] cd csci/201 [user@mach dir] jar xfv VarLab.jar
[user@mach dir] netbeans &
Perhaps the best analogy for a variable is a bottle. Think of a bottle - you put stuff inside it, then you put a label on the outside so that you know what you put in it. Variables work this way. A variable is nothing more than a container which holds some data that you refer to by it's name.
Java has two types of variables: those that store primitive data types and reference variables. The only difference between them is what they hold: variables that store primitive data types hold data, reference variables hold the locations of objects in the heap. Reference variables are used when working with objects, so we will wait awhile to discuss them.
When a variable is created, it is located in an area of the computer's memory called the stack. You can think of the stack as just a collection of variables.
To create a "primitive variable" (a variable that stores a primitive data type), you first specify the type of the variable you want to create, and then its name. Here are a couple of examples:
int x; //Creates a new variable of type //int called x double myVar; //Creates a new variable of type //double called myVar boolean runFlag; //Creates a new variable of type //boolean called runFlag
For your reference, here is a list of all primitive data types that
you can use when creating variables:
|Type||What it Holds|
|void||Doesn't contain anything at all|
|boolean||Holds either true or false|
|byte||Holds an integer from -128 to 127|
|short||Holds an integer from -32,768 to 32,767|
|int||Holds an integer from -2,147,483,648 to 2,147,483,647|
|long||Holds an integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|float||Holds a decimal number - Single Precision|
|double||Holds a more precise representation of a decimal number - Double Precision|
|char||Holds one character (letter, number, punctuation, etc)|
So now you know how to create a variable, but that's only half the battle. What good is creating a variable if you don't store anything in it? The assignment operator ( = ) is used to store data in a variable. Here are some examples:
int x; //Create a variable called x x = 5; //Put 5 inside x boolean myVar = false; //You can even do this when //you create the variable
So what is happening here? When you created x, a location in the stack was set aside and labeled x. The next line told the computer to put 5 in the location called x. When you created myVar, the same kind of thing happened, except that false was stored in a temporary location in the CPU until the space was created for myVar. This is all illustrated in the following Stack N' Heap:
Did you notice line 4? In java, as with most programming languages, you use variables where you normally would use what they contain. In this case, instead of saying int y = 5 , we replaced the integer 5 with x. The computer gets the value stored in x and places it in a temporary location in the CPU (i.e., a register). Then the assignment operator takes that value and stores it in y, just as if we had typed int y = 5. Cool, huh? The end result is y equals x - they store the same value.
Open up the file VarLab.java in NetBeans. Compile and run this file. Looks fine right? Try entering a number (other than zero). Ooops! This is what computer scientists call a logic error. The program is syntactically correct and compiles, but does the wrong thing.
You job is to correct this program. Good luck!
Hint: You only need to modify one character...